in reply to Re^2: Use of uninitialized value in concatenation
in thread Use of uninitialized value in concatenation

#!/usr/bin/perl -w use strict; my %words = map { $_ => 1 } qw($Word $name); my $line = '$Word $ $foo'; $line =~ s/(\$\w*)/exists $words{$1} ? $1 : qq{ dollar }/eg; print $line;
For $foo it prints dollar and not dollarfoo. But still gets a warning message

Replies are listed 'Best First'.
Re^4: Use of uninitialized value in concatenation
by AnomalousMonk (Archbishop) on Jun 11, 2009 at 15:20 UTC
      #!/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$2"/eg; print "$line\n\n";
      The exact code I am using is this. When I execute this : output is
      Use of uninitialized value in concatenation (.) or string at ex.pl lin +e 6. Use of uninitialized value in concatenation (.) or string at ex.pl lin +e 6. Use of uninitialized value in concatenation (.) or string at ex.pl lin +e 6. $Word dollar dollar dollar $test dollartesting
      The problem is with "$2". How to define $2.
        Again, the following code, using a regex from postings elsewhere in this thread, always perfectly defines  $2 (although sometimes it is defined as the empty string – perfectly valid!) and seems to give the required output:
        >perl -wMstrict -le "my %words = map { $_ => 1 } qw($Word $name $test); my $line = '$Word $ $ $ $test $testing'; $line =~ s/(\$(\w*))/exists $words{$1} ? $1 : qq{dollar$2}/eg; print qq{$line}; " $Word dollar dollar dollar $test dollartesting
        Note that  (\$(\w*)) is the regex used.