Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is buffer modified?

Status
Not open for further replies.

FishingIwishIwas

Technical User
Jun 16, 2006
5
US
Can someone explain why/how the array @buf is modified in the following simple program. I thought only the variable $item is supposed to be modified.

my @buf = ("a1\n", "a2\n");
print @buf;
foreach my $item (@buf) {
chop $item;
}
print @buf;

#the result is
a1
a2
a1a1
 
Your "$item" becomes a reference (more like a synonym or dereferenced reference) to each element of the list (or in your case the array). Consequently, altering "$item" in the foreach loop actually alters your array.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top