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 | [reply] [Watch: Dir/Any] |
|
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?
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
|
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
|
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
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.) | [reply] [Watch: Dir/Any] |
|
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 | [reply] [Watch: Dir/Any] [d/l] |
|
-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....
| [reply] [Watch: Dir/Any] |
|
Re: use strict
by KM (Priest) on Jun 07, 2000 at 19:17 UTC
|
-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 | [reply] [Watch: Dir/Any] |
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/
| [reply] [Watch: Dir/Any] |