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


in reply to scope and declaration in local packages

You're calling Animal::Hog->sound() too early for the variables to have been assigned a value.  Put the call after the block wherein you define the package, and things will work. Or use a BEGIN block.

The initialisation of the variables happens at runtime (execution of the block), while the setting up of the subroutines happens at compile-time.

(Also, in a real program, you'd probably want to call the constructor (new) to create an object instance — otherwise you might as well use regular non-OO subroutines (as opposed to methods)...)

Replies are listed 'Best First'.
Re^2: scope and declaration in local packages
by december (Pilgrim) on Jan 31, 2011 at 13:14 UTC

    The initialisation of the variables happens at runtime (execution of the block), while the setting up of the subroutines happens at compile-time.

    That explains my confusion as to why Perl doesn't complain about the functions but can't find the variables. Thanks!

    The reason I'm not calling new is that I would like to encapsulate a regex (and relevant condition) inside the object which should work without actually creating an instance. I just want to store that regex in the object for cleanliness, apart from any actual OO code.