[tpm] dereferencing anonymous hashes

Rob Janes janes.rob at gmail.com
Thu Apr 12 02:05:05 PDT 2007

I guess I've just never seen $x = (); before.  doesn't seem very useful
unless it's a list expression.

[robj at localhost ~]$ perl -e '$x=(5,44); print "$x\n";'

[robj at localhost ~]$ perl -e '$x=@x=(5,44); print "$x\n";'

So the comma operator behaves differently in a scalar context from how it
behaves in a list context.

[robj at localhost ~]$ perl -e 'sub x { print "side effect\n"; return 5 }
$x=(x,44); print "$x\n";'
side effect

Showing that the comma operator is useful for side effects in scalar context

On 4/10/07, Uri Guttman <uri at stemsystems.com> wrote:
> >>>>> "RJ" == Rob Janes <janes.rob at gmail.com> writes:
>   RJ> $x = (); is a list assignment to a scalar.  scalar conversion
>   RJ> results in the scalar ($x that is) being assigned the count of the
>   RJ> number of items in the list.  0 or zero.
> wrong result and wrong explanation. you can't have a list in scalar
> context by definition. in this case the () are just doing grouping of
> nothing so it is just like saying my $x and the value in $x is undef.
> perl -lwe '$x = () ; print $x'
> Use of uninitialized value in print at -e line 1.
> you can only get the number of elements in an array by putting it in
> scalar context. and () does not make a list. parens only do grouping in
> perl, and never directly make a list.
> uri
> --
> Uri Guttman  ------  uri at stemsystems.com  --------
> http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and
> Coding-
> Search or Offer Perl Jobs  ----------------------------
> http://jobs.perl.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/toronto-pm/attachments/20070412/30308d65/attachment.html 

More information about the toronto-pm mailing list