M
Michael Carman
In a separate thread someone recently asked what happens if they modify
the variable in a 'while ($var =~ /pattern/g)' loop. In crafting a
sample program I noticed something that surprised me a little:
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'xyz' if $1 eq 'b';
print "$1\n";
}
__END__
a - a
b - y
x - x
y - y
z - z
In the second result, you can see that the value of $1 changes after
reassigning $s. Its value becomes the text from the new string at the
position corresponding to the match against the old one. This makes it
pretty clear that $1 is actually an alias instead of a copy but I can't
find this documented anywhere.
That made me wonder what would happen if the new string was shorter than
the match position in the old one. Consider
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'x' if $1 eq 'c';
print "$1\n";
}
__END__
a - a
b - b
c - c # <--
x - x
as well as:
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'xy' if $1 eq 'c';
print "$1\n";
}
__END__
a - a
b - b
c - # <--
x - x
y - y
If that doesn't scream "NUL terminated C string!" I don't know what does.
Is this documented anywhere, preferably with a caveat about using $1 and
kin after you've changed the match string?
-mjc
the variable in a 'while ($var =~ /pattern/g)' loop. In crafting a
sample program I noticed something that surprised me a little:
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'xyz' if $1 eq 'b';
print "$1\n";
}
__END__
a - a
b - y
x - x
y - y
z - z
In the second result, you can see that the value of $1 changes after
reassigning $s. Its value becomes the text from the new string at the
position corresponding to the match against the old one. This makes it
pretty clear that $1 is actually an alias instead of a copy but I can't
find this documented anywhere.
That made me wonder what would happen if the new string was shorter than
the match position in the old one. Consider
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'x' if $1 eq 'c';
print "$1\n";
}
__END__
a - a
b - b
c - c # <--
x - x
as well as:
my $s = 'abc';
while ($s =~ /(\w)/g) {
print "$1 - ";
$s = 'xy' if $1 eq 'c';
print "$1\n";
}
__END__
a - a
b - b
c - # <--
x - x
y - y
If that doesn't scream "NUL terminated C string!" I don't know what does.
Is this documented anywhere, preferably with a caveat about using $1 and
kin after you've changed the match string?
-mjc