To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Sent: 2/4/02 11:12 PM
Subject: Why localize $_, was-> Re: why shift @_ ?
Bompa wrote:
"I wrote a script to demonstrate how the value of $_ could get changed
unexpectedly. (It took me an hour, but I learned from it, heh).
use strict;
our @list = qw
$var = 42;
dosomething($var);
sub dosomething {
local ($var) = @_;
&dosomethingelse;
print "\$var is now $var\n";
}
sub dosomethingelse {
$var++;
}
Using local, you will print 43. If you change local to my, you will get
42.
local makes a variabl
Bompa wrote:
"I wrote a script to demonstrate how the value of $_ could get changed
unexpectedly. (It took me an hour, but I learned from it, heh).
use strict;
our @list = qw(a b c d);
foreach (@list) {
&check_b;
print $_, "\n";
}
sub check_b {
foreach (@list) {
#local $_;
$
Steve Howard wrote:
>
>
> However, since the whole idea behind using local or my with variables
> is to prevent a variable name from writing over the same variable
> name elsewhere in the script, it makes even *more* sense to me to
> use local with Perl's special variables; in that they are used
h populated entirely differently.
The last paragraph like I said, is probably not something for the beginners
board, but my point is that there is more than one reason to localize.
Steve H.
-Original Message-
From: Dave Benware [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 04, 2002 9:0
Timothy Johnson wrote:
>
> Okay, that makes sense after playing around with it a little. One more
> question. Does that offer an advantage over doing this?
>
> use strict;
> our @list = qw(a b c d);
> foreach (@list) {
> &check_b;
> print $_, "\n";
> }
>
> sub check_b {
> foreach (@list
stead of local $_
$string =~ s/b/bb/;
# SAVE TO FILE
}
}
-Original Message-
From: Dave Benware [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 04, 2002 7:00 PM
To: Beginners perl
Subject: Why localize $_, was-> Re: why shift @_ ?
Timothy Johnson wrote:
>
> Ok, I f
Timothy Johnson wrote:
>
> Ok, I figured that much, but I guess my question is this: Is there a
> pressing need to scope a predefined variable like $_? How could this
> adversely affect a program? (I'm not trying to be a smart aleck, I really
> want to know)
>
Hi,
Sorry for such a short ans