Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Diff b/w mods for plugging variables into symbol table:'use var' and 'our'

by cool (Scribe)
on May 16, 2006 at 17:03 UTC ( #549818=perlquestion: print w/replies, xml ) Need Help??

cool has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I am referring to the book, 'Professional Perl Progamming' by Peter Wainwright and have a doubt on page 260..

for the explanation of use var, it says..

Varivables declared with use vars are plugged into the symbol table for the current package at compile time, because use takes effect at compile time

and then in next page the explanation for our goes like...

..our works like use cars inthat it adds a new entry to the symbol table for the current package. However, unlike use vars it does this at compile time, not run time. This means that the cariable can be accessed without a package prefix from any package , butt only so long as its lexical scope exists. THis means that a package variable, declared with our at the top of the file, is global throughout the file even if it contains more than one package, just as we would normally expect a global vaiable to be.

Can any one help me to understand

1)about the diff in reading at compile time and run time. Mean to say, how this is making diff to the symbol table..

2)Ain't above two are saying same thing as that these are plugged at compile time only..

  • Comment on Diff b/w mods for plugging variables into symbol table:'use var' and 'our'

Replies are listed 'Best First'.
Re: Diff b/w mods for plugging variables into symbol table:'use var' and 'our'
by NetWallah (Canon) on May 16, 2006 at 17:40 UTC
    From perldoc vars
    NOTE: For variables in the current package, the functionality prov +ided by this pragma has been superseded by "our" declarations, availabl +e in Perl v5.6.0 or later. See "our" in perlfunc.
    So , to answer your question - they provide very similar functionality, but please use "our".

    One other note - "our" is lexically scoped whereas "vars" was package scoped.

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Re: Diff b/w mods for plugging variables into symbol table:'use var' and 'our'
by blazar (Canon) on May 17, 2006 at 10:35 UTC
    • about the diff in reading at compile time and run time. Mean to say, how this is making diff to the symbol table..
    • Ain't above two are saying same thing as that these are plugged at compile time only

    For all practical purposes there's no difference between the vars pragma and our, and with modern enough perls I'd recommend using the latter. But just keep in mind that most often you will just want to use lexical variables declared with my. It's nice that you want to learn more about Perl, but be patient and wait until you will really need our. I say so because I've seen some scripts using it every here and there, fundamentally for no real goo reason. I'm not saying this is your case, and I hope it is not. I'm just warning you...

    Also check difference between my and local: despite the title, it also contains some discussion about our.

    2006-05-17 Retitled by holli, as per Monastery guidelines
    Original title: 'Re: Entry to symbol table??'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://549818]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-02-28 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (127 votes). Check out past polls.

    Notices?