X hits on this document

PDF document

Running and Debugging Perl - page 13 / 30





13 / 30

Running and Debugging Perl

or on the command line like this:

>perl -w program.plx

This allows us to change Perl's behavior either when writing the program or when running it. Some switches can only be used on the command line. By the time perl has opened and read the file, it may be too late to apply the behavior. This is most clearly illustrated in the case of the –e switch, which we'll be taking a look at next.

There are two major types of switch: those that take an argument and those that do not. -w does not take an argument, and neither does -c. (We'll see what -c does very soon.) If you want to specify both switches, you can either put them one after the other, -w -c, or combine them in a cluster, by saying -wc.

For switches that take an argument, such as -i, the argument must directly follow the switch. So, while you can combine -w, -c, and -i00 as -wci00, you may not say -i00wc, as the wc will be interpreted as part of the argument to -i. You must either put switches that take an argument at the end of a cluster or separate them entirely.

  • -


The most commonly used switch is -e. This may only be used on the command line, because it tells perl not to load and run a program file but to run the text following –e as a program. This allows you to write quick Perl programs on the command line. For example, the very first program we wrote can be run from the command line like this:

>perl -e 'print "Hello world\n";' Hello world >

Notice that we surround the entire program in single quotes. This is because, as we saw when looking at @ARGV, the shell itself splits up the arguments on the command line into separate words. Without the quotes, our program would just be print, with "Hello world\n" as the first element of @ARGV.

There are two problems with this. First, we can't put single quotes inside our single quotes, and second, some operating systems' shells prefer you to use double rather than single quotes around your program. They then have differing degrees of difficulty coping with quotes in the program.

For instance, DOS, Windows and so on, will want to see this:

>perl -e "print \"Hello world\n\";"

You can get around most of this by judicious use of the q// and qq// operators. For instance, you could say perl -e 'print qq/Hello world\n/;', which easily translates to a DOS-acceptable form as perl -e "print qq/Hello world\n/;". Note that on UNIX systems, single quotes are usually preferable, as they prevent the shell interpolating your variables.

In the following examples, we'll be showing examples in single-quoted format. If you're using Windows, just convert them to double-quoted format as described above.


Document info
Document views104
Page views104
Page last viewedThu Jan 19 15:51:45 UTC 2017