in reply to foreach funny business

A bug in your version of perl, perhaps? On mine, it dies:

sidhekin@blackbox:~$ perl -v This is perl, v5.8.7 built for i486-linux Copyright 1987-2005, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. sidhekin@blackbox:~$ perl sub mychop(\$) { chop ${shift()} } foreach my $thing (qw/foo bar baz/) { print "$thing -> "; my $c = mychop $thing; print " $thing,$c\n"; } __END__ Modification of a read-only value attempted at - line 1. foo ->

Update: After some trial-and-error, it seems to be the case that, on threaded builds, trying to take a reference to an alias of a constant string creates a copy of that constant string and results in a reference to that copy, while on non-threaded builds, you get a reference to the constant itself.

# v5.8.3 built for i386-linux-thread-multi: -bash-2.05b$ perl -le 'print map{chop$$_, $_}\$_, \$_, \$_ for "abc", +($x="abc")' cSCALAR(0x804cbb8)cSCALAR(0x804cc78)cSCALAR(0x804c99c) cSCALAR(0x805f69c)bSCALAR(0x805f69c)aSCALAR(0x805f69c) # v5.8.7 built for i486-linux: sidhekin@blackbox:~$ perl -le 'print map{chop$$_, $_}\$_, \$_, \$_ for + "abc", ($x="abc")' Modification of a read-only value attempted at -e line 1. Attempt to free unreferenced scalar: SV 0x812ec2c.

Whether this is a bug or undefined behaviour, though ... I'll leave that for others to decide.

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
Re^2: foreach funny business
by demerphq (Chancellor) on Jun 29, 2006 at 20:48 UTC

    Bug or undefined behaviour? FWIW ive seen this issue with DDS in all kinds of places. It seems to be OS and version dependent. The question comes down to whether shift returns an alias to the item shifted out or if it returns a copy. Similar is whether \\$foo returns a reference to a readonly value or not.

    Personally i think that the "reasonable" behaviour is to return a copy as that is the most DWIM behaviour.

    ---
    $world=~s/war/peace/g

Re^2: foreach funny business
by krunchky (Sexton) on Jun 29, 2006 at 19:39 UTC
    Interesting. I'm running debian unstable, which has this perl right now:
    rob@toblerone:~$ perl -v This is perl, v5.8.8 built for i486-linux-gnu-thread-multi Copyright 1987-2006, Larry Wall etc, etc....