next up previous contents
Next: Example uses of command-line Up: Perl Command-Line Options Previous: How Are the Options

The Command-line Options

Below we provide a short description of each command-line option used with Perl.

-0
-- Lets you specify the record separator ($/) as an octal number. For example, -0055 will cause records to end on a dash. If no number is specified, records will end on null characters. The special value of 00 will place Perl into paragraph mode. And 0777 will force Perl to read the whole file in one shot because 0777 is not a legal character value.
-a
-- This option must be used in conjunction with either the -n or -p option. Using the -a option will automatically feed input lines to the split function. The results of the split are placed into the @F variable.

-c
-- This option lets you check the syntax of your script without fully executing it. The BEGIN blocks and use statements are still executed because they are needed by the compilation process.

-d
-- This option lets you start the Perl debugger. See the Chapteron Debugging Perl for more information.

-D
-- This option lets you turn on different behaviors related to the debugging process. The following table shows you the sub-options that can be used. Please note, however, that not all releases of Perl can use this feature. For example, the hip port of Perl for Win32 can't. If your version of Perl does not have this option, you will see the message Recompile perl with -DDEBUGGING to use -D switch when you try it. If you want to watch your script as it executes, use -D14. Following is a list of the other values that you can use. You can add the numbers together to specify more than one behavior (such as 8+4+2 = 14) or you can use the letters.

1 p Tokenizing and Parsing
2 s Stack Snapshots
4 l Label Stack Processing
8 t Trace Execution
16 o Operator Node Construction
32 c String/Numeric Conversions
64 P Print Preprocessor Command for -P
128 m Memory Allocation
256 f Format Processing
512 r Regular Expression Parsing
1024 x Syntax Tree Dump
2048 u Tainting Checks
4096 L Memory Leaks (not supported any more)
8192 H Hash Dump - usurps values()
16384 X Scratchpad Allocation
32768 D Cleaning Up

-e
-- The option lets you specify a single line of code on the command line. This line of code will be executed in lieu of a script file. You can use multiple -e options to create a multiple line program-although given the probability of a typing mistake, I'd create a script file instead. Semi-colons must be used to end Perl statements just like a normal script.

-F
-- This option modifies the behavior of the -a option. It lets you change the regular expression that is used to split the input lines. For example, -F /:+/ will split the input line whenever one or more colons are found. The slashes are optional; they simply delimit the pattern if they are there. I use them for their aesthetic value.

-I
-- This option lets you edit files in-place. It is used in conjunction with the -n or -p option. See "Example: Using the -i option" for more information.

-I
-- This option is used in conjunction with the -P option. It tells the C preprocessor where to look for include files. The default search directories include /usr/include and /usr/lib/Perl.

-l
-- This option turns on line-ending processing. It can be used to set the output line terminator variable ($/) by specifying an octal value. See "Example: Using the -0 option" for an example of using octal numbers. If no octal number is specified, the output line terminator is set equal to the input line terminator (such as

$\ = $/;).

-n
-- This option places a loop around your script. It will automatically read a line from the diamond operator and then execute the script. It is most often used with the -e option.

-p
-- This option places a loop around your script. It will automatically read a line from the diamond operator, execute the script, and then print $_. It is most often used with the -e option.

-P
-- This option will invoke the C preprocessor before compiling your script. This might be useful if you have some C programming experience and would like to use the #include and #define facility. The C preprocessor can also be used for conditional compilation. Use the -I option to tell Perl where to find include files.

-s
-- This option lets you define custom switches for your script. See "Examples: Using the -s Option" for more information.

-S
-- This option makes Perl search for the script file using the PATH environment variable. It's mostly used with UNIX systems that don't support the #! Line. The docs/perlrun.htm documentation file that comes with your Perl distribution has more information about this option.

-T
-- This UNIX-based option turns on taint checking. Normally, these checks are only done when running setuid or setgid. The docs/perlsec.htm documentation file that comes with your Perl distribution has more information about this option. -uThis UNIX-based option will cause Perl to dump core after compiling your script. See the Perl documentation that came with your Perl distribution for more information.

-U
-- This UNIX-based option will let Perl do unsafe operations. Its use is beyond the scope of this book.

-v
-- This option will display the version and patchlevel of your Perl executable. -w This option prints warnings about unsafe programming practices. See Chapter on Debugging Perl for more information.

-x
-- This option will let you extract a Perl script from the middle of a file. This feature comes in handy when someone has sent you a script via e-mail. Perl will scan the input file looking for a #! line that contains the word perl. When it is found, it will execute the script until the __END__ token is found. If a directory name is specified after the -x option, Perl will switch to that directory before executing the script.

As you can see, Perl has quite a few command-line options. Most of them are designed so that you can do useful things without needing to create a text file to hold the script. If you are a system administrator then these options will make you more productive. You'll be able to manipulate files and data quickly and accurately. If you're looking to create applications or more complicated programs, you won't need these options-except for -w and -d.

The rest of the chapter is devoted to demonstrating the -0, -n, -p, -i, and -s options.


next up previous contents
Next: Example uses of command-line Up: Perl Command-Line Options Previous: How Are the Options
dave@cs.cf.ac.uk