puts the left side in scalar context

That's not quite right - the "operator" requires scalar context onfrom its LHS* for it to work correctly. From perlsecret (emphasis mine): It "provides a list context to its right side and returns the number of elements to its left side. Note that the left side must provide a scalar context; obviously, a list context on the left side will receive the empty list in the middle."

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 reply to Re^3: Perlsecret - plus no-ops by haukex
in thread Perlsecret - plus no-ops by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.