Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: HTML::LinkExtor weirdness (scoping closures nested named subs)

by Anonymous Monk
on Nov 06, 2013 at 21:26 UTC ( #1061485=note: print w/replies, xml ) Need Help??

in reply to HTML::LinkExtor weirdness

Off the top of my head :)

It should work the way you expect if you change to

my $callback = sub { ... }; HTML::LinkExtor->new($callback);

FWIW, you probably got some warnings about this (will not stay shared)

Re: Entry Edit Refresh (nested subs and closures)

  • Comment on Re: HTML::LinkExtor weirdness (scoping closures nested named subs)
  • Download Code

Replies are listed 'Best First'.
Re^2: HTML::LinkExtor weirdness (scoping closures nested named subs)
by skazat (Hermit) on Nov 06, 2013 at 22:33 UTC

    Indeed, that change doe make things work! Is there somewhere I an further read to understand where I went wrong?

    Infinitely grateful for your help - that darn thing was giving me public fits ;)


      The error message Variable "%s" will not stay shared is described in perldiag. The description is brief but explains the behavior you experienced. You can also read about closure in perlref, perlsub and elsewhere.

      This is why you don't nest named subs, the closure becomes "static"

      perl -E " sub f{ my %g; sub g{say \%g; } g(); say \%g; \%g } say f(),$/ for 1 .. 4;"

      perl -E ' sub f{ my %g; sub g{say \%g; } g(); say \%g; \%g } say f(),$/ for 1 .. 4; '

      HASH(0xabd9c4) HASH(0xabd9c4) HASH(0xabd9c4) HASH(0xabd9c4) HASH(0x3f9b6c) HASH(0x3f9b6c) HASH(0xabd9c4) HASH(0x3f9b8c) HASH(0x3f9b8c) HASH(0xabd9c4) HASH(0x3f9b6c) HASH(0x3f9b6c)

      By making the nested subroutine a variable (also known as an anonymous sub), the closure is dynamic

      $ perl -lE " sub f{ my %g; my $g = sub {say \%g; $g{1}++}; $g->(); say \%g; \%g } say f(),$/ for 1 .. 4;"

      HASH(0xabd9d4) HASH(0xabd9d4) HASH(0xabd9d4) HASH(0x3f9b1c) HASH(0x3f9b1c) HASH(0x3f9b1c) HASH(0x99b374) HASH(0x99b374) HASH(0x99b374) HASH(0x99b344) HASH(0x99b344) HASH(0x99b344)
      I linked it already, just follow the linked links :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061485]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2016-10-23 10:57 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (301 votes). Check out past polls.