Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Stricht

Posted by matt (matt), 11 March 2003
I see

use stricht;

quite a lot in code, can you give me a brief explanation?

Thanks in advance

Matt

p.s. what does the -w do after #!/usr/bin/perl -w


Posted by admin (Graham Ellis), 12 March 2003
There are a few features of the Perl language which can be regarded as "accidents waiting to happen" when you're writing longer applications / subroutines / modules, even though they're great with smaller applications.   For example, the automatic creation of varibales (without the need to tell Perl "I want a variable called $xxx") is great in shorter pieces of code, but can create problems (unexpected variables shared between all subroutines appear) if you mis-spell or forget to "my" a variable in a longer application or module.

If you
use strict;
in your code, three extra sets of rules are applied at compile time which enforce extra checking by the compiler, and you'll not be allowed to run code that breaks the rules.  Here's a very short demo program - two versions - with and without "strict" showing you the effect:

Code:
[localhost:~/mar03] graham% cat stopper
#!/usr/bin/perl

use strict;

$demo = 15;
print ("demo has the value $demo\n");

[localhost:~/mar03] graham% cat oopsie
#!/usr/bin/perl

$demo = 15;
print ("demo has the value $demo\n");

[localhost:~/mar03] graham% perl stopper
Global symbol "$demo" requires explicit package name at stopper line 5.
Global symbol "$demo" requires explicit package name at stopper line 6.
Execution of stopper aborted due to compilation errors.
[localhost:~/mar03] graham% perl oopsie
demo has the value 15
[localhost:~/mar03] graham%


Of course, you are going to need variables in your program (!), so you must declare them using my or by giving a full path name (in our example $main::demo) which can hardly be done by accident.   The current and recent versions of Perl also support variable decalarations using the our keyword.   Thus

Code:
[localhost:~/mar03] graham% cat fixed
#!/usr/bin/perl

use strict;

our $demo = 15;
print ("demo has the value $demo\n");

[localhost:~/mar03] graham% perl fixed
demo has the value 15
[localhost:~/mar03] graham%


As well as checking for global variables, strict also checks for symbolic references (where you have a variable that contains the name of another variable), and bear words which would otherwise be assumed to be subroutines;  further details under "man strict". There's a syntax available to let you choose just one or two of the tests in your code, and you can turn strict back off if you wish.

The -w command line option is rather different; it won't stop your code running, but it will give you warnings in some (different to strict) situations.  For example, you'll be warned if a variable name is only used once (a typo perhaps?) or if you use the contents of a variable which hasn't been initialised.   Many authoritys tell you that you should always use "-w" as you write Perl, and it's certainly an excellent way of discovering where to look for errors in a mis-behaving program



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho