Expert: What is wrong here?

G

Great Deals

#!/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?
 
G

Gary E. Ansok

#!/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
 
J

Jay Tilton

(e-mail address 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);
}
 
T

Tore Aursand

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)?
 
G

Great Deals

#!/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.
 
J

Jay Tilton

(e-mail address 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top