Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: use strict

by lhoward (Vicar)
on Jun 07, 2000 at 19:14 UTC ( [id://16873]=note: print w/replies, xml ) Need Help??


in reply to use strict

-w is the same as "use warnings". Warnings turns on optional warnings that may indicate problems with your code. Warnings will not stop your program from running, just send a message when it encounters something. Warnings also tend to be more run-time messages.

"use strict" restricts the use of unsafe constructs. Preventing the code from running if you do "dangerous" things. If something fails the "use strict" test the program will not run.

strict and warnings are well used together as they can help you prevent and catch different types of errors.

Replies are listed 'Best First'.
RE: Re: use strict
by KM (Priest) on Jun 07, 2000 at 19:31 UTC
    I'll take this a step further too and mention the diagnostics pragma (perldoc diagnostics). I don't think it is practical for production use, but can be helpful for new Perl folks when learning and finding that their code doesn't seem to work with -w all of a sudden :) The diagnostics pragma will give verbose warning messages (more verbose than the normal -w warnings). Here is an example:

    #!/usr/bin/perl -w print "Hello;
    When run, this will produce:
    Can't find string terminator '"' anywhere before EOF at ./test.pl line 3.
    

    Ok, this is an easy one to see the error :) I am using it for examples sake! Anyways, now, let's change the script to:

    #!/usr/bin/perl -w use diagnostics; print "Hello;

    Now, this is what you would see:

    Can't find string terminator '"' anywhere before EOF at ./test.pl line 4 (#1)
    
        (F) Perl strings can stretch over multiple lines.  This message means that
        the closing delimiter was omitted.  Because bracketed quotes count nesting
        levels, the following is missing its final parenthesis:
    
            print q(The character '(' starts a side comment.);
    
        If you're getting this error from a here-document, you may have
        included unseen whitespace before or after your closing tag. A good
        programmer's editor will have a way to help you find 
    these characters.
    
    Uncaught exception from user code:
            Can't find string terminator '"' anywhere before EOF at ./test.pl line 4.
    

    Just an aside to hopefully help some folks learn

      To further this a bit more, sometimes it is not possible or desirable to use the diagnostics pragma. All of the diagnostics, with full explanation call also be found by reading perldiag.

      Simply use perldoc perldiag and search for the error message. It sometimes takes a bit of work to find the precise error due to verbage changes, but it is sometimes faster than rerunning things under the 'use diagnostics'.

      Mik
      mikfire

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found