Switches

 < Day Day Up > 

Perl has a variety of command-line options (switches) that can be used to change one or more aspects of Perl's behavior. Some switches will change how input data is handled (such as end-of-line characters), whereas others toggle Perl's internal behavior (such as to turn on debugging). These switches can appear on the command line or can be placed on the #! line at the beginning of the Perl program.

NOTE

Several command-line switches can be stacked together, so -pie is the same as -p -i -e.


The following list describes all available command-line switches:

-0[digits] Specifies the input record separator ($/) as an octal number. $/ is usually a newline, so you get one line per record. For example, if you read a file into an array, this gives you one line per array element. The value 00 is a special case and causes Perl to read in your file one paragraph per record.

-a Turns on Autosplit mode when used with an -n or -p. That means each line of input is automatically split into the @F array.

-C Enables native support and system interfaces for wide characters (multibyte character sets).

-c Tells Perl to perform syntax checking on the specified Perl program without executing it. This is invaluable, and the error messages given are informative, readable, and tell you where to begin looking for the problem, which is a rarity in error messages.

-Dflags Sets debugging flags. See perlrun for more details.

-d Runs the script under the Perl debugger. See perldebug for more information.

-d:foo Runs the script under the control of a debugging or tracing module installed as Devel::foo. For example, -d:Dprof executes the script using the Devel::DProf profiler. See perldebug for additional information on the Perl debugger.

-e commandline Indicates that what follows is Perl code. This enables you to enter Perl code directly on the command line, rather than running code contained in a file:

 $ perl -e 'print join " ", keys %ENV;' 

-Fpattern Specifies the pattern to split on if -a is also in effect. This is " " by default, and -F enables you to set it to whatever works for you, such as ',' or ';'. The pattern can be surrounded by //, "", or ''.

-h Typing perl -h lists all available command-line switches.

-Idirectory Directories specified by -I are prepended to the search path for modules (@INC).

-i[extension] This indicates that files are to be edited in place. If the extension is provided, the original is backed up with that extension. Otherwise, the original file is overwritten.

-l[octnum] Enables automatic line-ending processing, which means that end-of-line characters are automatically removed from input and put back on to output. If the optional octal number is unspecified, this is just the newline character.

-m[-]module or -M[-]module Loads the specified module before running your script. There's a subtle difference between m and M. See perlrun for more details.

-n Causes Perl to loop around your script for each file provided to the command line. Does not print the output. The following example, from perlrun, deletes all files older than a week:

 $ find . -mtime +7 -print | perl -nle 'unlink;' 

-P This causes your script to be run through the C preprocessor before compilation by Perl.

-p This is just like -n except that that each line is printed.

-S This searches for the script using the PATH environment variable.

-s This performs some command-line switch parsing and puts the switch into the corresponding variable in the Perl script. For example, the following script prints '1' if run with the -foobar switch. Your Perl code might look like this:

 #!/usr/bin/perl -s print $foobar; 

When you execute it, you would enter

 $ ./myperl -foobar 

-T This enables taint checking. In this mode, Perl assumes that all user input is tainted, or insecure, until the programmer tells it otherwise. This helps protect you from people trying to exploit security holes in your code and is especially important when writing CGI programs.

-U This enables you to do unsafe things in your Perl program, such as unlinking directories while running as superuser.

-u An available, but obsolete switch to tell Perl to dump core after compiling a program.

-V This prints a summary of the major Perl configuration values and the current value of @INC.

 -V:names 

This parameter displays the value of the names configuration variable.

-v This prints the version and patch level of your Perl executable. For example,

 $ perl -v This is perl, v5.8.4 built for i386-linux-thread-multi (with 1 registered patch, see perl -V for more detail) Copyright 1987-2003, Larry Wall \'98 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using `man perl' or `perldoc perl'.  If you have access to the Internet, point your browser at http://www.perl.com/, the Perl Home Page. 

-W This tells Perl to display all warning messages.

-w This tells Perl to display warning messages about potential problems in the program, such as variables used only once (might be a typo), using = instead of == in a comparison, and the like. This is often used in conjunction with the -c flag to do a thorough program check.

 $ perl -cw finalassignment.pl 

-X This tells Perl to disable all warning messages.

-x directory This tells Perl that the script is embedded in something larger, such as an email message. Perl throws away everything before a line starting with #!, containing the string 'perl', and everything after _END__. If a directory is specified, Perl changes to the directory before executing the script.

NOTE

The Perl documentation is referred to a few times in this section. perldebug, perlrun, perlmod, and perlmodlib, for example, are documents from the Perl documentation. To see these documents, just type perldoc perlmodlib at the shell prompt. You can also see all the Perl documents online at http://www.cpan.org/doc/index.html or on any CPAN site. CPAN is the Comprehensive Perl Archive Network. See the "Modules and CPAN" section later in this chapter for more information.

Perl documentation is written in POD (plain old documentation) format and can be converted into any other format, such as TeX, ASCII, or HTML, with the pod2* tools that ship with Perl. For example, to produce HTML documentation on the Fubar module, you would type pod2html Fubar.pm > Fubar.html.


     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net