Keep It Simple, Stupid | |
PerlMonks |
Re^3: Perlsecret - plus no-opsby haukex (Archbishop) |
on Jun 24, 2022 at 15:46 UTC ( [id://11145018]=note: print w/replies, xml ) | Need Help?? |
puts the left side in scalar context That's not quite right - the "operator" requires scalar context is this example the same as saying @filled = grep {scalar @$_} @arrays; ? If by "this example" you mean the one from perlsecret, then yes, the effect is the same. ()=@$_ means to dereference the array reference that is stored in $_, and assign it to the empty list. And Assignment Operators says: "a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment." The grep is what provides the scalar context to the ()=@$_ expression (as opposed to map, which provides list context for its expression). So interpolated for the 3rd element of array this would mean grep {scalar 1,2} @array ? No, not quite. scalar("a","b") causes the Comma Operator to be evaluated in scalar context, where "it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value." So the aforementioned expression just returns "b". This is why it's important to not use arrays of numbers when testing things like this, because scalar(1,2) returns a misleading result of 2 - scalar(9,42) returns 42. An array in scalar context returns the number of elements it contains. So a more appropriate equivalence would be something like scalar(@{["a","b"]}) instead, which evaluates to 2. * Update shortly after posting: Well, to be nitpicky, it's not really an operator and doesn't really have an LHS. Take the example $n =()= "abababab" =~ /a/, run that through perl -MO=Deparse,-p, and you'll see something like this, which I've edited slightly for clarity: $n=( ()=( 'abababab'=~/a/ ) ). So as I explained above, what is happening is that the assignment ()=... is being evaluated in scalar context provided by the $n=.... A few other very minor edits.
In Section
Seekers of Perl Wisdom
|
|