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

what is scalar?

by kiseok7 (Beadle)
on Jul 09, 2001 at 06:40 UTC ( #94890=perlquestion: print w/replies, xml ) Need Help??

kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: what is scalar?
by japhy (Canon) on Jul 09, 2001 at 07:24 UTC
    The scalar() function does not return the size of a list. It enforces scalar context upon its argument. Some functions behave different in scalar context than in list context. Arrays return their length in scalar context.

    Lists, however, don't exist in scalar context. But the comma operator does exist in scalar context; it evaluates its operands, and then returns the one on the right.
    print scalar(1,2,3,4); # 4 print scalar(2,3,4); # 4 print scalar(4,3,2,1); # 1

    japhy -- Perl and Regex Hacker
Re: what is scalar?
by myocom (Deacon) on Jul 09, 2001 at 06:50 UTC

    The difference boils down to this: localtime() is not a list, it's a function that returns a list when called in list context. When it's called in scalar context (such as when you force it with scalar or assign to a scalar value such as $now = localtime;) it returns a "pretty" version of the current date and time.

Re: what is scalar?
by CheeseLord (Deacon) on Jul 09, 2001 at 06:56 UTC

    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

      You can also force list context without changing @_ by assigning to an empty list.

      scalar ( () = localtime(time) );
      The scalar value of a list assignement is the number of elements in the list on the right side of the assignment. This yields the number of elements returned by localtime (not the number of elements in the empty list, that would be dull).


      Cheese obsession? No, do roll with it!

      Instead of the historical Fred, Barney, Adam, Eve, Alice and Bob, the standard will become Munster, Cheddar, Gouda, Brie, Parmesan, Ricotta, Swiss, Monteray Jack, Mozzarella, Derby, Colby, Blue, Feta, Cheshire and Provolone!

Re: what is scalar?
by Seumas (Curate) on Jul 09, 2001 at 06:55 UTC
    I'm not certain I understand your question with regard to "what is scalar?", but to do what you're attempting, you can use this:
    my ($a, $b, $c, $d) = localtime(time); print $d;
    After reading the first response to your question, what you were asking is clearer (had my blinders on, apparently). I like the way myocom explained it. In Perl, context is everything. I also like CheeseLord's snip of code better, if all you want is to grab the day of the month. My example is assuming you want to use other portions of the returned time and date as well.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94890]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2023-03-24 09:08 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.