list vs scalar context for localtime?

I

Ian Wilson

Which X and Y in `perldoc -X Y` will explain why we need parentheses
around localtime in

#!/usr/bin/perl
use strict;
use warnings;
my ($d,$m,$y) = (localtime)[3,4,5];
printf (" Today is %02d/%02d/%04d\n", $d, $m+1, $y+1900);

AFAIK Perl uses parentheses for list context and for precedence, I think
they are used here to force a list context?

In the assignment statement, since the LHS is a list, I'd have thought
it would supply a list context to the RHS anyway.

Also, it superficially appears to me that the [3,4,5] ought to be a hint
that the thing to the left ought not to be interpreted in a scalar context.

I've read perldoc -f localtime and perused my blue Camel.
Clearly I'm missing something fundamental :-(
 
J

Jeff 'japhy' Pinyan

Which X and Y in `perldoc -X Y` will explain why we need parentheses
around localtime in

#!/usr/bin/perl
use strict;
use warnings;
my ($d,$m,$y) = (localtime)[3,4,5];
printf (" Today is %02d/%02d/%04d\n", $d, $m+1, $y+1900);

AFAIK Perl uses parentheses for list context and for precedence, I think
they are used here to force a list context?

The () are necessary for the [3,4,5] to be a subscript of a list.
Consider the function localtime(), which takes an optional argument, the
number of seconds for which to calculate the date. If you did:

my @stuff = localtime [3,4,5];

you would be passing an array reference to localtime(), and you'd get
unexpected values.

The () in (localtime)[3,4,5] don't *make* a list, they *contain* the list.

I would guess perldata would discuss this issue.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 
T

Tad McClellan

Subject: list vs scalar context for localtime?


Your question does not have anything to do with context.

Which X and Y in `perldoc -X Y` will explain why we need parentheses
around localtime in
my ($d,$m,$y) = (localtime)[3,4,5];


perldoc perldata

See the "Slices" section.

AFAIK Perl uses parentheses for list context and for precedence, I think
they are used here to force a list context?


No, because...

In the assignment statement, since the LHS is a list, I'd have thought
it would supply a list context to the RHS anyway.


It is the assignment operator that supplies the context for its RHS.

I've read perldoc -f localtime and perused my blue Camel.


Yes, because a list slice can be used with _any_ list, not just
the list returned by localtime.

Clearly I'm missing something fundamental :-(


"list slices" are the fundamental that you are missing.
 
M

Matija Papec

X-Ftn-To: Ian Wilson

Ian Wilson said:
Which X and Y in `perldoc -X Y` will explain why we need parentheses
around localtime in

#!/usr/bin/perl
use strict;
use warnings;
my ($d,$m,$y) = (localtime)[3,4,5];
printf (" Today is %02d/%02d/%04d\n", $d, $m+1, $y+1900);

AFAIK Perl uses parentheses for list context and for precedence, I think
they are used here to force a list context?

In the assignment statement, since the LHS is a list, I'd have thought
it would supply a list context to the RHS anyway.

Also, it superficially appears to me that the [3,4,5] ought to be a hint
that the thing to the left ought not to be interpreted in a scalar context.

No, left side clearly tells to localtime that it wants list context. (see
perldoc -f wantarray) You need braces around localtime so you can pick
particular list elements => 3,4,5

You can also write this like
my (undef, undef, undef, $d,$m,$y) = localtime;
and you'll be again ignoring everything but 3,4, and 5th element from list.
 
U

Uri Guttman

IW> Which X and Y in `perldoc -X Y` will explain why we need parentheses
IW> around localtime in

-X??

IW> my ($d,$m,$y) = (localtime)[3,4,5];

IW> AFAIK Perl uses parentheses for list context and for precedence, I
IW> think they are used here to force a list context?

IW> In the assignment statement, since the LHS is a list, I'd have thought
IW> it would supply a list context to the RHS anyway.

IW> Also, it superficially appears to me that the [3,4,5] ought to be a
IW> hint that the thing to the left ought not to be interpreted in a
IW> scalar context.

try to parse localtime[3,4,5].

without trying, it looks like illegal syntax. a [] subscript needs a
list or an array (scalar or list mode) to index into. (localhost)
provides the list whereas localhost doesn't.

the parens are always for precedence or syntax and (almost) never to
create a list. list/scalar context is provided by nearby code and not by
the list (or what looks like a list) itself.

uri
 
U

Uri Guttman

A> $$ without trying, it looks like illegal syntax. a [] subscript needs a
A> $$ list or an array (scalar or list mode) to index into. (localhost)
A> $$ provides the list whereas localhost doesn't.

A> localtime [3,4,5] is legal syntax. It's equivalent to
A> localtime ([3,4,5]).

like i said, i didn't try :)

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top