in reply to How to reverse a string *revisited*

And don't forget there's the reverse of scalar()
()
which provides array context:

my $foo = (1,1,1,1,1); my $bar = () = (1,1,1,1,1); print "foo = $foo, bar = $bar\n"; __END__ foo = 1, bar = 5

Liz

Update:
Please check out Abigail-II's and AM's corrections to my poor understanding/wording of this feature of Perl and how it is used in some nice Perl idioms.

Replies are listed 'Best First'.
Re: How to reverse a string *revisited*
by Abigail-II (Bishop) on Nov 11, 2003 at 10:09 UTC
    Well, that's a dangerous thing to say. Already there are way to many people thinking that dumping a set of parens around an expression puts that expression into list context. That isn't true. In your example, it's the fact that there is a list on the LHS which makes list context.
    #!/usr/bin/perl use strict; use warnings; sub f { print wantarray ? "LIST\n" : defined wantarray ? "SCALAR\n" : "VOI +D\n" } (f); my $foo = (f); my @foo = f; __END__ VOID SCALAR LIST

    Abigail

Re: Re: How to reverse a string *revisited*
by Anonymous Monk on Nov 11, 2003 at 10:08 UTC
    There is no such thing as array context, that's a list in scalar context ;) read perldoc perldata

    List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:

    $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 $x = (($foo,$bar) = f()); # set $x to f()'s return count
    This is handy when you want to do a list assignment in a Boolean context, because most list functions return a null list when finished, which when assigned produces a 0, which is interpreted as FALSE.

    It's also the source of a useful idiom for executing a function or performing an operation in list context and then counting the number of return values, by assigning to an empty list and then using that assignment in scalar context. For example, this code:

    $count = () = $string =~ /\d+/g;
      There is no such thing as array context, that's a list in scalar context ;)

      There's no such this as a list in scalar context. ;-)

      From perlop - Comma Operator:

      Binary ``,'' is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.
      In list context, it's just the list argument separator, and inserts both its arguments into the list.
      ihb