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

... 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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: regex syntax and idomatic Perl
by haukex (Archbishop) on Mar 22, 2017 at 22:04 UTC

    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?

        So ... $_ in the for loop, and $y are references

        Not exactly, $_ is an alias to the elements being looped over, not a reference (which would need to be dereferenced), see the first few paragraphs of Foreach Loops. Some more fun with aliases, note how the magic substr lvalue remembers its bounds in the string:

        our ($x,$y) = "Hello, World!"; *y = \substr $x, 7, 5; # alias via glob $y = "substring"; print "<$x>\n"; # prints <Hello, substring!> $y = "Perl"; print "<$x>\n"; # prints <Hello, Perl!> $x = "Magic, cool stuff!"; print "<$y>\n"; # prints <cool>

        Yes, except for a precedence problem:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 'abcdefghi'; substr($x, 5, 3) =~ s/g/G/; print $x; " abcdefGhi
        Without some sort of scoping disambiguation, the s/g/G/ binds to 3 in
            substr $x,5,3 =~ s/g/G/;


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

Re^4: regex syntax and idomatic Perl
by cbeckley (Curate) on Mar 23, 2017 at 02:26 UTC
    > (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?