in reply to about scope and s///eeg

Your example as given is perhaps not what you're really running?   Fixing the example is as simple as adding
$var_one = 'now defined';
after the my $var_one;.

Replies are listed 'Best First'.
Re: Re: about scope and s///eeg
by Anonymous Monk on Sep 12, 2003 at 15:26 UTC
    Oops. The global variable is really initialized when defined, as in:
    my $var_one = "a string"; sub copy_file { my $f = shift; open(IFILE, "<$f"); while($x = <IFILE>) {$x =~ s|(\$\w+)|$1|eeg; print $x}; close(IFILE); } copy_file("afile");
      But then you still get warnings?   What I tested with was:
      #!/usr/bin/perl -w use strict; use warnings; my $var_one; $var_one = 'now defined'; sub copy_file { while( my $x = <DATA>) { $x =~ s|(\$\w+)|$1|eeg; print $x; }; } copy_file(); __DATA__ The variable is $var_one.
      As coded above I get the output
      The variable is now defined.
      
      If I comment out the   $var_one =   I see
      Use of uninitialized value in substitution iterator at anon02.pl line 12, <DATA> line 1.
      The variable is .
      
      Any chance the variable name is misspelled in the text file?   When I tried the following data:
      __DATA__
      The variable is $var_one.
      The variable is $var_one2.
      The variable is $Var_one.
      
      I got error messages:
      The variable is now defined.
      Use of uninitialized value in substitution iterator at anon02.pl line 14, <DATA> line 2.
      The variable is .
      Use of uninitialized value in substitution iterator at anon02.pl line 14, <DATA> line 3.
      The variable is .