Posted by matt (matt), 11 March 2003I see
quite a lot in code, can you give me a brief explanation?
Thanks in advance
p.s. what does the -w do after #!/usr/bin/perl -w
Posted by admin (Graham Ellis), 12 March 2003There 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.
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:
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
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
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho