> On Jun 5, 6:34 am, sharma
@hotmail.com wrote:
> > #############################
> > 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
Thanks a lot Paul for your explanation! Your suggested fix runs perfect.