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