($year, $month, $day) = Today(); # Line 7
Global symbol "$year" requires explicit package name at ./date_calc_test.pl line 7.
When you're using strict you have to let Perl know what the scope of each variable is. Variables declared with the keyword "my" exist from when you declare them until the end of their current block. A block is the space between matching curly braces, or, if they are outside of all curly braces, the file.
So in the following we have a few blocks
# Start block 0
use strict;
my $foo; # exists from here to the end of the file
{ # Start block 1
my $a; # exists from here to end of block 1
{ # Start block 2
my $b; # exists from here to end of block 2
print $b;
} # End block 2: $b stops existing here
# print $b; # (would fail, no $b can be found)
$a++;
} # End block 1: $a stops existing here
# print $a; # (would fail, no $a can be found)
# print $b; # (would fail)
print $foo; # successful
# End of file $foo stops existing here.
Note that the commented out prints would work if you took away "use strict". This is because Perl would ASSUME that you intended to print the value of the (non-existant) global $a or $b at the points where the lexical (declared with "my") variables didn't exist. That it doesn't exist isn't a problem, Perl will automatically bring it into existance just for you.
This is a problem if you do something like the following:
my @friends = ("Simon", "Howard", "Jane", "Jacinta");
print $freind[3];
because Perl will very happily print you the third element of a non-existant list. Strict is very helpful at picking up spelling mistakes and errors like this.
When we're using strict, Perl still assumes that variables not declared with "my" must be referring to the global ones. Global variables can be accessed from any block regardless of where (how many blocks in) they were first mentioned. They can also be accessed from outside the file. However, strict still requires you to tell it that you want variables to be global (if you do, which you probably don't). This can be done with the "our" keyword.
As I said in my earlier post, by adding the word "my" next to the first times you used your variables your script would have become strict compliant.
($year, $month, $day) = Today(); # Line 7
$Dd = -1;
becomes:
my ($year, $month, $day) = Today(); # Line 7
my $Dd = -1;
If you have any questions about strict, feel free to ask.
All the best,
jarich |