Adding parentheses creates a list context ...

This is either terminal nitpicking on my part or a confession of total ignorance, but I thought that for substitution, the
    ($x = $y) =~ s///;
form (case B below) worked by circumventing normal operator precedence and making the value, normally a scalar, which is bound by the =~ operator into an assignment expression, which causes the substitution result to be assigned to the assignment lvalue. I.e., list context does not come into play at all. In the
    $x = $y =~ s///;
statement (case C), the =~ operator has precedence over = and the  s/// return value, the number of substitutions in any context, is assigned. (Somewhere in my Perlish consciousness, I have the idea that substitution normally builds its result in a temporary variable, which is then copied to the bound scalar upon successful completion of the substitution. If an assignment expression is detected, the temp result is simply copied to a different destination.)

Of course, for a  m// match operation (case A below), list context can be very important.

c:\@Work\Perl>perl -wMstrict -le "my $y = '2017-01-29 11:30:07.370'; my $x; ;; ($x) = $y =~ m{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* }xms +; print qq{A: x '$x' y '$y'}; ;; ($x = $y) =~ s{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* } {$2-$1 $3}xms; print qq{B: x '$x' y '$y'}; ;; $x = $y =~ s{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* } {$2-$1 $3}xms; print qq{C: x '$x' y '$y'}; " A: x '2017' y '2017-01-29 11:30:07.370' B: x '01-29-2017 11:30' y '2017-01-29 11:30:07.370' C: x '1' y '01-29-2017 11:30'


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Assigning a parsed date to a variable by AnomalousMonk
in thread Assigning a parsed date to a variable 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.