in reply to Re: In place search and replace with a hash
in thread In place search and replace with a hash

Did you test it?

I can't at the moment°, but imho the first step on RHS is string like var interpolation. There is no eval flag set.

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

°) mobile in a disco, ain't that I'll? ;)

  • Comment on Re^2: In place search and replace with a hash

Replies are listed 'Best First'.
Re^3: In place search and replace with a hash
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 02:52 UTC
    c:\@Work\Perl\monks>perl -wMstrict -le "my %hash = ('$1' => 'oops', 'abc' => 'ok'); ;; 'abc' =~ m{ (abc) }xms; print qq{captured '$1'}; ;; print $hash{'$1'}; " captured 'abc' oops

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

      You missed the point. Imho RHS of s/// isn't evaluated w/o e flag.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Hi AnomalousMonk, thanks for your reply. I probably should have specified that I'm pretty new to perl. Could you comment at all at what your code does?

        The code example above was meant as a counter-example to what I understood LanX to be saying. Comments:

        my %hash = ('$1' => 'oops', 'abc' => 'ok'); # create a hash 'abc' =~ m{ (abc) }xms; # capture a sub-string to $1 print qq{captured '$1'}; # show what that string is print $hash{'$1'}; # show effect of single-quotes: no interpolation
        Had  $1 been used directly (as in $hash{ $1 }), the output would have been 'ok'. A more complete example:
        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %hash = ('$1' => 'oops', 'abc' => 'ok'); dd \%hash; ;; 'abc' =~ m{ (abc) }xms; print qq{captured '$1'}; ;; print 'single-quoted: ', $hash{'$1'}; print 'straight: ', $hash{ $1 }; " { "\$1" => "oops", "abc" => "ok" } captured 'abc' single-quoted: oops straight: ok


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

Re^3: In place search and replace with a hash
by Anonymous Monk on Dec 28, 2014 at 03:02 UTC
    You're so addicted to Perl you can't forget it even in a disco :)

    It works with strings too :)

    use 5.020; use warnings; my %h = ( foo => 1, bar => 2 ); $_ = 'foobar'; s/(foo)/$h{'$1'}/; say $_; $_ = 'foobar'; s/(foo)/$h{$1}/; say $_; my $foo = 'foo'; say "$h{'$foo'}bar";
    output:
    Use of uninitialized value $h{"$1"} in substitution iterator at extr.p +l line 7. bar 1bar Use of uninitialized value $h{"$foo"} in concatenation (.) or string a +t extr.pl line 15. bar
      OK I understand now what's happening, thanks! :)

      The interpolation is done by translating to a concat which is executed.

      (parsing hell)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      update

      see update in this post for an explanation of how interpolation works.