Yes, that's why I always use characters ... but there is another trap lurking ...

Extra tip: never use the range operator to return a list.

One of the most annoying design flaws of Perl is the propagation of context to a subs returning statement. That's action at a distance...

And you really don't want a flip-flop when you expect a list...

DB<50> sub tst { "a".."d" } DB<51> x tst() # list context => range 0 'a' 1 'b' 2 'c' 3 'd' DB<52> p scalar tst() # scalar context => flip-flop 1E0 DB<53> x tst() # list context => WTF??? 0 0 DB<54> x tst() # once flip-flop, always flip-flop 0 0 DB<55>

Workaround: reverse

DB<44> sub tst { reverse "a".."d" } DB<45> x tst() 0 'd' 1 'c' 2 'b' 3 'a' DB<46> p scalar tst() dcba DB<47> x tst() 0 'd' 1 'c' 2 'b' 3 'a' DB<48>

And if don't like the order, reverse twice

DB<55> sub tst { reverse reverse "a".."d" }


another - uglier - alternative:

DB<59> sub tst { @{["a".."d"]} } DB<60> x tst() 0 'a' 1 'b' 2 'c' 3 'd' DB<61> p scalar tst() 4 DB<62> x tst() 0 'a' 1 'b' 2 'c' 3 'd' DB<63>

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

In reply to Re^3: What's happening in this expression? (Pro Tip: never return range) by LanX
in thread What's happening in this expression? by zapdos

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.