in reply to regex syntax and idomatic Perl

precedence

http://perldoc.perl.org/perlop.html#Conditional-Operator

$x % 2 ? $x += 10 : $x += 2
Really means this:
(($x % 2) ? ($x += 10) : $x) += 2

$capacity =($df_output =~ m[(\d+)%\s+/home$]) ? $1 : 'Match Error';

Replies are listed 'Best First'.
Re^2: regex syntax and idomatic Perl
by cbeckley (Curate) on Mar 21, 2017 at 13:33 UTC

    Ah, ok. I see why

    $capacity =($df_output =~ m[(\d+)%\s+/home$]) ? $1 : 'Match Error';

    is correct.

    Thank you for the link, otherwise my next question was going to why the previous version didn't produce an error. However, the fact that this operator can evaluate to an lvalue now prompts the next question, where else does this occur? Does any expression that evaluates to a object or variable work the same way?

    Thanks,
    cbeckley

      ... the fact that this operator can evaluate to an lvalue now prompts the next question, where else does this occur? Does any expression that evaluates to a object or variable work the same way?

      Interesting question. All the more so since I have only a tenuous grasp of the syntactic/semantic issues involved. Obviously, something like

      c:\@Work\Perl\monks>perl -wMstrict -le "my $t = 1; my $f = ''; ;; ($t || $f) = 'foo'; print qq{t '$t' f '$f'}; " Can't modify logical or (||) in scalar assignment ... Execution of -e aborted due to compilation errors.
      doesn't work:  ($t || $f) can't be an lvalue.

      The only other situation I can think of in which this sort of hocus-pocus occurs is with Lvalue subroutines (see perlsub). I'd be interested to learn if there are any others. (Of course, now that I've asked the question, folks will probably respond with a million examples! :)

      BTW: the  ?: ternary operator in C/C++ has the same lvalue selection property.


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

        substr is also an lvalue sub, but its return value can also be magic...

        $ perl -lwMstrict my $x="abcdefghi"; for (substr $x,1,3) { print; s/c/C/; print; } print $x; my $y = \substr $x,5,3; $$y=~s/g/G/; print $x; __END__ bcd bCd abCdefghi abCdefGhi
        > (Of course, now that I've asked the question, folks will probably respond with a million examples! :)

        Bring it On!
        The first time I saw syntax like

        obj.method(p1, pn).another_method(...).and_yet_another();

        I felt the earth move ...

        It wasn't Perl of course, but that's the kind thing ... well, it's why we keep doing this, isn't it?