in reply to RE: Re: Passing arrays to a CGI
in thread Passing arrays to a CGI

Indeed, I just read this in Effective Perl Programming. I was shocked to discover that wrapping parens about a variable does NOT force it into list context. For the curious, the book lists as the only(?) way to force &foo into list context:  @{[&foo]}.

(that is, dereference an anonymous list of the item being forced to list context. See perlref for more on how referencing/de-referencing works.)

Anyone know WHY () doesn't work as the list version of scalar? I'm assuming there is either a good programming or perl innards practical reason, because I doubt that arturo and I were the only ones thinking this incorrectly.

Replies are listed 'Best First'.
Forcing list context
by merlyn (Sage) on Sep 21, 2000 at 20:07 UTC
    There was a long thread about this on perl6-language the other day. There's no need for a "list" operator (opposing the "scalar" operator) because everything that can make use of list context already provides list context automatically.

    And there are no consistent ways to "downcast" a list to a scalar, so you'd have to be explicit about what you wanted: first element, last element, count, and so on. And all of those are already available, either in the form of a literal slice:

    $first = (list_context_expr)[0]; $last = (list_context_expr)[-1];
    or list-assignment operator:
    $count = (() = list_context_expr); # also written as: $count= () = list_context_expr;
    So, a "list" operator is neither necessary nor sufficient. Parens are not a "list" operator. They're just a syntax helper, usually for precedence.

    -- Randal L. Schwartz, Perl hacker