Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: regex syntax and idomatic Perl

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


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

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?

Replies are listed 'Best First'.
Re^6: regex syntax and idomatic Perl
by haukex (Chancellor) on Mar 23, 2017 at 11:20 UTC
    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>

      Ah, I think I see (again). So, in the for loop, $_ is an alias to the same memory location as the element in the list being looped over. If that memory location is occupied by something that is semantically an lvalue, then $_ behaves as if it were a C pointer, which is to say, behaves just like the thing it's pointing to. If the memory location is not an lvalue, then $_ does not behave like a C pointer, but still behaves like the thing it's pointing to.
      Is that right?

      I have to stop trying to understand the mechanics of Perl in terms of C.
      <Insert Yoda quote here.>
      So, $_ is imbued with the semantic context of the thing that occupies the memory location to which it points. Still thinking in C, clearly ...

Re^6: regex syntax and idomatic Perl
by AnomalousMonk (Bishop) on Mar 23, 2017 at 01:43 UTC

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

      Ah, yes, I suspected that might be the case.
      Didn't have access to a command prompt, alas.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-05-28 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (166 votes). Check out past polls.

    Notices?