Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: OUR declaration

by sgifford (Prior)
on Sep 23, 2006 at 17:10 UTC ( [id://574529]=note: print w/replies, xml ) Need Help??


in reply to OUR declaration

I usually use our in preference to use vars because it allows a declaration and assignment in one go:

our $globalvar = 10;

For global variables, I usually use our in preference to my because it doesn't create closures within subs, which can cause problems under mod_perl.

Replies are listed 'Best First'.
Re^2: OUR declaration
by nobull (Friar) on Sep 24, 2006 at 13:03 UTC
    When using our as an alternative to my in pseudo-CGI scripts that'll be running under mod_perl I recommend getting into the habit of using local our so that you get a pseudo "global destruction" at the end of the pseudo-CGI script.

    Otherwise, if the declaration doesn't initialise, the value from a previous execution of the script may persist and also if there are any destruction side effects (like closing files) these may get deferred.

      I don't reccommend using our "as an alternative to my" under mod_perl. They don't do the same thing, and mod_perl doesn't change that. If you are using "our" because your code has unintentional closures around "my" variables that showed up when you used mod_perl, you should really change your code to fix this problem. It can bite you in other ways, and "local our" is a pretty nasty construct.
        The mod_perl Reference recommends exactly that.

        While there are differences between using my and our for global variables, in my experience they don't come into play in most scripts unless they're run under something which turns all your subroutines into inner subroutine, like mod_perl. That's why I suggested using our for global variables, since it works the same in both scenarios.

        My understanding is the our was a right and proper way to declare package globals.

        Are there problems you're aware of with using our for package globals under mod_perl or anywhere else?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found