Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: regex syntax and idomatic Perl

by cbeckley (Curate)
on Mar 21, 2017 at 13:33 UTC ( #1185351=note: print w/replies, xml ) Need Help??


in reply to Re: regex syntax and idomatic Perl
in thread regex syntax and idomatic Perl

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

Replies are listed 'Best First'.
Re^3: regex syntax and idomatic Perl
by AnomalousMonk (Bishop) on Mar 22, 2017 at 21:50 UTC
    ... 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

        For the record, I stared at that piece of code for an entire beer.

        So ... $_ in the for loop, and $y are references ...
        and you could have said

        substr $x,5,3 =~ s/g/G/;

        Instead of

        my $y = \substr $x,5,3; $$y=~s/g/G/;

        For the same result?

      > (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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1185351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-06-01 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (12 votes). Check out past polls.

    Notices?