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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
RE: RE: Re: Passing arrays to a CGI
by swiftone (Curate) on Sep 21, 2000 at 20:04 UTC
    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.

      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

RE: RE: Re: Passing arrays to a CGI
by merlyn (Sage) on Sep 21, 2000 at 19:56 UTC
    @values = (param('foo'));
    The 'extra' parens around the param() call force list context.
    They do nothing of the sort! They are completely unnecessary. This will do just fine:
    @values = param 'foo';

    -- Randal L. Schwartz, Perl hacker

      According to the documentation, maybe, and probably lots of your experience, it will work just fine.
      This didn't work for me. All the array values I passed were dumped into the first array element when I tried
      @list = param 'foo'; I was forced to add
      @list_f = split /\s+/, $list[0];
      I'm sure this had something to do with slices.
      I sent the data to this script using
      script.pl?list=@list