RMGir has asked for the wisdom of the Perl Monks concerning the following question:

Trick question. What do you think the following prints?
perl -w -e'; @a=(split / /,"1 2 3 4 5")[1,2,1]; print "{",(join ",",@a +),"}\n";'
If you answered "{2,3,2}, of course", you'd be right, but only if you're using 5.8.0 or better.

Under 5.6.1, that prints "{2,3,}".

It behaves as if the list resulting from the split is some strange heisenbergian construct, where looking at a value destroys it.

Using 1,2,3 as an index list works, but re-using any index returns undef for the 2nd time it appears.

It IS fixed in 5.8.0, but I thought I should warn any fellow monks who might still be in 5.6.1 of this oddity...

EDIT: The behaviour isn't limited to split, either. This has the same problem:

perl -e'$x="1 2 3 4 5"; @a=($x=~/\d+/g)[1,2,1]; print "{",(join ",",@a +),"}\n";'

--
Mike

Replies are listed 'Best First'.
Re: Interesting (split) oddity with 5.6.1, fixed in 5.8.0
by BrowserUk (Patriarch) on Oct 17, 2003 at 20:02 UTC

    That's a real complicated set of factors required to caused the bug manifest.

    The simplest I've been able to reduce it to is

    sub list{ ( '1', '2', '3', '4', '5' ) } print @a=( list() )[0,0];

    From what I can work out, the following circumstances are required

    1. The list slice has to try to recover the same element twice.
    2. The result of the list slice has to be assigned to an array.
    3. The list has to be the result of a function --built-in or user.
    4. The list has to be a list of strings rather than numbers!! Ie. (1,2,3,4,5) won't cause it, but ('1','2','3','4','5') will, if the other circumstances are present.

    Removing any one of these factors and the bug doesn't occur.

    sub list{ ( 1, 2, 3, 4, 5 ) } print @a=( list() )[0,0]; 11 sub list{ ( '1', '2', '3', '4', '5' ) } print +(l())[0,0] 11 sub list{ ( '1', '2', '3', '4', '5' ) } print @a=( list() )[0,1]; 12 print @a=( list() )[1,0]; 21 sub list{ ( '1', '2', '3', '4', '5' ) } print @a=( list() )[0,0]; 1 Use of uninitialized value in print ...

    It would be interesting to know if this was fixed accidently for 5.8.0, or whether someone sat down and tracked down the cause from the symptoms. If it was the latter, that would have been one educational debugging session to have watched.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re: Interesting (split) oddity with 5.6.1, fixed in 5.8.0
by Nkuvu (Priest) on Oct 17, 2003 at 18:57 UTC
    This is interesting, but it doesn't look like a split oddity. It looks like a slice oddity. And I can't find any documentation in my brief perusal through the Camel book that shows whether this is defined behavior or not.
      Sort of. It's a slice on temporary lists oddity, I think. That's what I was trying to indicate with (split). It also applies to (//g), for instance.

      Oddly, it doesn't affect "real" slices like @a[1,2,1] or (localtime)[1,2,1], or even slicing map or grep results.

      I've searched through my local p5p archive, and haven't managed to find who first spotted or fixed this. They deserve major congratulations for sorting out something this odd...
      --
      Mike

Re: Interesting (split) oddity with 5.6.1, fixed in 5.8.0
by kleucht (Beadle) on Oct 21, 2003 at 01:08 UTC
    "strange heisenbergian construct"

    Are we geeks, or what?!?!?
    :-)