Expert: What is wrong here?

Discussion in 'Perl Misc' started by Great Deals, Sep 24, 2003.

  1. Great Deals

    Great Deals Guest

    #!/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?
    Great Deals, Sep 24, 2003
    #1
    1. Advertising

  2. In article <>,
    Great Deals <> 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
    Gary E. Ansok, Sep 24, 2003
    #2
    1. Advertising

  3. Great Deals

    Jay Tilton Guest

    (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);
    }
    Jay Tilton, Sep 24, 2003
    #3
  4. Great Deals

    Tore Aursand Guest

    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 <>
    Tore Aursand, Sep 25, 2003
    #4
  5. Great Deals

    Great Deals Guest

    (Great Deals) wrote in message news:<>...
    > #!/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?
    Great Deals, Sep 25, 2003
    #5
  6. Great Deals

    Jay Tilton Guest

    (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.
    Jay Tilton, Sep 25, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cas

    only for expert: what's wrong with this code?

    Cas, Oct 25, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    714
  2. Cas

    only for expert: what's wrong with this code?

    Cas, Oct 25, 2006, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    155
  3. Tony Gilbert

    What am I doing wrong - expert question

    Tony Gilbert, Nov 21, 2003, in forum: Javascript
    Replies:
    0
    Views:
    100
    Tony Gilbert
    Nov 21, 2003
  4. Wow

    Anyone here is a real JS expert?

    Wow, Jul 24, 2004, in forum: Javascript
    Replies:
    5
    Views:
    68
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
  5. QA
    Replies:
    4
    Views:
    111
    Randy Webb
    Sep 11, 2004
Loading...

Share This Page