Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

by davido (Cardinal)
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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-01-19 09:33 GMT
Find Nodes?
    Voting Booth?