I was under the impression that using the strict pragma and declaring variables with my was a good thing.

However, I have found that using "my" actually prevents warnings about variable names that are used only once being displayed.

Compare the following code examples:-

#!/Perl/bin/perl -w use CGI::Carp qw(fatalsToBrowser warningsToBrowser); # use strict; use warnings; print "Content-type: text/html\n\n"; warningsToBrowser(1); $blah = 1; print "No problemo\n";

The above code generates the following warning message (in an HTML comment):-

warning: Name "main::blah" used only once: possible typo at c:\test\test.pl line 7.

#!/Perl/bin/perl -w use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use strict; use warnings; print "Content-type: text/html\n\n"; warningsToBrowser(1); my $blah = 1; print "No problemo\n";

The above code generates no warnings - even if I change "my $blah = 1" to just "my $blah" thereby not even giving it a value.

I realise that any real variable name typos would most likely be caught if using strict with "my" because they show up as undeclared, but how can you catch variables that are declared and not used?

Also, is there a reliable way of making a Perl CGI program run in an "ultra pedantic" mode so that errors such as using a string as a number or vice versa are caught? (The above settings don't do that.)

__________
"Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction which doesn't work." -- (Author Unknown)


In reply to Using "my" suppresses "Name used only once" warning? by Wysardry

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.