P
Picker Leon
##code with problems
#!/usr/bin/perl -w
use warnings;
use diagnostics;
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; #WHAT IS THE RETURN HERE FOR?
}
my $p = HTML::LinkExtor->new(\&callback);
$p->parse($input);
}
**************
output:
matchkey outsite callback img
matchkey inside callback img
matchkey inside callback img
matchkey outsite callback a
matchkey inside callback img
matchkey inside callback img
**************
I was told to change lines to those:
my $callback = sub { print "matchkey in $matchkey\n"; }; #WHY NO MORE
RETURN?
my $p = HTML::LinkExtor->new($callback);
*************
Here is the explain from perl:
(W closure) An inner (nested) named subroutine is referencing a
lexical variable defined in an outer subroutine.
When the inner subroutine is called, it will probably see the value of
the outer subroutine's variable as it was before and during the *first*
call to the outer subroutine; in this case, after the first call to the
outer subroutine is complete, the inner and outer subroutines will no
longer share a common value for the variable. In other words, the
variable will no longer be shared.
Furthermore, if the outer subroutine is anonymous and references a
lexical variable outside itself, then the outer and inner subroutines
will never share the given variable.
This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs that
reference variables in outer subroutines are called or referenced, they
are automatically rebound to the current values of such variables.
************
What I don't understand is when I delete my, then it works perfectly. I
copied the code from linkextor, but apparently it only works if I use globle
varible not my varibles. I think linkextor should change their sample codes.
Please update it, Larry.
MY QUESTION IS WHY INNER AND OUTTER ARE NOT SHAREING THE MY VARIBLE? I THINK
LARRY SHOULD CHANGE THE BEHAVOUR OF MY TOO.
#!/usr/bin/perl -w
use warnings;
use diagnostics;
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; #WHAT IS THE RETURN HERE FOR?
}
my $p = HTML::LinkExtor->new(\&callback);
$p->parse($input);
}
**************
output:
matchkey outsite callback img
matchkey inside callback img
matchkey inside callback img
matchkey outsite callback a
matchkey inside callback img
matchkey inside callback img
**************
I was told to change lines to those:
my $callback = sub { print "matchkey in $matchkey\n"; }; #WHY NO MORE
RETURN?
my $p = HTML::LinkExtor->new($callback);
*************
Here is the explain from perl:
(W closure) An inner (nested) named subroutine is referencing a
lexical variable defined in an outer subroutine.
When the inner subroutine is called, it will probably see the value of
the outer subroutine's variable as it was before and during the *first*
call to the outer subroutine; in this case, after the first call to the
outer subroutine is complete, the inner and outer subroutines will no
longer share a common value for the variable. In other words, the
variable will no longer be shared.
Furthermore, if the outer subroutine is anonymous and references a
lexical variable outside itself, then the outer and inner subroutines
will never share the given variable.
This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs that
reference variables in outer subroutines are called or referenced, they
are automatically rebound to the current values of such variables.
************
What I don't understand is when I delete my, then it works perfectly. I
copied the code from linkextor, but apparently it only works if I use globle
varible not my varibles. I think linkextor should change their sample codes.
Please update it, Larry.
MY QUESTION IS WHY INNER AND OUTTER ARE NOT SHAREING THE MY VARIBLE? I THINK
LARRY SHOULD CHANGE THE BEHAVOUR OF MY TOO.