Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Expert: What is wrong here?

Reply
Thread Tools

Expert: What is wrong here?

 
 
Great Deals
Guest
Posts: n/a
 
      09-24-2003
#!/usr/bin/perl
use HTML::LinkExtor;

$input='<a href=hreflink></a> <img src=imglink>';

getlink ('img');
getlink ('a');

sub getlink{
my $matchkey = $_[0];
print "matchkey outsite callback $matchkey \n";

sub callback {

print "matchkey inside callback $matchkey\n";
return;
}

my $p = HTML::LinkExtor->new(\&callback);
$p->parse($input);
}


The output is:
matchkey outsite callback img
matchkey inside callback img
matchkey inside callback img
matchkey outsite callback a
matchkey inside callback img
matchkey inside callback img

But it should be
matchkey outsite callback img
matchkey inside callback img
matchkey inside callback img
matchkey outsite callback a
matchkey inside callback a
matchkey inside callback a


What's wrong here?
 
Reply With Quote
 
 
 
 
Gary E. Ansok
Guest
Posts: n/a
 
      09-24-2003
In article <(E-Mail Removed) >,
Great Deals <(E-Mail Removed)> wrote:
>#!/usr/bin/perl
> use HTML::LinkExtor;


If you'd asked Perl, it would have told you what was wrong.

Although in this case, the short message you get from -w or
use warnings is rather cryptic. However, the longer message
you get from use diagnostics is quite specific on what the
problem is and how to fix it.

>$input='<a href=hreflink></a> <img src=imglink>';
>
>getlink ('img');
>getlink ('a');
>
>sub getlink{
> my $matchkey = $_[0];
> print "matchkey outsite callback $matchkey \n";
>
> sub callback {
>
> print "matchkey inside callback $matchkey\n";
> return;
> }
>


Here's the problem... make this
my $callback = sub { print "matchkey in $matchkey\n"; };

> my $p = HTML::LinkExtor->new(\&callback);


And then this needs to be
my $p = HTML::LinkExtor->new($callback);

Or you can combine them:
my $p = HTML::LinkExtor->new(sub { print "in\n" });

> $p->parse($input);
>}


Gary
--
10:26 a.m. A tan-sweatered hacky sack player allegedly offered marijuana
for sale to the wrong person, who told a shop owner, who called police.
The wacky terbacky hacky lackey fast slacked back sack-smacking heels in
the Pink House, and that's a fact. - Arcata Eye Police Log, 13 Aug 2003
 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      09-24-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Great Deals) wrote:

: #!/usr/bin/perl
: use HTML::LinkExtor;
: $input='<a href=hreflink></a> <img src=imglink>';
: getlink ('img');
: getlink ('a');
: sub getlink{
: my $matchkey = $_[0];
: print "matchkey outsite callback $matchkey \n";
: sub callback {
: print "matchkey inside callback $matchkey\n";
: return;
: }
: my $p = HTML::LinkExtor->new(\&callback);
: $p->parse($input);
: }
:
:
: The output is:
: matchkey outsite callback img
: matchkey inside callback img
: matchkey inside callback img
: matchkey outsite callback a
: matchkey inside callback img
: matchkey inside callback img
:
: But it should be
: matchkey outsite callback img
: matchkey inside callback img
: matchkey inside callback img
: matchkey outsite callback a
: matchkey inside callback a
: matchkey inside callback a
:
:
: What's wrong here?

Add the lines

use warnings;
use diagnostics;

to the beginning of the program. Ask for explanation if the diagnostic
message is unclear.

The cure is to use a real closure instead of trying to define a named
subroutine inside another subroutine.

sub getlink{
my $matchkey = $_[0];
print "matchkey outside callback: $matchkey \n";
my $callback =
sub {
print "matchkey inside callback $matchkey\n";
return;
};
my $p = HTML::LinkExtor->new($callback);
$p->parse($input);
}

 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      09-24-2003
On Wed, 24 Sep 2003 12:18:09 -0700, Great Deals wrote:
> What's wrong here?


Everything? Why don't you just tell us what you are _trying_ to do, and
then will feed you with suggestions on how to solve your problem The Best
Way Possible(tm)?


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Great Deals
Guest
Posts: n/a
 
      09-25-2003
(E-Mail Removed) (Great Deals) wrote in message news:<(E-Mail Removed). com>...
> #!/usr/bin/perl
> use HTML::LinkExtor;
>
> $input='<a href=hreflink></a> <img src=imglink>';
>
> getlink ('img');
> getlink ('a');
>
> sub getlink{
> my $matchkey = $_[0];


local $matchkey = $_[0];
i changed one thing and it works, but i don't know why? why local
works, my does not?

> print "matchkey outsite callback $matchkey \n";
>
> sub callback {
>
> print "matchkey inside callback $matchkey\n";
> return;
> }
>
> my $p = HTML::LinkExtor->new(\&callback);
> $p->parse($input);
> }

i don't want to change \&callback because it is the STANDRED example
in LinkExtor help file. So i think there is nothing wrong there.

>
>
> The output is:
> matchkey outsite callback img
> matchkey inside callback img
> matchkey inside callback img
> matchkey outsite callback a
> matchkey inside callback img
> matchkey inside callback img
>
> But it should be
> matchkey outsite callback img
> matchkey inside callback img
> matchkey inside callback img
> matchkey outsite callback a
> matchkey inside callback a
> matchkey inside callback a
>
>
> What's wrong here?

 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      09-25-2003
(E-Mail Removed) (Great Deals) wrote:

: local $matchkey = $_[0];
: i changed one thing and it works, but i don't know why? why local
: works, my does not?

Because now $matchkey is a global variable instead of a lexical.
&callback is no longer a broken closure; it's an ordinary sub.

: i don't want to change \&callback because it is the STANDRED example
: in LinkExtor help file. So i think there is nothing wrong there.

Long live the Cargo Cult.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments