http://qs321.pair.com?node_id=663995


in reply to regexp list return 5.6 vs 5.8

I'm still very curious about the weird [[] and {{} that hipowls was getting. Maybe it's worth a thread.

It would seem that I'm unable to locate the cause of the error people seem to experience testing my package on v5.6. If any of you are interested, it's Data::FeatureFactory on cpan, maybe you can have a look (make test). I'll try to find a 5.6 installation somewhere and play with it.

use strict; use warnings; print "Just Another Perl Hacker\n";

Replies are listed 'Best First'.
Re^2: regexp list return 5.6 vs 5.8
by almut (Canon) on Jan 24, 2008 at 15:06 UTC

    I get the same behaviour with 5.8.4, BTW. It seems that when the range 0..-1 is used to select the elements of the slice ($#rv is -1 when @rv is empty), the 'final element' of the slice (incorrectly) evaluates to the previous/last element on the Perl stack (or some such). With Perl versions up to at least 5.8.4, that is — but no longer with 5.8.8 and 5.10.0 (I currently don't have access to versions 5.8.5 - 5.8.7, so I can't tell when it got fixed).

    (Ranges like [99..98] behave the same way as [0..-1], so what seems to matter is just that the second value in the range is smaller than the first...)

    my @x = ('A','B'); print "-------------------\n"; print "[foo", scalar(@x[0..2]), "]\n"; print "-------------------\n"; print "[foo", scalar(@x[0..1]), "]\n"; print "-------------------\n"; print "[foo", scalar(@x[0..0]), "]\n"; print "-------------------\n"; print "[foo", scalar(@x[0..-1]), "]\n"; print "-------------------\n";

    With 5.8.4 (and earlier), this prints

    ------------------- Use of uninitialized value in print at ./663945.pl line 9. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- [foo[foo] -------------------

    and with 5.8.8 or 5.10.0

    ------------------- Use of uninitialized value in print at ./663945.pl line 9. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- Use of uninitialized value in print at ./663945.pl line 15. [foo] -------------------

    As has already been pointed out elsewhere in the thread, this is mostly expected behaviour, because (from perldoc -f scalar)

    scalar EXPR
    (...)
    Because "scalar" is unary operator, if you accidentally use for EXPR a parenthesized list, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context.

    ...except for the "[foo[foo]", of course.

    I don't have a real explanation (probably simply a bug)...  just a couple of related observations with respect to using [0..-1] with slices. When you use a literal list instead of a named array, there's still some curious behaviour in recent releases of Perl:

    print "-------------------\n"; print "[foo", scalar(('A','B')[0..2]), "]\n"; print "-------------------\n"; print "[foo", scalar(('A','B')[0..1]), "]\n"; print "-------------------\n"; print "[foo", scalar(('A','B')[0..0]), "]\n"; print "-------------------\n"; print "[foo", scalar(('A','B')[0..-1]), "]\n"; print "-------------------\n";

    prints

    ------------------- Use of uninitialized value in print at ./663945.pl line 26. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- Argument "[foo" isn't numeric in list slice at ./663945.pl line 32. [fooA] -------------------

    The '"[foo" isn't numeric...' seems to suggest that with [0..-1] the value "[foo" is being used to index the element from the list... which is confirmed by this:

    print "-------------------\n"; print 2, scalar(('A','B')[0..-1]), "]\n"; # elem at index 2 (undef) print "-------------------\n"; print 1, scalar(('A','B')[0..-1]), "]\n"; # elem at index 1 ('B') print "-------------------\n"; print 0, scalar(('A','B')[0..-1]), "]\n"; # elem at index 0 ('A') print "-------------------\n"; print -1, scalar(('A','B')[0..-1]), "]\n"; # elem at index -1 (last e +lem 'B') print "-------------------\n"; print -2, scalar(('A','B')[0..-1]), "]\n"; # elem at index -2 ('A') print "-------------------\n"; print -3, scalar(('A','B')[0..-1]), "]\n"; # elem at index -3 (undef) print "-------------------\n";

    which prints

    ------------------- Use of uninitialized value in print at ./663945.pl line 37. 2] ------------------- 1B] ------------------- 0A] ------------------- -1B] ------------------- -2A] ------------------- Use of uninitialized value in print at ./663945.pl line 47. -3] -------------------

    Looks like this "indirect indexing" feature could be useful for obfus ;)

      Oh dear God, this is crazy. :-)) Thanks an awful lot for the explanation! This also explains why my test has been failing.

      use strict; use warnings; print "Just Another Perl Hacker\n";
Re^2: regexp list return 5.6 vs 5.8
by hipowls (Curate) on Jan 24, 2008 at 13:44 UTC

    For what's worth I can confirm that it happens with ActiveState's 5.6.1, build 638 on Linux. My tool chain is too modern to build it from source;-(