Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Anonymous Monk
on Dec 10, 2007 at 22:00 UTC ( [id://656245]=note: print w/replies, xml ) Need Help??


in reply to Re: Generic accessor for inside-out objects...
in thread Generic accessor for inside-out objects...

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.

Replies are listed 'Best First'.
Re^3: Generic accessor for inside-out objects...
by Joost (Canon) on Dec 10, 2007 at 22:15 UTC
    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://656245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found