in reply to replacing equal to operator

I'm sorry -- why doesn't this work?:

s/=(.*)/="\1"/;

Replies are listed 'Best First'.
Re^2: replacing equal to operator
by AnomalousMonk (Archbishop) on Jan 28, 2021 at 20:03 UTC

    It works, but using a backreference \1 in place of a capture variable $1 in the replacement string is Frowned Upon and will earn you a warning:

    Win8 Strawberry 5.8.9.5 (32) Thu 01/28/2021 14:55:21 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); my $s = "parameter=TO_DATE('1900-01-01','YYYY-MM-DD')\n"; dd 'before:', $s; $s =~ s/=(.*)/="\1"/; \1 better written as $1 at - line 7. dd 'after:', $s; ^Z ("before:", "parameter=TO_DATE('1900-01-01','YYYY-MM-DD')\n") ( "after:", "parameter=\"TO_DATE('1900-01-01','YYYY-MM-DD')\"\n", )


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

Re^2: replacing equal to operator
by BillKSmith (Monsignor) on Jan 28, 2021 at 20:49 UTC
    You need "=" in substitution field. Replace "\1" with "$1" per warnings.
    use strict; use warnings; use Test::Simple tests=>1; my $input_string = qq(parameter=TO_DATE('1900-01-01','YYYY-MM-DD')\n); my $desired_output = qq(parameter="TO_DATE('1900-01-01','YYYY-MM-DD')" +\n); my $output = $input_string =~ s/=(.*)/="$1"/r; ok( $output eq $desired_output, $output );

    OUTPUT:

    1..1 ok 1 - parameter="TO_DATE('1900-01-01','YYYY-MM-DD')" #
    Bill
      You need "=" in substitution field.

      And I had it. What's your point?

        OOPS, Perhaps I need new glasses!
        Bill