Reputation: 3222
I have a script which is working fine in my local system (Cygwin in Windows 10).
But when I run same in Linux machine x86_64 GNU/Linux this shows below error:
Bareword found where operator expected at script.pl line 22, near "s/$regex/$1,/rg"
syntax error at script.pl line 22, near "s/$regex/$1,/rg"
Execution of script.pl aborted due to compilation errors.
Here is my script:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $site_name = $ARGV[0];
my $type_of_site = $ARGV[1];
chomp($site_name);
chomp($type_of_site);
my $var = `sh shell_script.sh $site_name $type_of_site`;
#The above shell script gives me following data in $var
#"Result data [[The Incident result is shown with Node and IP address. error.log warning.log http://10.0.0.11/home/node_data/2020-07-08_data.txt NODE IP NODE1 10.0.0.1 NODE2 10.0.0.2 NODE3 10.0.0.3 NODE4 10.0.0.4 NODE5 10.0.0.5 ]]";
print $var;
my $regex = qr/.*?(?P<Node>\w+)\s(?:(?:\d{1,3}\.){3}\d{1,3}) ?]?]?/mp;
my $result = $var =~ s/$regex/$1,/rg;
chop $result;
my @nodes_list = split /,/, $result;
print Dumper(\@nodes_list);
I am extracting node names from the shell script resultant data using regex. But why its showing the error when I run it in Linux environment?
Upvotes: 2
Views: 1710
Reputation: 52419
my $result = $var =~ s/$regex/$1,/rg;
I don't have a perl version that old to test with, but the r
modifier was added in 5.14. If you're using 5.10, I bet that's the cause of the error you're seeing - and it would explain why it's working fine on newer versions. Perl 5.10 was released in December 2007 - there's been a lot of work since then. I'd upgrade if you can, possibly using perlbrew
.
But in the meantime... r
returns a new copy of the transformed string, instead of altering the one the regular expression is bound to. So you might try something like
my $result = $var;
$result =~ s/$regex/$1,/g;
as a workaround.
Upvotes: 7
Reputation: 2341
The /r feature (Non-destructive substitution) you use in line 22
my $result = $var =~ s/$regex/$1,/rg;
was introduced in Perl 5.14.0: perl5140delta.
Upvotes: 1