Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: scope and declaration in local packages

by Anonyrnous Monk (Hermit)
on Jan 29, 2011 at 18:06 UTC ( #885048=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-05-21 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?