($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


In reply to Using strict by jarich
in thread Values of day and month in two digets by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.