Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

To me, this scenario isn't covered by either of the given explanations.

It's covered by the second one. %data has gone out of scope. The lexical block in which it resides (the file) was exited before AUTOLOAD was called.

A sub can extend the life of a lexical by becoming a closure and capturing the lexical. However, a sub only captures the variables it needs, meaning it only captures the lexicals it references. Other lexicals aren't captured.

If there's a reference to (i.e. a mention of) %data in AUTOLOAD (e.g. if you uncomment any of the commented statements), then the sub captures %data, and the eval will find %data.

If there are no references to %data in AUTOLOAD, then the sub doesn't capture %data, so it won't exist when eval is evaluated.

Adding «%data if 0;» to AUTOLOAD is enough to make it capture the lexical without warnings.

$ perl -wE'{ my $s=1; sub f { eval q{$s} } } say f || 0;' Variable "$s" is not available at (eval 1) line 2. 0 $ perl -wE'{ my $s=1; sub f { $s if 0; eval q{$s} } } say f || 0;' 1

Another option is to use a package variable since they don't go out of scope.

$ perl -wE'{ my $s=1; sub f { eval q{$s} } } say f || 0;' Variable "$s" is not available at (eval 1) line 2. 0 $ perl -wE'{ our $s=1; sub f { eval q{$s} } } say f || 0;' 1

Then, running perl Foo.pm works fine

I cringe whenever someone does

perl -c Foo.pm
instead of
perl -e'use Foo;'

because they're not equivalent. The crucial difference in this case is that you now call AUTOLOAD before the lexical scope of %data is exited.


In reply to Re: Variable "%data" is not available by ikegami
in thread Variable "%data" is not available by Hue-Bond

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2020-07-11 13:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?