On Feb 4, Timothy Johnson said:
>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)
There sure is.
@n = qw( john jacob jingleheimer schmidt );
print "<@n>\n";
for (@n) { print "even: $_\n" if even(split //) }
print "<@n>\n";
sub even {
my $sum = 0;
while (@_) {
$_ = shift;
$sum += ord;
}
return $sum % 2 == 0;
}
Since $_ was not localized, changing it in even() changes $_ in the for
loop, and since $_ in the for loop is aliased to the elements of the
array... well... run the code to see what happens. :(
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]