in reply to foreach funny business

Consider
#1 sub mychop(\$) { chop ${shift()} }
versus
# 2 sub mychop(\$) { chop ${$_[0]} }
As perl v5.8.8, the program dies with mychop version #2, but not with version #1.

${shift()} copies (via shift()) $_[0] into a ref-thingy, which is then dereferenced with $ - a behaviour which I deem correct. I would take a die() with #1 as a bug. At version #2 the die() is ok - the reference $_[0] is wrapped into a reference and dereferenced - still $_[0], thus read-only.

mhm.. really? oh, wait...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: foreach funny business
by krunchky (Sexton) on Jun 29, 2006 at 20:13 UTC
    Running your #2 on my perl does not die, and mine claims to be 5.8.8. Are you sure you're on your rocker? :-)

    Anyway, to my way of thinking the shift() shouldn't be giving you anything other than _exactly_ what was in $_[0], not some copy.

    Now who's with me? /me hoists pitchfork and torch
      I'm with you, you're right, I'm not. And then, again you're right - 5.8.8 doesn't die, I copied from the wrong shell, and shift() should just pass $_[0], not a copy, unless somebody made shift ro-aware. Have to look after this...

      Mhm. Scanning perldelta... nothing. Then, it's a bug. Or it's just undefined behaviour?

      But hey, it's a clever one. It's like, say, putting your hand in the band-saw, and get a finger cut, but then you draw out your hand and it's ok because it's read-only. One finger extra ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}