Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Why is a hash the default "object" in oo perl?

by davido (Archbishop)
on Jul 18, 2004 at 03:57 UTC ( #375326=note: print w/ replies, xml ) Need Help??


in reply to Why is a hash the default "object" in oo perl?

As an aside ... I (again accidently) discovered that if you do:
{{ my $variable } sub accessor { .... }}
That the raw $variable is accessible from anywhere within the package its declared. I have NO idea why. If someone can explain I'd love it. However, anyone outside the package -- including a package that inherits it, can't get at $variable and must use the accessor. Not sure if theres any practical purpose for this yet.

Actually, your construct above creates a lexical $variable that immediately falls out of scope. Then the sub accesses (and autovivifies) a package global named $variable, but this is not the same entity as the lexical. From that point on, your package is dealing with the package global $variable, not the lexical (which is gone, destroyed, garbage-collected, etc).

You mention that it's not accessible outside the package (except by the accessor sub, of course). But that's not entirely true. If the package is named "mypackage", the package global $variable may be accessed from package main as: $mypackage::variable.

Using use strict; would alert you to this issue.


Dave


Comment on Re: Why is a hash the default "object" in oo perl?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-09-02 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls