|Do you know where your variables are?|
scope and declaration in local packagesby december (Pilgrim)
|on Jan 29, 2011 at 17:41 UTC||Need Help??|
december has asked for the
wisdom of the Perl Monks concerning the following question:
Hey fellow monks,
I have a question about scope in packages and I hope somebody smarter and with deeper knowledge of Perl internals can explain the resulting error messages to me.
Given the following simplified code:
... I get this result:
I am wondering why Perl knows about Animal::Hog->sound() without predeclaring it, yet returns an error for the lexical variables $sound1 and $sound2. It confuses me because I expect Perl to either have parsed the section and declared the variables and functions, or for it to not have done so and hence not know about any variables or functions in that block at all.
Thanks for any insight!
PS: I know I could move the package declaration above the invocation, use a BEGIN block or declare the objects in another file.