in reply to Use of uninitialized value in substitution iterator in variable expansion

Because you're executing twice (/eeg), you need to delay your defined test past the first time you expand $1:
$line =~ s/(\$\w+)/"defined $1 ? $1 : ''"/eeg;
should work. The first e will expand it to a statement, and the second will evaluate the statement.

Caution: Contents may have been coded under pressure.
  • Comment on Re: Use of uninitialized value in substitution iterator in variable expansion
  • Download Code

Replies are listed 'Best First'.
Re^2: Use of uninitialized value in substitution iterator in variable expansion
by gam3 (Curate) on Apr 07, 2005 at 16:37 UTC
    or
    $line =~ s/(\$\w+->{\w+})/"$1 or ''"/eeg; # or $line =~ s/(\$\w+->{\w+})/eval "$1" or ''/eg; # or my favorite $line =~ s/(\$\w+->{\w+})/eval "$1" or print(STDERR "$1 is not defined\n"), ''/eg;
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re^2: Use of uninitialized value in substitution iterator in variable expansion
by jaco (Pilgrim) on Apr 07, 2005 at 16:30 UTC

    This gives me the same warning. I thought it should work as well, though i wrote it like this with and without parans around the defined statement as mentioned above. Curious

    for my $line (@cfile){ $line =~ s/(\$\w+->{\w+})/defined $1 ? $1 : ''/eeg; $line =~ s/(\$\w+)/defined $1 ? $1 : ''/eeg; $text .= $line; }
      Quotation marks. I was suggesting you use quotation marks.
      #!perl use strict; use warnings; my $foo = undef; # No warning my $line = 'substitute for $foo in here'; $line =~ s/(\$\w+)/"defined $1 ? $1 : ''"/eeg; print $line, "\n"; # Warning $line = 'substitute for $foo in here'; $line =~ s/(\$\w+)/defined $1 ? $1 : ''/eeg; print $line, "\n";

      Caution: Contents may have been coded under pressure.

        I understood what you meant, i should have been more clear in my reply. I think i may have discovered part of the problem though. I'll look into it and get back to you

        It looks like i had a scope problem a little further up in the code. the only reason i noticed it was because i attempted to run your code with the addition on an undelcared var. i.e.

        #!/usr/bin/perl use strict; use warnings; my $foo = undef; # No warning my $line = 'substitute for $foo in $bar here'; $line =~ s/(\$\w+)/"defined $1 ? $1 : ''"/eeg; print $line, "\n";
        hairbear% ./test2.pl Use of uninitialized value in substitution iterator at ./test2.pl line + 11. substitute for in here

        Edit by tye: remove PRE tags around long lines