Re: meaning of =()=
by ikegami (Patriarch) on Jan 04, 2010 at 17:39 UTC
|
$scalar =()= EXPR
is a funky way of writing
$scalar = ( () = EXPR )
It consists of two operations: a list assignment (() = EXPR) as the RHS of a scalar assignment ($scalar = ...). Both types of assignments are documented in perlop, but the bits that apply are:
- The RHS of a list assignment is evaluated in list context.
- The RHS of a scalar assignment is evaluated in scalar context.
- A list assignment in scalar context returns the number of elements returned by its RHS.
In short, it's a means of evaluating an expression in list context and returning the number of elements returned instead of the elements themselves.
See Mini-Tutorial: Scalar vs List Assignment Operator
| [reply] [d/l] [select] |
Re: meaning of =()= (idiom)
by toolic (Bishop) on Jan 04, 2010 at 17:33 UTC
|
| [reply] |
Re: meaning of =()=
by JavaFan (Canon) on Jan 04, 2010 at 18:01 UTC
|
Is there a name for the =()= "operator"?
It's the goatse (secret) operator.
Does it have any further significance beyond returning the number of matches from a regular expression?
Of course! It can also be used to find out the number of elements returned by localtime. Or by caller. Or any other function that in scalar context doesn't return the number of elements of what it would return in list context.
| [reply] [d/l] [select] |
|
One obscure person cannot rename the flying lentil (hairy lentil) to goatse (secret) operator.
| [reply] |
Re: meaning of =()=
by Anonymous Monk on Jan 04, 2010 at 17:42 UTC
|
Thanks all. Just found this page too, which seconds the "goatse" name
http://www.catonmat.net/blog/secret-perl-operators/ | [reply] |
|
| [reply] [d/l] |
|
When I invented @{[]}, I neglected to give it a name. Maybe I should have asked Heidi (Larry Wall's daughter), like I did to come up with <=> as "spaceship", which has apparently stuck (having been even in the text of an error message for a while).
-- Randal L. Schwartz, Perl hacker
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
| [reply] [d/l] [select] |
|
|
Re: meaning of =()=
by zentara (Archbishop) on Jan 04, 2010 at 17:34 UTC
|
... the spread eagle ?.... ;-)
| [reply] |
|
| [reply] |
|
.... i'm glad i'm not a psychotherapist
| [reply] |
Re: meaning of =()= is flying lentil
by Anonymous Monk on Jan 04, 2010 at 21:50 UTC
|
| [reply] |
Re: meaning of =()=
by hbm (Hermit) on Jan 06, 2010 at 16:04 UTC
|
| [reply] |
Re: meaning of =()= (null list accumulator, list counter)
by Anonymous Monk on Aug 05, 2013 at 10:59 UTC
|
$ perl -e " @g=( 1 .. 4 ); $f[0]=()=@g; dd\@f; dd\@g "
[4]
[1 .. 4]
In list context the null list as lvalue is null (accumulates and discards ) $ perl -e " @g=( 1 .. 4 ); @f=()=@g; dd\@f; dd\@g "
[]
[1 .. 4]
| [reply] [d/l] [select] |