Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found