mod_perl: "our" vs. "my" in CGI scripts
Max Kanat-Alexander
mkanat at bugzilla.org
Thu Jun 22 12:27:21 UTC 2006
Hey everybody. Here's another important thing to know for mod_perl. In
a CGI, the following code doesn't work:
my $var = 0;
sub foo {print $var;}
The following code *does* work:
our $var = 0;
sub foo {print $var;}
That's because "my" isn't really supposed to make variables available
to subroutines, but it works normally because of some strangeness in
normal perl.
If you have an "our" outside of a function, though, you always have to
initialize the variable to something. That is, don't do "our $var",
always do "our $var = 'something'". If you don't have anything to
initialize it to, you can do "local our $var;" -- that works fine.
-Max
--
http://www.everythingsolved.com/
Competent, Friendly Bugzilla Services. And Everything Else, too.
More information about the developers
mailing list