Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

Monks,

I'm finding the uncommon error Variable "%data" is not available when using the following module:

package Foo; use warnings; use strict; my %data = qw/foo bar/; our $AUTOLOAD; sub AUTOLOAD { my ($self, @args) = @_; my ($pkg, $field) = (__PACKAGE__, $AUTOLOAD); $field =~ s/${pkg}:://; ## uncommenting any of these, makes the code work #%data; #() = %data; #map $_, %data; #map $_, \%data; #$::qwerty123 = %data; eval "*$field = sub { return \\%$field; };"; $@ and die "eval failed: $@\n"; goto &$field; } 1;
$ perl -I. -MData::Dumper -MFoo -e'print Dumper (Foo->data)' Variable "%data" is not available at (eval 1) line 1. $VAR1 = {};

As a workaround, it's enough to uncomment any of the commented lines (although the first of them triggers the expected warning):

$ perl -I. -MData::Dumper -MFoo -e'print Dumper (Foo->data)' Useless use of private hash in void context at Foo.pm line 16. $VAR1 = { 'foo' => 'bar' };

At this point I checked perldiag, which says: "This can happen for one of two reasons. First, the outer lexical may be declared in an outer anonymous subroutine that has not yet been created. [...] The second situation is caused by an eval accessing a variable that has gone out of scope".

To me, this scenario isn't covered by either of the given explanations. I could be led to believe that the first of the reasons explains it, although then I'd like to be educated on how this module compares to an anonymous subroutine regarding the compilation-time vs run-time issues that are detailed in the documentation and result in this error.

Just for fun, I appended the following at the bottom of Foo.pm:

package main; use warnings; use strict; use Data::Dumper; print Dumper (Foo->data);

Then, running perl Foo.pm works fine even with all those 5 lines mentioned above commented out.

--
 David Serrano
 (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling errors. Thank you!).


In reply to 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 chilling in the Monastery: (5)
    As of 2020-07-07 13:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?