A little spacing and expansion helps:

perl -ne '$& && print( $_ ), $_ =~ m/default|$/'

Take that one step at a time...

-n tells Perl to loop over the input, line by line, placing each line into $_, one line per iteration.

$& is the special variable that contains the string matched in the most recent pattern match. On the first iteration $_ is set to the first line, but $& is empty because we haven't yet executed a match.

&& is the short-circuit operator. It evaluates the LHS first. If the LHS is false (no value, or zero), it skips the right hand side. But if the lefthand side is true (contains a value that is non-zero, and not the empty string) it evaluates (executes) the right-hand side. On the right hand side is 'print'.

On the first iteration (as mentioned before), $& is empty, thus false. So nothing gets printed. Then the comma operator moves us along to the regexp matching. If $_ matches /default|$/, $& gets a value, and if it matched the 'default' portion of the alternation in the regexp, on the next iteration, the logical short-circuit will be true. And that being the case, the contents of $_ (on the next iteration) will be printed.

Some of this will make more sense if you have a look at perlrun and perlvar. This is what they're talking about when they say that Perl has a rich and compact syntax.


Dave


In reply to Re: Obscure expression! by davido
in thread Obscure expression! by chopper1804

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.