in reply to Use of uninitialized value in concatenation

The minimum the regex can match is a $ because the second capture is optional. If there are no \w characters following a $ character then the second capture will be undef and you get the warning. There are two ways you can fix it depending on the desired result. Using

(\$(\w+))

prints '$Word $' for the sample line, and

(\$(\w*))

prints '$Word dollar'.


True laziness is hard work

Replies are listed 'Best First'.
Re^2: Use of uninitialized value in concatenation
by gem555 (Acolyte) on Jun 11, 2009 at 05:42 UTC
    For the example like this
    #!/usr/bin/perl -w use strict; my %words = map { $_ => 1 } qw($Word $name); my $line = '$Word $ $1'; $line =~ s/(\$(\w+)?)/exists $words{$1} ? $1 : "dollar$2"/eg; print "$line\n\n";
    It gives a warning message
        The "$testing" is replaced with "dollar" with the below code
        #!/usr/bin/perl -w use strict; use warnings; my %words = map { $_ => 1 } qw($Word $name $test); my $line = '$Word $ $ $ $test $testing'; $line =~ s/(\$\w*)/exists $words{$1} ? $1 : " dollar "/eg; print "$line\n\n"
        The output which i need is if the values are not present in array, then $Word dollar dollar dollar $test dollartesting