/e option in the s/// command

S

sharma__r

#############################
use strict;
use warnings;

my $var = "''''''''''''";

my $p_pulse = q{./\\};
my $n_pulse = q{'\\/};

my $knt = 1;

print $var;
$var =~ s/^((...){$knt}).../$1 . (($1 =~ m{'}) ? $n_pulse : $p_pulse)/
e; ## <---- offending line
print $var;
##########################

when I run the above piece of perl code i get the following warnings:
Use of uninitialized value in concatenation (.) or string at
line 12.
And when i change the $var to "..........." it works fine.

what could be the problem here?

TIA,
-rakesh
 
P

Paul Lalli

#############################
use strict;
use warnings;

my $var = "''''''''''''";

my $p_pulse = q{./\\};
my $n_pulse = q{'\\/};

my $knt = 1;

print $var;
$var =~ s/^((...){$knt}).../$1 . (($1 =~ m{'}) ? $n_pulse : $p_pulse)/
e; ## <---- offending line
print $var;
##########################

when I run the above piece of perl code i get the following warnings:
Use of uninitialized value in concatenation (.) or string at
line 12.
And when i change the $var to "..........." it works fine.

what could be the problem here?

The $1, $2, $3 <etc> variables are set after every successful pattern
match. In your replacement, you are doing a pattern match. That
pattern match succeedes, because the current value of $1 does indeed
match m{'}. That successful match causes $1 to be set to undef,
because that pattern match doesn't have any capturing parentheses. So
it is the first $1 listed in your replacement - the one that's
concatenated to the result of the ?: operator - that's undefined.

It worked fine when you changed $var to all periods because then the
inner pattern match was not successful and so $1 wasn't changed.

You can fix this by saving off the value of $1 in your replacement, as
follows:

$var =~ s/^((...){$knt}).../my $save = $1; $save . (($save =~ m{'}) ?
$n_pulse : $p_pulse)/e;

Hope this helps,
Paul Lalli
 
S

sharma__r

The $1, $2, $3 <etc> variables are set after every successful pattern
match. In your replacement, you are doing a pattern match. That
pattern match succeedes, because the current value of $1 does indeed
match m{'}. That successful match causes $1 to be set to undef,
because that pattern match doesn't have any capturing parentheses. So
it is the first $1 listed in your replacement - the one that's
concatenated to the result of the ?: operator - that's undefined.

It worked fine when you changed $var to all periods because then the
inner pattern match was not successful and so $1 wasn't changed.

You can fix this by saving off the value of $1 in your replacement, as
follows:

$var =~ s/^((...){$knt}).../my $save = $1; $save . (($save =~ m{'}) ?
$n_pulse : $p_pulse)/e;

Hope this helps,
Paul Lalli



Thanks a lot Paul for your explanation!

Your suggested fix runs perfect.

Best regards,
-rakesh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top