Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl regex warning: \1 better written as $1 at (eval 1) line 1

use strict;
use warnings;

my $newPasswd = 'abc123';
my @lines = ( "pwd = abc", "pwd=abc", "password=def", "name= Mike" );

my %passwordMap = (
    'pwd(\\s*)=.*'      => 'pwd\\1= $newPasswd',
    'password(\\s*)=.*' => 'password\\1= $newPasswd',
);

print "@lines\n";

foreach my $line (@lines) {
    while ( my ( $key, $value ) = each(%passwordMap) ) {
        if ( $line =~ /$key/ ) {
            my $cmdStr = "\$line =~ s/$key/$value/";
            print "$cmdStr\n";
            eval($cmdStr);
            last;
        }
    }
}

print "@lines";

run it will give me the correct results:

pwd = abc pwd=abc password=def name= Mike
$line =~ s/pwd(\s*)=.*/pwd\1= $newPasswd/
\1 better written as $1 at (eval 2) line 1 (#1)
$line =~ s/password(\s*)=.*/password\1= $newPasswd/
\1 better written as $1 at (eval 3) line 1 (#1)
pwd = abc123 pwd=abc password= abc123 name= Mike

I don't want to see the warnings, tried to use $1 instead of \1, but it does not work. What should I do? Thanks a lot.

like image 657
John Avatar asked Feb 25 '23 10:02

John


2 Answers

\1 is a regex pattern that means "match what was captured by the first set of capturing parens." It makes absolutely no sense to use that in a replacement expression. To get the string captured by the first set of capturing parens, use $1.

$line =~ s/pwd(\s*)=.*/pwd\1= $newPasswd/

should be

$line =~ s/pwd(\s*)=.*/pwd$1= $newPasswd/

so

'pwd(\\s*)=.*'      => 'pwd\\1= $newPasswd',
'password(\\s*)=.*' => 'password\\1= $newPasswd',

should be

'pwd(\\s*)=.*'      => 'pwd$1= $newPasswd',
'password(\\s*)=.*' => 'password$1= $newPasswd',

or better yet

qr/((?:pwd|password)\s*=).*/ => '$1= $newPasswd',
like image 105
ikegami Avatar answered Mar 05 '23 17:03

ikegami


I see a lot of repetition in your code.

Assuming you're using Perl 5.10 or later, this is how I would have written your code.

use strict;
use warnings;
use 5.010;

my $new_pass = 'abc123';
my @lines = ( "pwd = abc", "pwd=abc", "password=def", "name= Mike" );

my @match = qw'pwd password';
my $match = '(?:'.join( '|', @match ).')';

say for @lines;
say '';

s/$match \s* = \K .* /$new_pass/x for @lines;
# which is essentially the same as:
# s/($match \s* =) .* /$1$new_pass/x for @lines;

say for @lines;
like image 40
Brad Gilbert Avatar answered Mar 05 '23 16:03

Brad Gilbert