Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Questions about context

by Revelation (Deacon)
on Jan 30, 2005 at 20:15 UTC ( [id://426417]=note: print w/replies, xml ) Need Help??


in reply to Questions about context

It's important to note that arrays and lists are not equivalent. With regards to  (caller 0)[3] and (localtime)[5]+1900 the parentheses are forcing the evaluation of the list like an array. Lists, which are usually diagramed as  (LIST) in Perl can be subscripted like a normal array. However, to do so the a slice syntax is used, which takes the form  @result/$result = ( LIST ) [ LIST ];. The parens are used to avoid ambiguity. Merlyn could further clarify.

Thus,
my $scalar= ( EXPR )[0];
is the rough equivalent of:
my $scalar; { my @expr = EXPR(); $scalar = @expr[0]; # or $expr[0]; }

Gyan Kapur

Replies are listed 'Best First'.
Re^2: Questions about context
by rir (Vicar) on Jan 31, 2005 at 22:34 UTC
    (localtime)[5]+1900 the parentheses are forcing the evaluation of the list like an array.

    It is a misleading statement. The parentheses are only for grouping. localtime[5] will convert [5] into an array of date and time elements. This is not what you want because [5] is not a reasonable thing to convert to a time.

    The parentheses are only to get localtime to be called without an argument.

    Enclosing parentheses don't change context. Look up the dual-nature comma operator to understand lists and their parentheses. That the standard (i.e. Camel) example of a list includes parentheses doesn't aid proper understanding of this issue. But it is impressive how the dwimery works in spite of this.

    Be well,
    rir

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://426417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found