Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Our, use vars, and magic, oh my!

by OzzyOsbourne (Chaplain)
on Aug 23, 2001 at 22:10 UTC ( #107438=note: print w/ replies, xml ) Need Help??


in reply to Re: Our, use vars, and magic, oh my!
in thread Our, use vars, and magic, oh my!

An excellent explanation, but if you look at the example that I provided, what is the answer to question 1? The only post that seems to answer the question is dga's. Is my and our the same in my example?

-OzzyOsbourne


Comment on Re: Re: Our, use vars, and magic, oh my!
Select or Download Code
Re: Re: Re: Our, use vars, and magic, oh my!
by chipmunk (Parson) on Aug 23, 2001 at 23:16 UTC
    You seem to be looking for an answer that says, "You absolutely should use my..." or "You absolutely should use our..." to declare variables at the top of the script. Well, there really isn't a definite answer like that. It partly depends on how you're using the variables, and partly on personal preference.

    If you are only using the variables from within that file, then you can declare with my. But you can still declare with use vars or our as well. It's really up to you. The script you're asking about should work the same whichever way you declare %TOC.

    If you are accessing the variables from another file, then you have to declare with use vars or our. They are mostly the same when used at the top of the file, but our has the unusual behavior of crossing package boundaries.

    Personally, I still do most of my coding for perl5.005, so I don't use our. I generally use my, except when I'm declaring packing variables or working with mod_perl.

Re: Re: Re: Our, use vars, and magic, oh my!
by danger (Priest) on Aug 24, 2001 at 02:25 UTC

    This is, in many ways, a followup to chipmunk's excellent post, but with a slight twist in that I do not consider our() or 'use vars' as ways of declaring global variables (there is a subtle but important distinction to be made). OzzyOsbourne asks:

    1.Is there any difference between declaring variables at the beginning of scripts with my or our? Won't their scope be the same?

    Yes, there is a difference, and the difference is that No, the scopes of the *variables* will not be the same --- one will be a lexical variable (my) and the other will be a package-global variable (our). I think one major problem people have with my(), our(), and 'use vars' is that we all tend to discuss them as ways of declaring 'variables', and then confusion sets in because the scope of what we declared doesn't always coincide with the scope of the variable which doesn't seem to make sense. Do not think of our() and 'use vars' as ways of declaring *variables* and things become clearer.

    Lexical variables *are* declared and created with the my() declaration. Package variables are never really "declared" at all ... what is really being declared with either 'use vars' or our() is not the variable per se, but "unqualified access" to a package variable under the 'strict' pragma.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-07-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (137 votes), past polls