http://qs321.pair.com?node_id=375326


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