Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by chromatic (Archbishop)
on Jan 13, 2007 at 19:23 UTC ( #594564=note: print w/ replies, xml ) Need Help??


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

I guess they do.

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

Or is that also a kind of disambiguation (of the match operator)?

In one sense, those parentheses only mark the empty list, but it's probably more accurate to say that those do create a list. That's only true for the empty list however, as parsers have a very difficult time identifying invisible, non-existent characters.

Hmm.. here they obviously don't force list context?

Correct. Why would you expect them to do so? They're immaterial to the expression, just as in my $x = ( 2 + 2 );.

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

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

Perl doesn't have a strict leftmost-applicative evaluation order, so the parentheses are necessary to disambiguate precedence.

That's the exact reason why the parentheses are necessary to group lists, but do not create lists. In:

my @fib_values = 1, 1, 2, 3;

... the expression my @fib_values = 1 is a complete expression to the parser as it is. Now it may be completely unambiguous to you that the entire expression is a list assignment, but there are plenty of more complicated assignment forms that involve mixed expressions such that Perl will give you a warning that it may have guessed wrong in this case.

Note also that you don't need parentheses when passing an argument list to a function or especially a built-in operator... again, unless you need to disambiguate something.


Comment on Re^3: (RFC) Arrays: A Tutorial/Reference
Select or Download Code
Re^4: (RFC) Arrays: A Tutorial/Reference
by shmem (Canon) on Jan 13, 2007 at 20:16 UTC
    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}
      @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}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-12-27 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (176 votes), past polls