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

Re: Question regarding variable scope

by chimni (Pilgrim)
on Oct 16, 2003 at 06:49 UTC ( #299683=note: print w/replies, xml ) Need Help??

in reply to Question regarding variable scope

When you declare my $black_player at the start of the sub .That variable is global through the scope of the sub .
(My or our would behave the same with respect to this sub,behaviour would vary with respect to code followinng down the line).
When you do a foreach $black_player (@possible_opponents)
This effectively overrides the above $black_player .It is local to the loop.Same principle as a pass by value in C.
Therefore ,you can rename your looping variable or just do what roger has done ,use $_.

Replies are listed 'Best First'.
Re: Re: Question regarding variable scope
by MarkM (Curate) on Oct 17, 2003 at 05:33 UTC

    Actually, my() and our() do not behave the same from a subroutine. Both define a lexical context for the variable, however, my() allocates a new variable each time it is evaluated, while our() reuses a global.

    For recursive subroutines, or subroutines that are accessed at the same time from multiple threads, my() variables are safe, while our() variables are unsafe.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://299683]
[Lady_Aleena]: Why does grep(/.*$in.*/, @my_modules) work but grep { $_ =~ /.*$in.*/; $_ } @my_modules not work? What did I do wrong in the second?
[tobyink]: Why are you returning $_ n your grep block? You should return a boolean.
[tobyink]: *in your grep block

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-05-27 07:35 GMT
Find Nodes?
    Voting Booth?