Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by davido (Cardinal)
on Jul 18, 2004 at 03:57 UTC ( [id://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

Log In?
Username:
Password:

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

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

    No recent polls found