Interesting question... I think I can answer it.

Basically, localtime is specially written such that calling it in scalar context (which is what scalar does) will return a string, rather than just a list converted to scalar (e.g. 9). This is done through using wantarray. Take this example:

sub cheese { my @cheese = qw(cheddar swiss); if (wantarray) { return @cheese } else { return $cheese[0] } }

Basically, wantarray will tell you if your subroutine was called in scalar or list context, as I understand it. It's used here to return at least some kind of cheese, so that if all the caller wants is some cheese, they'll get it.

It should also be noted that you can check to see if the sub was called in void context by using defined wantarray.

BTW, if you really wanted 9, you could try this code:

scalar (@_ = localtime(time))

Update: Or you could do like bwana147 suggested and assign to an empty list, preserving the contents of @_. That'd probably be better.

(I only hope not too many people think I'm obsessed w/ cheese now...)

His Royal Cheeziness


In reply to Re: what is scalar? by CheeseLord
in thread what is scalar? by kiseok7

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.