Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

use strict

by Ignorance (Monk)
on Jun 07, 2000 at 19:01 UTC ( #16866=perlquestion: print w/ replies, xml ) Need Help??
Ignorance has asked for the wisdom of the Perl Monks concerning the following question:

If any of you have the Learning Perl book there is a program exercise on page 28 called "Say Hello"
I have added the -W and use strict; but now I see a slew of error messages.
Is it possible that the exercise in question can't stand up to this level of scrutiny, or should I go back through and fix my typos?

Also, what is the differenc between -W and use strict? Can they actualy cause a script to not work?

Thanks, Ignorance

Comment on use strict
RE: use strict
by jjhorner (Hermit) on Jun 07, 2000 at 19:05 UTC

    Well, when you add the "-w" option it will warn you of undeclared variables. Since you didn't declare variables, you get warnings. If you have a lot of errors, 'use strict' is stoping the interpretation.

    'use strict' forces you to predeclare your variables. your script won't run without predeclaring with 'use strict'.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
Re: use strict
by chromatic (Archbishop) on Jun 07, 2000 at 19:09 UTC
    - W (probably in - X) actually exists, contrary to something someone said in the Chatterbox earlier. It's a filetest operator. It tests to see if a file is writeable by the real UID/GID. Strict enforces good Perl habits, like not using symbolic references and things like that.

    If you used that instead of -w, it's a typo. If not, what everyone else said is correct.

    To see what kind of options you can feed Perl on the command line or the shebang line, see perlrun. (Note that the version on my box for 5.005_3 doesn't list anything for -W.)

      To be more specific, -W tests to see if a file is writable by the real uid/gid. It would be used like:

      if (-W $file) { ... do happy stuff ... }else{ ... do unhappy stuff ... }

      Whereas -w is a switch used when running Perl to add a behaviour to the interpreter as it compiles and runs your code. You can see about the file test swicthes by typing:
      perldoc -f -X

      However, there is a -W switch for Perl 5.6 to turn on all warnings.

      Cheers,
      KM

        -W used on the perl command line has a different meaning. perl -W enables all warnings. perl -w enables many useful warnings. At least in perl 5.6 it does....
Re: use strict
by lhoward (Vicar) on Jun 07, 2000 at 19:14 UTC

    -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.

      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

Re: use strict
by KM (Priest) on Jun 07, 2000 at 19:17 UTC
    From perlrun:

    -w
    prints warnings about variable names that are mentioned 
    only once, and scalar variables that are used before being 
    set. Also warns about redefined subroutines, and references 
    to undefined filehandles or filehandles opened read-only 
    that you are attempting to write on. Also warns you if you 
    use values as a number that doesn't look like numbers, 
    using an array as though it were a scalar, if your 
    subroutines recurse more than 100 deep, and innumerable 
    other things. 
    
    
    You can disable specific warnings using __WARN__ hooks, as 
    described in perlvar and perlfunc. See also perldiag and 
    perltrap. 
    
    

    The strict pragma is used to restrict unsafe constructs. There are 'refs', 'vars', and 'subs'. You can simply do 'perldoc strict' to see the POD, but in a nutshell, 'refs' makes sure you don't use symbolic references, 'vars' ensures you always predeclare your variables (and are declared in scope), and 'subs' makes sure you do not use a bareword unless it is in curlies {}, or is on the left side of =>. Likely, the example code does something that either -w or strict doesn't like. You can turn off one of the three pieces of strict like so:

    no strict 'refs';

    For example. Sometimes this comes up when some module isn't very strict friendly (sometimes I run across this when using filehandles for certain graphic manipulation modules). If you have warning turned on, and are about to do something that is not -w friendly, you can turn them off with:

    $^W = 0;

    and back on by setting it back to 1. I hope this quick rundown helps a bit.

    Cheers,
    KM

RE: use strict
by merlyn (Sage) on Jun 07, 2000 at 20:34 UTC
    The first examples in Learning Perl concentrate more on the feel of the language, and not on the tools that help you write better programs more quickly. Keep reading, and don't use stuff from later chapters on earlier code. You're trying to be too smart too early. {grin}

    -- Randal L. Schwartz, Perl hacker

      Is there any other book for learning Perl that you could recomend?
      Are there errors or perhaps bad practices in the O'reilly Learning Perl book that I should be aware of?
        O'Reilly Learning Perl is the book to use for learning perl.

        If you want to talk about all the errors and bad practices expressed in that book ask merlyn, I'm sure he'd have some interesting comments for you :)

        Blasphemer!

        Learning Perl is infallible! (and version 3 will be even more infallible than the previous versions)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://16866]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-07-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls