From: Subject: PERL -- Options Date: Fri, 21 Feb 2003 13:26:41 +0100 MIME-Version: 1.0 Content-Type: text/html; charset="windows-1250" Content-Transfer-Encoding: quoted-printable Content-Location: http://www-2.cs.cmu.edu/People/rgs/pl-opt.html X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 PERL -- Options

Options

Note: on first reading this section may not make much = sense to=20 you. It's here at the front for easy reference.=20

A single-character option may be combined with the following option, = if any.=20 This is particularly useful when invoking a script using the #! = construct which=20 only allows one argument. Example:

	#!/usr/bin/perl -spi.bak	# same =
as -s -p -i.bak
	...
Options include:=20

-0digits=20
specifies the record separator ($/) = as an=20 octal number. If there are no digits, the null character is the = separator.=20 Other switches may precede or follow the digits. For example, if you = have a=20 version of find which can print filenames terminated by the = null=20 character, you can say this:
    find . -name '*.bak' -print0 | =
perl -n0e unlink
The special value 00 will cause Perl to slurp files in paragraph = mode.=20 The value 0777 will cause Perl to slurp files whole since there is no = legal=20 character with that value.=20

-a=20
turns on autosplit mode when used with a -n or -p. = An=20 implicit split=20 command to the @F array is done as the first thing inside the implicit = while<= /A>=20 loop produced by the -n or -p.
	perl -ane 'print pop(@=
F), "\n";'
is equivalent to
	while<=
/A> (<>) {
		@F =3D split(' ');
		print pop(@=
F), "\n";
	}

-c=20
causes perl to check the syntax of the script and then exit = without=20 executing it.=20

-d=20
runs the script under the perl debugger. See the section on = Debugging.=20

-Dnumber=20
sets debugging flags. To watch how it executes your script, use=20 -D14. (This only works if debugging is compiled into your = perl.)=20 Another nice value is -D1024, which lists your compiled syntax = tree.=20 And -D512 displays compiled regular expressions.=20

-e commandline=20
may be used to enter one line of script. Multiple -e = commands may=20 be given to build up a multi-line script. If -e is given, = perl=20 will not look for a script filename in the argument list.=20

-iextension=20
specifies that files processed by the <> construct are to be = edited=20 in-place. It does this by renaming the input file, opening the output = file by=20 the same name, and selecting that output file as the default for print=20 statements. The extension, if supplied, is added to the name of the = old file=20 to make a backup copy. If no extension is supplied, no backup is made. = Saying=20 "perl -p -i.bak -e "s/foo/bar/;" ... " is the same as using the = script:
	#!/usr/bin/perl -pi.bak
	s/foo/bar/;
which is equivalent to
	#!/usr/bin/perl
	while<=
/A> (<>) {
		if =
($ARGV ne =
$oldargv) {
			renam=
e($ARGV, $ARGV . '.bak');
			open(=
ARGVOUT, ">$ARGV");
			select<=
/A>(ARGVOUT);
			$oldargv =3D $ARGV;
		}
		s/foo/bar/;
	}
	con=
tinue {
	    print;	# this prints to original filename
	}
	select<=
/A>(STDOUT);
except that the -i form doesn't need to compare $ARGV to=20 $oldargv to know when the filename has changed. It does, however, use = ARGVOUT=20 for the selected filehandle. Note that STDOUT is restored as = the=20 default output filehandle after the loop.=20

You can use eof = to locate=20 the end of each input file, in case you want to append to each file, = or reset=20 line numbering (see example under eof). =

-Idirectory=20
may be used in conjunction with -P to tell the C = preprocessor where=20 to look for include files. By default /usr/include and /usr/lib/perl = are=20 searched.=20

-loctnum=20
enables automatic line-ending processing. It has two effects: = first, it=20 automatically chops the line terminator when used with -n or = -p=20 , and second, it assigns $\ = to have the=20 value of octnum so that any print=20 statements will have that line terminator added back on. If = octnum is=20 omitted, sets $\ = to the=20 current value of $/. = For=20 instance, to trim lines to 80 columns:
	perl -lpe 'substr($_, 80) =
=3D ""'
Note that the assignment $\ = =3D $/ = is done=20 when the switch is processed, so the input record separator can be = different=20 than the output record separator if the -l switch is followed = by a=20 -0 switch:
	gnufind / -print0 | perl -ln0e 'print "found =
$_" =
if -p'
This sets $\ = to newline=20 and then sets $/ = to the null=20 character.=20

-n=20
causes perl to assume the following loop around your = script, which=20 makes it iterate over filename arguments somewhat like "sed -n" or = awk:=20
	while<=
/A> (<>) {
		...		# your script goes here
	}
Note that the lines are not printed by default. See -p to = have=20 lines printed. Here is an efficient way to delete all files older than = a week:=20
	gfind . -mtime +7 -print | perl -nle 'unlink;'
This is faster than using the -exec switch of find because you = don't=20 have to start a process on every filename found.=20

-p=20
causes perl to assume the following loop around your = script, which=20 makes it iterate over filename arguments somewhat like sed: =
	while<=
/A> (<>) {
		...		# your script goes here
	} con=
tinue {
		print;
	}
Note that the lines are printed automatically. To suppress = printing use=20 the -n switch. A -p overrides a -n switch.=20

-P=20
causes your script to be run through the C preprocessor before = compilation=20 by perl. (Since both comments and cpp directives begin with the = #=20 character, you should avoid starting comments with any words = recognized by the=20 C preprocessor such as "if", "else" or "define".)=20

-s=20
enables some rudimentary switch parsing for switches on the = command line=20 after the script name but before any filename arguments (or before a = --). Any=20 switch found there is removed from @ARGV and sets the corresponding = variable=20 in the perl script. The following script prints "true" if and = only if=20 the script is invoked with a -xyz switch.
	#!/usr/bin/perl -s
	if =
($xyz) { print "true\n"; }

-S=20
makes perl use the PATH=20 environment variable to search for the script (unless the name of the = script=20 starts with a slash). Typically this is used to emulate #! startup on = machines=20 that don't support #!, in the following manner:
	#!/usr/bin/perl
	eval=
 "exec=
 /usr/bin/perl -S $0 $*"
		if =
$running_under_some_shell;
The system ignores the first line and feeds the script to /bin/sh, = which=20 proceeds to try to execute the perl script as a shell script. = The shell=20 executes the second line as a normal shell command, and thus starts up = the=20 perl interpreter. On some systems $0 = doesn't=20 always contain the full pathname, so the -S tells perl = to search=20 for the script if necessary. After perl locates the script, it = parses=20 the lines and ignores them because the variable = $running_under_some_shell is=20 never true. A better construct than $* = would be=20 ${1+"$@"},=20 which handles embedded spaces and such in the filenames, but doesn't = work if=20 the script is being interpreted by csh. In order to start up sh rather = than=20 csh, some systems may have to replace the #! line with a line = containing just=20 a colon, which will be politely ignored by perl. Other systems can't = control=20 that, and need a totally devious construct that will work under any of = csh, sh=20 or perl, such as the following:
	eval=
 '(exit=
 $?0)' && eval=
 'exec /usr/bin/perl -S $0 =
${1+"$@"}'
	& eval=
 'exec /usr/bin/perl -S $0 =
$argv:q'
		if =
0;

-u=20
causes perl to dump core after compiling your script. You = can then=20 take this core dump and turn it into an executable file by using the = undump=20 program (not supplied). This speeds startup at the expense of some = disk space=20 (which you can minimize by stripping the executable). (Still, a "hello = world"=20 executable comes out to about 200K on my machine.) If you are going to = run=20 your executable as a set-id program then you should probably compile = it using=20 taintperl rather than normal perl. If you want to execute a portion of = your=20 script before dumping, use the dump=20 operator instead. Note: availability of undump is platform specific = and may=20 not be available for a specific port of perl.=20

-U=20
allows perl to do unsafe operations. Currently the only = "unsafe"=20 operations are the unlinking of directories while running as = superuser, and=20 running setuid programs with fatal taint checks turned into warnings.=20

-v=20
prints the version and patchlevel of your perl executable.=20

-w=20
prints warnings about identifiers that are mentioned only once, = and scalar=20 variables that are used before being set. Also warns about redefined=20 subroutines, and references to undefined filehandles or filehandles = opened=20 readonly that you are attempting to write on. Also warns you if you = use =3D=3D on=20 values that don't look like numbers, and if your subroutines recurse = more than=20 100 deep.=20

-xdirectory=20
tells perl that the script is embedded in a message. = Leading=20 garbage will be discarded until the first line that starts with #! and = contains the string "perl". Any meaningful switches on that line will = be=20 applied (but only one group of switches, as with normal #! = processing). If a=20 directory name is specified, Perl will switch to that directory before = running=20 the script. The -x switch only controls the the disposal of = leading=20 garbage. The script must be terminated with __END__ if there is = trailing=20 garbage to be ignored (the script can process any or all of the = trailing=20 garbage via the DATA filehandle if desired).