RPrinceton
Programmer
Hi,
I am new to CGI and learning about many "quirks" associated with the language. The latest one deals with using the 'my' operator. My understanding of the 'my' operator is to simply make a variable local to a subroutine. It appears that it has much deeper implications e.g.,
The following code yields "abc and "def" written in my log file:
open (LOG, ">>log.txt"
$var1 = "abc";
$var2 = "def";
for(1..2){
$varname = "var".$_ ;
print LOG ${"$varname"},"\n";
}
close (LOG);
...while the following code yields empty values written in my log file by just adding the 'my' operator:
open (LOG, ">>log.txt"
my $var1 = "abc";
my $var2 = "def";
for(1..2){
my $varname = "var".$_ ;
print LOG ${"$varname"},"\n";
}
close (LOG);
Why is this any different?
The book I am learning from i.e., "Learning Perl" by O'Reilly does not go into the 'my' operator in any depth. It is imperative that I understand it thoroughly since I see it used all over the place. Any light is appreciated. Thanks in advance.
Regards,
Randall Princeton
I am new to CGI and learning about many "quirks" associated with the language. The latest one deals with using the 'my' operator. My understanding of the 'my' operator is to simply make a variable local to a subroutine. It appears that it has much deeper implications e.g.,
The following code yields "abc and "def" written in my log file:
open (LOG, ">>log.txt"
$var1 = "abc";
$var2 = "def";
for(1..2){
$varname = "var".$_ ;
print LOG ${"$varname"},"\n";
}
close (LOG);
...while the following code yields empty values written in my log file by just adding the 'my' operator:
open (LOG, ">>log.txt"
my $var1 = "abc";
my $var2 = "def";
for(1..2){
my $varname = "var".$_ ;
print LOG ${"$varname"},"\n";
}
close (LOG);
Why is this any different?
The book I am learning from i.e., "Learning Perl" by O'Reilly does not go into the 'my' operator in any depth. It is imperative that I understand it thoroughly since I see it used all over the place. Any light is appreciated. Thanks in advance.
Regards,
Randall Princeton