Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Class::InsideOut - yet another riff on inside out objects.

by diotalevi (Canon)
on Dec 19, 2002 at 06:03 UTC ( #221052=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Class::InsideOut - yet another riff on inside out objects.
in thread Class::InsideOut - yet another riff on inside out objects.

If you need to go hunting for lexicals then you start with PadWalker. If the lexical you are looking for isn't immediately visible then you might also use Devel::Caller to get the code references for other places in the calling stack. You'd then use PadWalker on those references to check for otherwise inaccessible lexicals. Eventually every lexical exists in some scope that itself is visible either from the symbol table or via the calling stack.


Fun Fun Fun in the Fluffy Chair


Comment on Re^3: Class::InsideOut - yet another riff on inside out objects.
Re^4: Class::InsideOut - yet another riff on inside out objects.
by adrianh (Chancellor) on Dec 19, 2002 at 10:52 UTC

    As I understand it the %hash isn't actually in scope when the ATTR handler is called, so there is no way to get at it with PadWalker...

    Demonstration.

    #! /usr/bin/perl use strict; use warnings; package Foo; use Attribute::Handlers; use PadWalker qw(peek_my); use Data::Dumper; sub Field : ATTR(HASH) { my ($class, $symbol, $hash) = @_; if ($symbol eq 'LEXICAL') { my $level=0; while (eval {peek_my($level+1)} && !$@) {$level++}; print Dumper(peek_my($level)); } else { print "got %", *{$symbol}{NAME}, " from symbol table\n"; }; }; package FooBar; use base qw(Foo); my %just_to_prove_we_are_in_the_right_scope; print "about to call ATTR with \\%foo\n"; my %foo : Field; print "about to call ATTR with \\%bar\n"; my %bar : Field;

    produces

    about to call ATTR with \%foo $VAR1 = { '%just_to_prove_we_are_in_the_right_scope' => {} }; about to call ATTR with \%bar $VAR1 = { '%just_to_prove_we_are_in_the_right_scope' => {}, '%foo' => {} };

    Unless I'm missing something (entirely possible) I don't see how Devel::Caller can help here? There isn't a way that (in the above example) you can get at the name 'foo' at the time the %foo hash reference is passed to the ATTR handler.

      Oh ok. I was expressing more of a generalized ability so I might combine that with a search of all the contexts I could find on the symbol table as well. Very slow, not the right approach but still doable. So that'd just have to go all around and collect the pads from every visible code reference and I'm thinking that eventually you hit the right one...


      Fun Fun Fun in the Fluffy Chair

        Darn. I hoped there was something obvious I was missing :-)

        While scanning all coderefs & symbol table entries might work for a lot of cases, I don't think it is completely general. For example, consider:

        { package Foo; use base qw(Class::InsideOut); sub new { return bless [], shift }; { my %foo : Field }; };

        There are no subroutines that have %foo in their scope so, without generating code with a source filter, I can see no way of getting access to the pad with "%foo" in it.

      Hmm...
      $VAR1 = { '%bar' => {}, '%just_to_prove_we_are_in_the_right_scope' => {}, '%foo' => {} }; $VAR1 = { '%bar' => {}, '%just_to_prove_we_are_in_the_right_scope' => {}, '%foo' => {} }; about to call ATTR with \%foo about to call ATTR with \%bar
      Perl 5.6.1, PadWalker 0.08, Attribute::Handlers 0.78

      Makeshifts last the longest.

        Hmmm... I'm getting my behaviour under perl = 5.008 , Attribute::Handlers = 0.78 , PadWalker = 0.08 ,

        Off to go look at the module dependencies. I'll be so happy if I can make it work without that source filter :-)

        Ah ha! - from perldelta for perl 5.8.0

        The "my EXPR : ATTRS" syntax now applies variable attributes at run-time. (Subroutine and "our" variables still get attributes applied at compile-time.) See attributes for additional details. In particular, how- ever, this allows variable attributes to be useful for "tie" interfaces, which was a deficiency of earlier releases.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-10-25 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (139 votes), past polls