The first line of each of our Perl programs so far has been the line
#!/usr/bin/perl
This line is actually a comment, but it tells the underlying Unix system where the Perl interpreter is located. This line can be used in non-Unix systems as well without any problems although the line is treated in such systems as simply comment. However, it provides for easier portability across systems. Of course, we should specify where the Perl interpreter is located on the system. In this first line of comment, it is possible to specify arguments to modify the behavior of the Perl interpreter. These arguments are single letters and are preceded by the minus (-) sign. Such arguments are called switches. Perl has many command-line switches. We will look at just a few of them here.
The -h switch does not run the Perl program, but prints all the switches that are available. Similarly, the -v option, instead of running the program, tells us what version of Perl we are running. If we do not want to execute the program, but just check if it is syntactically correct, we use the -c switch. In such a case, the first line of the program should be
#!/usr/bin/perl -c
Another useful switch is the -w option. If we put
#!/usr/bin/perl -w
as the first comment of our Perl code, the Perl compiler gives us a lot more warning than usual. For example, it will complain about scalar variables that are used before initialization. It will complain about subroutines that are redefined, and about filehandles that are used improperly, e.g., a read filehandle being used for writing. There many other errors that Perl may otherwise ignore, but will check for us if we use the -w option.
If we are having logical problems running our code that is syntactically correct, we can use the -d switch to run the Perl code under the debugger.
#!/usr/bin/perl -d
The debugger will enable us to do many useful things to isolate our problem. We can single step through the program if we like. In single stepping, we run one instruction at a time. We can look for the presence of a certain pattern or regular expression in the code, both forwards and backwards from the current line of code. If we are inside a subroutine, we can force to return from the subroutine. We can set breakpoints anywhere in the code. A breakpoint is a point in the code where the program execution stops, waiting for us to restart. We can perform various exploratory actions at a breakpoint before proceeding again. We can examine the value of a variable at a certain point in execution. We can do a lot of other things in our attempt to find the problem or problems why our code is not giving the expected results.
A very interesting switch is the -n switch. It is there for the convenience of a programmer who wants to write a program that takes a number of files as command line argument and runs the program on all the files. We have seen how to do this in the section of pattern matching. We can include the programÕs code inside a while loop that has (<ARGV>) or equivalently (<>) as the conditional. Since this seems to happen quite frequently, Perl provides us with the -n switch. When this switch is used, Perl automatically wraps the code provided within a while loop with <ARGV> as the conditional.
So, when we run the following program with a command line argument * in Unix representing all files in the current directory, the program is run on all files.
Program 1.22
#!/usr/bin/perl -n
#Takes a list of files and prints all those lines in
#the files that contain the vowels in order.
if (/a.*e.*i.*o.*u/i){
print;
}
The program is run as if the actual code is
while (<>){
if (/a.*e.*i.*o.*u/i){
print;
}
}
We can use more than one switch at the same time. For example, if we want to use the -w, -n and -d switches at the same time, we can write
#!/usr/bin/perl -w -n -d
where the three switches can be written in any order. Equivalently we can put two or more switches together and precede them by just one minus (-) sign. If we want to state all three switches together, we can write
#!/usr/bin/perl -wnd