Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 ./ line 9. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- [foo[foo] -------------------

and with 5.8.8 or 5.10.0

------------------- Use of uninitialized value in print at ./ line 9. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- Use of uninitialized value in print at ./ 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";


------------------- Use of uninitialized value in print at ./ line 26. [foo] ------------------- [fooB] ------------------- [fooA] ------------------- Argument "[foo" isn't numeric in list slice at ./ 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 ./ line 37. 2] ------------------- 1B] ------------------- 0A] ------------------- -1B] ------------------- -2A] ------------------- Use of uninitialized value in print at ./ line 47. -3] -------------------

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

In reply to Re^2: regexp list return 5.6 vs 5.8 by almut
in thread regexp list return 5.6 vs 5.8 by Sixtease

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (6)
    As of 2020-11-24 09:34 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found