Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^5: (RFC) Arrays: A Tutorial/Reference

by rir (Vicar)
on Jan 15, 2007 at 03:52 UTC ( #594688=note: print w/replies, xml ) Need Help??

in reply to Re^4: (RFC) Arrays: A Tutorial/Reference
in thread (RFC) Arrays: A Tutorial/Reference

@list = (1) x 15;
This is a special case for the repetition operator's LHS as documented in perlop.

It is misleading to think that parentheses do anything but group or, more formally, create a term.

Be well,

Replies are listed 'Best First'.
Re^6: (RFC) Arrays: A Tutorial/Reference
by shmem (Chancellor) on Jan 15, 2007 at 08:24 UTC

    That case isn't so special.

    Parens do term grouping, except when when used in the LHS of a binary op, in which case they provide list context to the LHS (and force that context on the RHS(?) ), e.g. list assignment vs. scalar assignment.

    $c = () = /\w+/g;


    $c = ( () = /\w+/g );

    The parens (as LHS) in the rightmost (inner) assignment provide list context to the match operator, it's result is then (by the scalar on the LHS of the outer assignment) forced back into scalar context, which gives the element count. No list is created by the parens; the count is taken from the "fall-through"-list of that non-assigment, not from the "empty list".

    In the expr (split)[1], the parens don't create a list (this is done by split), but group that list, so an element can be pulled out via an index ([1]).

    I stand corrected, the statement "parens sometimes create lists" is bogus. Please correct me if any of the above statements is incorrect.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      The short version: Parentheses create a list when the context is indeterminant. Always? Sometimes? I don't know, I just have the wwimery1 down pretty well.
      $c = ( () = /\w+/g );
      This is a special case documented in perlop under Assignment Operators and, I think, perldata documents () as the literal null list. Your description seems correct to me.

      () = ("head", "tail"); @ar = ( "head", "tail"); ($head, $tail) = ( "head", "tail" );

      Lead us, in notational way, to accepting or expecting the behaviour of

      ($head) = ( "head", "tail" );
      but I don't think I've seen this documented. Here the parenthesis do make a list of the assignment's LHS. It is too easy to think that the LHS of an assignment operation is effected by the enclosing context, when we should realize that the context of an assignment op's LHS is self-determinant. That is the similarity with the repetition operator.

      Be well,

      1 Write what it means.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://594688]
[erix]: advantage of insomnia: having a good look at Jupiter with its four galilean moons

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-04-20 03:21 GMT
Find Nodes?
    Voting Booth?