scope subroutine argument with "our"

Discussion in 'Perl Misc' started by pacmac, Oct 1, 2009.

  1. pacmac

    pacmac Guest

    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');
    }
    pacmac, Oct 1, 2009
    #1
    1. Advertising

  2. pacmac

    Uri Guttman Guest

    >>>>> "p" == pacmac <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Oct 1, 2009
    #2
    1. Advertising

  3. pacmac

    C.DeRykus Guest

    On Oct 1, 12:36 pm, pacmac <> wrote:
    > 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 ....

    --
    Charles DeRykkus
    C.DeRykus, Oct 2, 2009
    #3
  4. pacmac

    Uri Guttman Guest

    >>>>> "CD" == C DeRykus <> writes:

    CD> On Oct 1, 12:36 pm, pacmac <> wrote:
    >> 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.


    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

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Oct 2, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    94
  2. GreenLeaf
    Replies:
    3
    Views:
    127
    Tad McClellan
    Feb 24, 2005
  3. king
    Replies:
    5
    Views:
    170
  4. Replies:
    0
    Views:
    152
  5. ccc31807

    passing argument to a subroutine

    ccc31807, Dec 3, 2009, in forum: Perl Misc
    Replies:
    10
    Views:
    186
Loading...

Share This Page