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

#!/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.

Replies are listed 'Best First'.
Re^6: Use of uninitialized value in concatenation
by AnomalousMonk (Archbishop) on Jun 12, 2009 at 15:48 UTC
    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.