[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: $_ global



Original:

$_ = "Am I global?";
print "$_\n";
@marbles = ('red','orange','yellow','green','blue','indigo','violet');
foreach (@marbles) {print "$_\n";}
print "$_\n";


New:

$_ = "Am I global?";
print "$_\n";
@marbles = ('red','orange','yellow','green','blue','indigo','violet');
for( $i=0; $i<=$#marbles; $i++ ){print "$marbles[$i] = $_\n";}
print "$_\n";

This is a more accurate way of testing this.  The Perl book
clearly states that the value of $_ is replaced in loops
such as the one you had.  If it didn't then you would not be able to take advantage of the smaller, more
compact, set of commands.  Such as:

foreach( @marbles ){
	@theLine = split( /e/ );
	}

or

foreach( @marbles ){
	$lastLetter = chop;
	print $lastLetter, "\n";
	}