in reply to Re: about scope and s///eeg
in thread about scope and s///eeg

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");

Replies are listed 'Best First'.
Re: Re: Re: about scope and s///eeg
by shenme (Priest) on Sep 12, 2003 at 19:04 UTC
    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 .