Using 'use warnings;' and 'use strict;' will often give you tons of messages telling you that something's wrong. It's natural to want to avoid such discouraging messages, but these two instructions (called 'pragmas') will identify certain problems for you long before you get into trouble from them. They're something like a smoke alarm. You don't just take the batteries out of a smoke alarm when it sounds, you go find out what caused it to go off. Similarly, when you get messages from using strict and warnings, they are telling you that your code has problems. It's usually better to track down the problems, than to ignore them by removing the pragmas.

Your error messages all refer to 'explicit package name', which is somewhat confusing. The problem is that you're using these variables without telling Perl anything about them - such as where you want them to exist. Perl allows you to do this, but as your programs get longer, this will cause problems. It is things like this that 'warn' and 'strict' help to avoid.

For example, it is often convenient to use $i or $j as a counter or an index into an array. However, if you have several arrays, all using $i, you'll have problems if that same $i is visible and accessible to all the arrays. You need to tell Perl where each $i is allowed to be seen, or else you need to remember the value and location of all your variables, which becomes extremely difficult.

The magic word for declaring variables to be lexically scoped (visible within a block of code, or a sub) is 'my'. It is a good idea to declare your variables close to where you're using them, and to also try to limit their scope as much as possible in order to avoid clashes with other variables with the same name. So, for example, you could do this:

my $i=100; # Some code here... ... sub_1 { my $i; # This $i is undefined. It's not the same $i that was defined +outside the sub. # Code that uses $i ... } # $i now = 100, as before, since we're not in the sub. sub_2 { my $i = 4; # This $i = 4. print "$i\n"; # Prints 4. } # Once again, $i = 100.

As your programs get longer, 'warnings' and 'strict' become more important in keeping you sane. Without them, it is very easy to introduce subtle, hard to find bugs that could take many hours to track down. It's a good idea to use these pragmas consistently, unless you have a very good reason to ignore them. And then it's still a good idea to disable them only for the smallest bit of code nesessary, and to enable them again when you're past that bit.


In reply to Re: Declaring variables under 'use strict' by spiritway
in thread Declaring variables under 'use strict' by Yoda_Oz

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.