the function is called in scalar context, so that the number of list elements should be printed.

That's not what scalar context does. Scalar context simply lets operators know they can only return a single scalar value. What that value is up to each individual operator.

Some operators (e.g. @array, grep) return the number of values that would otherwise be returned, but that's far from typical. The most common practice is to return exactly the same thing in every context.

Looking at your code, you have a number of subs evaluated in scalar context. That means their return expression is evaluated in scalar context. Your code is equivalent to

print(scalar( @{ [] } ),"\n"); print(scalar( qw,, ),"\n"); print(scalar( () ),"\n");

which the tokenizer changes into

print(scalar( @{ [] } ),"\n"); print(scalar( () ),"\n"); print(scalar( () ),"\n");

In the first case, you have an array lookup in scalar context. That returns the number of elements in the array being looked up, which is zero.

In the second and third case, you have a list literal in scalar context. That returns the last element of the list after evaluating it scalar context. If the list is empty, undef is returned.


In reply to Re: Empty List miracle(1) by ikegami
in thread Empty List miracle(1) by rovf

Title:
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, details, 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, summary, 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.