scope subroutine argument with "our"

P

pacmac

Hello All,

I'm trying to understand someone else's code. It's a mod_perl
PerlAccessHandler.

I'm not sure what's accomplished by "our" scoping an already-our-
scoped variable when passed as an argument to a subroutine. See the
way check_pwd() is called below. Does doing this make a copy? I don't
get it.

Can someone please enlighten me?
PM

package myControl;

use strict;

use [typical Apache2 modules];

our $PWD_SECRET = '128XYZ345';

sub handler {
my $r = shift;
...
my ($stat, $msg) = check_pwd($r, our $PWD_SECRET);
...
}

sub check_pwd {
my $r = shift;
my $secret = shift;

... do stuff ...
return (1, 'OK');
}
 
U

Uri Guttman

p> I'm not sure what's accomplished by "our" scoping an already-our-
p> scoped variable when passed as an argument to a subroutine. See the
p> way check_pwd() is called below. Does doing this make a copy? I don't
p> get it.

p> package myControl;

p> our $PWD_SECRET = '128XYZ345';

p> sub handler {
p> my $r = shift;
p> ...
p> my ($stat, $msg) = check_pwd($r, our $PWD_SECRET);
p> ...
p> }

AFAIK it doesn't do anything special. the outer our is file scoped so
the inner one is redundant. both will refer to the same package
variable. the coder probably didn't understand the scoping rules of our
and thought our needed to be used in front of each use of that
variable. it only needs to be used the first time in a given scope.

uri
 
C

C.DeRykus

Hello All,

I'm trying to understand someone else's code. It's a mod_perl
PerlAccessHandler.

I'm not sure what's accomplished by "our" scoping an already-our-
scoped variable when passed as an argument to a subroutine. See the
way check_pwd() is called below. Does doing this make a copy? I don't
get it.

The second "our" will draw a warning too if 'use warnings' is
in effect:

"our" variable $PWD_SECRET redeclared at ....
 
U

Uri Guttman

CD> The second "our" will draw a warning too if 'use warnings' is
CD> in effect:

you caught something there i missed but i wouldn't redeclare anyhow. if
both declarations were in the same namespace they would refer to the
same variable so the code still works. i bet since it is under mod_perl
the warnings (if ever seen) are tucked away in some log and only happen
at startup anyway. still a bad coding thing.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top