|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
Recommended Books | Recommended Links | Perl Debugging |
A single-character options may be combined. This is useful when invoking a script using the #! construct which only allows only a single argument. Example:
#!/usr/bin/perl -spi.bak # same as -s -p -i.bak ...Perl options include:
find . -name '*.bak' -print0 | perl -n0e unlinkThe special value 00 will cause Perl to slurp files in paragraph mode. The value 0777 will cause Perl to slurp files whole since there is no legal character with that value.
perl -ane 'print pop(@F), "\n";'is equivalent to
while (<>) { @F = split(' '); print pop(@F), "\n"; }
#!/usr/bin/perl -pi.bak s/foo/bar/;which is equivalent to
#!/usr/bin/perl while (<>) { if ($ARGV ne $oldargv) { rename($ARGV, $ARGV . '.bak'); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT);except that the -i form doesn't need to compare $ARGV to $oldargv to know when the filename has changed. It does, however, use ARGVOUT for the selected filehandle. Note that STDOUT is restored as the default output filehandle after the loop.
You can use
eof to locate the end of each input file, in case you want to
append to each file, or reset line numbering (see example under
eof).
perl -lpe 'substr($_, 80) = ""'Note that the assignment $\ = $/ is done when the switch is processed, so the input record separator can be different than the output record separator if the -l switch is followed by a -0 switch:
gnufind / -print0 | perl -ln0e 'print "found $_" if -p'This sets $\ to newline and then sets $/ to the null character.
while (<>) { ... # your script goes here }Note that the lines are not printed by default. See -p to have lines printed. Here is an efficient way to delete all files older than a week:
gfind . -mtime +7 -print | perl -nle 'unlink;'This is faster than using the -exec switch of find because you don't have to start a process on every filename found.
while (<>) { ... # your script goes here } continue { print; }Note that the lines are printed automatically. To suppress printing use the -n switch. A -p overrides a -n switch.
#!/usr/bin/perl -s if ($xyz) { print "true\n"; }
#!/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 proceeds to try to execute the perl script as a shell script. The shell executes the second line as a normal shell command, and thus starts up the perl interpreter. On some systems $0 doesn't always contain the full pathname, so the -S tells perl to search for the script if necessary. After perl locates the script, it parses the lines and ignores them because the variable $running_under_some_shell is never true. A better construct than $* would be ${1+"$@"}, which handles embedded spaces and such in the filenames, but doesn't work if the script is being interpreted by csh. In order to start up sh rather than csh, some systems may have to replace the #! line with a line containing just a colon, which will be politely ignored by perl. Other systems can't control that, and need a totally devious construct that will work under any of csh, sh 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;
|
Switchboard | ||||
Latest | |||||
Past week | |||||
Past month |
Perl has a wide range of command-line options or switches that you can use. The options are also called switches because they can turn on or turn off different behaviors. A thorough knowledge of the command line switches will enable you to create short one-time programs to perform odd little tasks. For example, the -e option lets you specify a line of code directly on the command-line instead of creating a script file. You use the -l option to change the line endings in a text file.
How Are the Options Specified?
The most frequent way to specify a command-line options is on the command line. All of Perl's options are specified using a dash and then a single character followed by arguments, if needed. For example,
perl -I/usr/~john/include script.plYou can combine options with no arguments with the following switch. The following two command lines are equivalent.
perl -cI/usr/~john/include script.pl perl -c -I/usr/~john/include script.plYou can also specify command-line options inside your script file using the #! line. Just place them following the directory and/or executable name. If you are working on a UNIX system, you are probably familiar with using the #! notation to tell the system where to find the Perl executable. The various UNIX systems and Windows can interpret the #! line in different ways. Therefore, Perl starts parsing the #! switches immediately after the first instance of "perl" on the line. For example, if you started your script with this line:
#!/bin/perl -wThen Perl will run with the -w option in effect.
Caution Some UNIX systems will only read the first 32 characters of the #! line. So try to have your options either end before the 32nd position or start after the 32nd position. Placing the options after the 32nd position will help to make your scripts more portable because you will be bypassing one of the inconsistencies of UNIX.
What Are the Options?
Table 17.1 provides a short description of each command-line option used with Perl. After the table, examples of several options will be shown.
Table 17.1-Perl's Command Line Options Option Description -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. See "Example: Using the -0 option" for more information. -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. See "Example: Using the -n and -p Options" for more information. -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 Chapter 16, "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. I know that 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 anymore) 8192 H Hash Dump -- usurps values() 16384 X Scratchpad Allocation 32768 D Cleaning Up -e This 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. See "Examples: Using the -n and -p Options" for more information. -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. See "Examples: Using the -n and -p Options" for more information. -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. -u This 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 16, "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.
Example: Using the -0 Option
The -0 option will let you change the record separator. This is useful if your records are separated by something other than a newline. Let's use the example of input records separated by a dash instead of a newline. First, you need to find out the octal value of the dash character. The easy way to do this is covert from the decimal value which will be displayed if you run the following command line.
perl -e "print ord('-');"This program will display 45. Converting 4510 into octal results in 558.Next, you'll need an input file to practice with. Listing 17.1 shows a sample input file.
Listing 17.1-17LST01.DAT - Test Input File for the -0 Option Veterinarian-Orthopedist-Dentist-
Listing 17.2 holds a program that reads the above data file using the diamond operators. The program will use the dash character as an end-of-line indicator.
Pseudocode Set the record separator to be a dash using the #! switch setting method. Open a file for input.
Read all of the records into the @lines array. One element in @lines will be one record.
Close the file.
Iterate over the @lines array and print each element.
Listing 17.2-17LST02.PL - Using the -0 Option to Change the Record Separator #!perl -0055 open(FILE, "<17LST01.DAT"); @lines = <FILE>; close(FILE); foreach (@lines) { print("$_\n"); }
Tip Instead of using the command-line option, you could also say $/ = "-";. Using the command-line is a better option if the line ending changes from input file to input file.
This program will display:
Veterinarian- Orthopedist- Dentist-Notice that the end-of-line indicator is left as part of the record. This behavior also happens when the newline is used as the end-of-line indicator. You can use chop() or chomp() to remove the dash, if needed.Example: Using the -n and -p Options
The -n and -p options wrap your script inside loops. Before looking at specific examples, let's see what the loops look like and how they are changed by the -a and -F options.The -n option causes Perl to execute your script inside the following loop:
while (<>) { # your script }The -p option uses the same loop, but adds a continue block so that $_ will be printed every time through the loop. If both -n and -p are specified on the command line, the -p option will take precedence. The loop looks like this:
while (<>) { # your script } continue { print; }The -a option adds a split() function call to the beginning of each iteration of the loop. So that the loop looks like this:
while (<>) { @F = split(/ /); # your script }The -F option lets you split on something besides the space character. If you used -F/|+/ on the command line, the loop would look like this:
while (<>) { @F = split(/|+/); # your script }You can use BEGIN and END blocks if you need to specify some initialization or cleanup code. The initialization section might be used to create objects or to open log files. The cleanup section can be used to display statistics or close files. For example,
BEGIN { # initialization section $count = 0; } while (<>) { # your script } END { # cleanup section print("The count was $count.\n"); }Next, you'll see some examples of these options in action. Let's start with a command-line that simply displays each line of the input file - like the "type" command in DOS and UNIX.
The following examples use a data file called data.dat with the following lines:
David Veterinarian John Orthopedist Jeff Dentistperl -p -e "1;" data.datThis command line is equivalent to:
while (<>) { 1; } continue { print; }
Note The 1; statement was used to give Perl something to process. Otherwise, Perl would not have had any statements to execute.
And will display:
David Veterinarian John Orthopedist Jeff DentistHow about just printing the first word of each line? You could use this command line:perl -p -e "s/\s*(\w+).*/$1/;" test.datwhich is equivalent to:while (<>) { s/\s*(\w+).*/$1/; } continue { print; }And will display:David John JeffIf you have data files that store information in columns, you can pull out the second column of information like this:
perl -p -e "s/\s*.+\s(.+)\s*/$1\n/;" test.datwhich will display:Veterinarian Orthopedist DentistYou can use the -a option to get access to information stored in columns. For example, you could also display the second column like this:perl -p -a -e "$_ = \"$F[1]\n\";" test.datwhich is equivalent towhile (<>) { @F = split(/ /); $_ = "$F[1]\n"; } continue { print; }Notice you need to escape the double-quotes in the above command-line. If you don't do this you get an error message.
Example: Using the -i Option
The -i option lets you modify files in-place. This means that Perl will automatically rename the input file and open the output file using the original name. You can force Perl to create a backup file by specifying a file extension for the backup file immediately after the -i. For example, -i.bak. If no extension is specified, no backup file will be kept.One of the more popular uses for the -i option is to change sequences of characters. This kind of change normally requires 10 or more lines of code. However, using command line options you can do it like this:
perl -p -i.bak -e "s/harry/tom/g;" test.datThis command line will change all occurrences of "harry" to "tom" in the test.dat file.Examples: Using the -s Option
The -s option lets you create your own custom switches. Custom switches are placed after the script name but before any filename arguments. Any custom switches are removed from the @ARGV array. Then a scalar variable named after the switch is created and initialized to 1. For example, let's say that you want to use a switch called -useTR in a script like the one in Listing 17.3.
Listing 17.3-17LST03.PL - Checking for the useTR Switch i
f ($useTR) { # do TR processing. print "useTR=$useTR\n"; }You might execute this program using this following command line:
perl -s -w 17lst03.pl -useTRand it would display:useTR=1Summary
This chapter covered the different command-line options that you can use with Perl. The options can also be referred to as switches because they turn different behaviors on and off.The switches can be specified on the command line or using the #! line inside your script. If you use the #! line, try to place the options after the 32nd position to avoid inconsistent handling by different versions of UNIX.
The -n option is used to place your script inside of an input loop. The -p option uses the same loop, but also prints the $_ variable after each pass through the loop. The -a and -F options are used when you want the input lines to be split into the @F array.
Another very useful option is -i, which lets you edit files in-place. This option is good when you are doing a lot of text file manipulation.
The next chapter, "Using Internet Protocols," introduces you to some of the different standards used on the Internet. These standards let you perform activities like read mail, send mail, and transfer files.
Review Questions
- What is a command line option?
- What are the two places that the switches can be specified?
- What switch should always be used?
- Which switch lets you read records that end with the ~ character instead of the newline?
- What two options can be used with the -n option?
- How can you execute a script that someone sent you via E-mail?
- What happens if you specify both the -v and the -c options?
Review Exercises
- Use the -v option to see the patchlevel of your version of Perl.
- Use the chomp or chop function to remove the dash from the end of the records printed by the program listing 17.2.
- Write a program that uses the -p option to display the third column.
- Modify the program written in exercise 3 to use a BEGIN block to ask the user which column to display?
- Create a sample E-mail message that contains a Perl script. Use the -x option to execute it.
- Modify the E-mail message written for exercise 5 to display any text that appears after the __END__ token. Hint: Use the DATA file handle.
Society
Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers : Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism : The Iron Law of Oligarchy : Libertarian Philosophy
Quotes
War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard Shaw : Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds : Larry Wall : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting Languages : Perl history : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history
Classic books:
The Peter Principle : Parkinson Law : 1984 : The Mythical Man-Month : How to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|
You can use PayPal to to buy a cup of coffee for authors of this site |
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.
Last modified: September 30, 2020