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

Re: Generic accessor for inside-out objects...

by Joost (Canon)
on Dec 10, 2007 at 21:47 UTC ( [id://656241]=note: print w/replies, xml ) Need Help??


in reply to Generic accessor for inside-out objects...

Inside-out object's properties are generally represented as lexical variables. That means you cannot access those properties outside of their lexical (source code, nested block) environments. Not even using eval STRING.

  • Comment on Re: Generic accessor for inside-out objects...

Replies are listed 'Best First'.
Re^2: Generic accessor for inside-out objects...
by Anonymous Monk on Dec 10, 2007 at 22:00 UTC
    I don't understand the difference though, since the get method is in the scope of the object, and the string eval (or symbolic reference) is in the get method?

    It's not that I'm doubting or arguing with you, I just don't understand what nuance of the language I'm missing.

      Lexical variables are determined at compile time:
      # file Foo.pm use strict; package Foo.pm my $bee = 2; # lexical variable sub foo { return $bee++; }
      Assuming that's all in that file, Foo::foo() is the only subroutine with a handle on the lexical $bee. Note that foo() even keeps that variable available when $bee goes "out of scope". this is very important in understanding what's really going on. You'll want to do a search on "perl closures".

      That means there is no way to get at $bee except via Foo::foo(). Say:

      # some other file Bar.pm use strict; package Bar; sub bar { return $bee; }
      That fails because there is no $Bar::bee variable, but also, there is no way to specify you actually meant the $bee variable bound to the Foo::foo function.

      Note that package (or "global") variables can be accessed from other scopes:

      # file Foo.pm package Foo; our $blah = 2; # or use vars
      #file Bar.pm package Bar; print $Foo::blah;
        ...And the string eval is processed at runtime, so it's essentially in the main namespace? Wow. My brain hurts now, but I feel enlightened! Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-18 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found