Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by shmem (Canon)
on Jan 13, 2007 at 20:16 UTC ( #594568=note: print w/ replies, xml ) Need Help??


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

I hate being cruel... but read the parser.

Mhm. That's been on my todo list for a long time now. The parser, the tokenizer and the lexer. Well.. lots of excuses..

In one sense, those parentheses only mark the empty list, but it's probably more accurate to say that those do create a list.

as is the case with

@list = (1) x 15;

If parentheses did create lists, what would you expect this code to do?

my $x = ( 1 + 2 ) * 3;

I would expect the parens to do grouping. I didn't assume that parens always create lists (which they don't), but that they do - sometimes:

$_ = "foo bar baz"; $middle = (split)[1];

Well, as everywhere with perl - contetxt matters...

--shmem

update: replaced nonsensical $_ = qw(foo bar baz) with $_ = "foo bar baz". Common typo ;)

_($_=" "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}


Comment on Re^4: (RFC) Arrays: A Tutorial/Reference
Select or Download Code
Re^5: (RFC) Arrays: A Tutorial/Reference
by rir (Vicar) on Jan 15, 2007 at 03:52 UTC
    @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,
    rir

      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;

      is

      $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.

      --shmem

      _($_=" "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,
        Rob


        1 Write what it means.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-12-19 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (92 votes), past polls