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

Re^4: Lexical pad / attribute confusion

by adrianh (Chancellor)
on Dec 22, 2002 at 00:08 UTC ( [id://221689]=note: print w/replies, xml ) Need Help??


in reply to Re: Re^2: Lexical pad / attribute confusion
in thread Lexical pad / attribute confusion

They're also declared after INIT, and you can see them from there ;-) Also, moving dump_lex after the declarations like this:

#! /usr/bin/perl use strict; use warnings; package AttrTest; use Data::Dumper; use PadWalker qw(peek_my); sub MODIFY_HASH_ATTRIBUTES { my ($package, $reference, @attributes) = @_; $package->dump_lex("setting @attributes in $package for $reference" +); return; }; INIT { AttrTest->dump_lex('init') }; { package Foo; use base qw(AttrTest); my %foo : Attr = (one => 1); }; { package Bar; use base qw(AttrTest); use attributes (); my %bar; attributes::->import(__PACKAGE__, \%bar, 'Attr'); } AttrTest->dump_lex('runtime'); sub dump_lex { my ($class, $when) = @_; print "when $when top-level pad is\n"; my $level=0; while (eval {peek_my(++$level)} && !$@) {}; my $hash = peek_my($level-1); while (my ($name, $value) = each %$hash) { my $dumped = Data::Dumper->new([\$value],[$name])->Indent(0)->D +ump; print "\t$value -> $dumped\n"; }; print "\n"; };

Still gives you this (under 5.8):

when init top-level pad is HASH(0x22e150) -> $%bar = \{}; HASH(0xb5b50) -> $%foo = \{}; when setting Attr in Foo for HASH(0xb5b50) top-level pad is when setting Attr in Bar for HASH(0x22e150) top-level pad is HASH(0x22e150) -> $%bar = \{}; when runtime top-level pad is

Replies are listed 'Best First'.
Re: Re^4: Lexical pad / attribute confusion
by shotgunefx (Parson) on Dec 22, 2002 at 01:04 UTC
    Actually I get different results. On 5.6.1 (win32) lexicals declared after INIT are not visible from INIT.
    #!/usr/bin/perl use warnings; use strict; INIT { use Data::Dumper; use PadWalker qw (peek_my peek_sub); print Dumper(peek_my(0)); } my $foo = 'lee'; my $bar = "Bar"; { my @foo = (1..3); } { my @bar = (9..12); } print Dumper(peek_my(0)) __END___ $VAR1 = {}; $VAR1 = { '$foo '$bar };
    my has both runtime and compile time effects, I wouldn't be suprised if they are on the same pad, but are removed from the padlist when the block is exited.

    -Lee

    "To be civilized is to deny one's nature."
      Actually I get different results. On 5.6.1 (win32) lexicals declared after INIT are not visible from INIT.

      My results on 5.8 match yours if I run your code. Not surprising since your code is different from mine :-)

      dump_lex climbs up the callstack to the enclosing file lexical scope. So it would be the same sort of thing as doing this:

      use warnings; use strict; INIT { use Data::Dumper; use PadWalker qw (peek_my peek_sub); print Dumper(peek_my(1)); } my $foo = 'lee'; my $bar = "Bar"; print Dumper(peek_my(0)); { my @foo = (1..3) }; { my @bar = (9..12) }; print Dumper(peek_my(0));

      Which gives (on 5.8)

      $VAR1 = { '$foo' => \undef, '@bar' => [], '@foo' => [], '$bar' => \undef }; $VAR1 = { '$foo' => \'lee', '$bar' => \'Bar' }; $VAR1 = { '$foo' => \'lee', '$bar' => \'Bar' };

      Again, INIT can see everything, but nothing can be seen before/after the blocks are executed at the top level. I still don't understand what's happening here :-)

        Which brings this doozie that I previously struggled with. Why can't peeker see @bar ?
        use warnings; use strict; INIT { use Data::Dumper; use PadWalker qw (peek_my peek_sub); print Dumper(peek_my(1)); } my $foo = 'lee'; my $bar = "Bar"; sub peeker{ print Dumper(peek_my(shift)); } print Dumper(peek_my(0)); { my @foo = (1..3) } { my @bar = (9..12); peeker(1); } print Dumper(peek_my(0)); __END__ $VAR1 = { '$foo' => \undef, '@foo' => [], '$bar' => \undef, '@bar' => [] }; $VAR1 = { '$foo' => \'lee', '$bar' => \'Bar' }; $VAR1 = { '$foo' => \'lee', '$bar' => \'Bar' }; $VAR1 = { '$foo' => \'lee', '$bar' => \'Bar' };
        I don't know enough to say for sure, but when INIT is called, the pad accesses are probably all figured out. Then after INIT, it does whatever magic it does to give the appearence of a scope to a bare block. I would think that even though they are unavailable to the outer scope, their position is retained in the pad list. Otherwise the location of a my var would change from before and after the bare block.

        -Lee

        "To be civilized is to deny one's nature."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found