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

Re^3: OUR declaration

by perrin (Chancellor)
on Sep 25, 2006 at 03:57 UTC ( [id://574655]=note: print w/replies, xml ) Need Help??


in reply to Re^2: OUR declaration
in thread OUR declaration

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.

Replies are listed 'Best First'.
Re^4: OUR declaration
by sgifford (Prior) on Sep 25, 2006 at 05:54 UTC
    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?

      I'll bring this up on the mod_perl dev list. I don't think we should advise people to use such a crazy construct.

      The problems people encounter with ModPerl::Registry are usually not caused by inner subroutines, but rather by closures. The closures were always there, but were never noticed before in a non-persistent environment. Consider this:

      my $q = CGI->new; show_name(); sub show_name { print $q->param('name'); }

      This code will break when run in mod_perl because it forms a closure around $q, making it a private variable for the show_name() sub after compilation.

      This problem can be fixed by making $q an our variable, but that's a poor programming practice, giving $q a large scope and persistence that it should not have. The correct fix is to pass the variables that the sub requires:

      my $q = CGI->new; show_name($q); sub show_name { my $q = shift; print $q->param('name'); }
        Replacing my $q with local our $q would give $q neither a larger scope nor a greater persistance, and would also solve the problem.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found