Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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 ./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 ;)


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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found