Previous Page Next Page

3.3. Perl at the Command Line

Although most of your work with Perl will be done in scripts, Perl can also be executed at the command line for simple tasks, such as testing a function, a print statement, or simply testing Perl syntax. Perl has a number of command-line switches, also called command-line options, to control or modify its behavior. The switches discussed next are not a complete list (see Appendix A) but will demonstrate a little about Perl syntax at the command line.

When working at the command line, you will see a shell prompt. The shell is called a "command interpreter." UNIX shells such as Korn and Bourne display a default $ prompt, and C shell displays a % prompt. The UNIX, Linux (bash and tcsh), Mac OS shells are quite similar in how they parse the command line. By default, if you are using Windows XP or Vista, the MS-DOS shell is called command.com, and if you are using Windows NT, the command shell is a console application residing in cmd.exe. It too displays a $ prompt.[4] The Win32 shell has its own way of parsing the command line. Since most of your Perl programming will be done in script files, you will seldom need to worry about the shell's interaction, but when a script interfaces with the operating system, problems will occur unless you are aware of what commands you have and how the shell executes them on your behalf.

[4] It is possible that your command-line prompt has been customized to contain the current directory, history number, drive number, etc.

3.3.1. The -e Switch

The -e switch allows Perl to execute Perl statements at the command line instead of from a script. This is a good way to test simple Perl statements before putting them into a script file.

Example 3.6.

1   $ perl -e 'print "hello dolly\n";'     # UNIX/Linux
    hello dolly
2   $ perl -e "print qq/hello dolly\n/;"   # Windows and UNIX/Linux
    hello dolly

Explanation

  1. Perl prints the string hello dolly to the screen followed by a newline \n. The dollar sign ($) is the UNIX shell prompt. The single quotes surrounding the Perl statement protect it from the UNIX shell when it scans and interprets the command line. This will fail to execute on a Windows system.

  2. At the MS-DOS prompt, Perl statements must be enclosed in double quotes. The qq construct surrounding hello dolly is another way Perl represents double quotes. For example, qq/hello/ is the same as "hello". An error is displayed if you type the following at the MS-DOS prompt:

    $ perl -e 'print "hello dolly\n";'
    Can't find string terminator "" anywhere before EOF at -e line 1.

    Note: UNIX systems can use this format as well.

3.3.2. The -n Switch

If you need to print the contents of a file or search for a line that contains a particular pattern, the -n switch is used to implicitly loop through the file one line at a time. Like sed and awk, Perl uses powerful pattern-matching techniques for finding patterns in text. Only specified lines from the file are printed when Perl is invoked with the -n switch.

Reading from a File

The -n switch allows you to loop through a file whose name is provided at the command line. The Perl statements are enclosed in quotes, and the file or files are listed at the end of the command line.

Example 3.7.

(The Text File)
1   $ more emp.first
    Igor Chevsky:6/23/83:W:59870:25:35500:2005.50
    Nancy Conrad:6/18/88:SE:23556:5:15000:2500
    Jon DeLoar:3/28/85:SW:39673:13:22500:12345.75
    Archie Main:7/25/90:SW:39673:21:34500:34500.50
    Betty Bumble:11/3/89:NE:04530:17:18200:1200.75
2   $ perl -ne 'print;' emp.first     # Windows: use double quotes
    Igor Chevsky:6/23/83:W:59870:25:35500:2005.50
    Nancy Conrad:6/18/88:SE:23556:5:15000:2500
    Jon DeLoar:3/28/85:SW:39673:13:22500:12345.75
    Archie Main:7/25/90:SW:39673:21:34500:34500.50
    Betty Bumble:11/3/89:NE:04530:17:18200:1200.75

3   $ perl -ne 'print if /^Igor/;' emp.first
    Igor Chevsky:6/23/83:W:59870:25:35500:2005.50

Explanation

  1. The text file emp.first is printed to the screen. Perl will use this filename as a command-line argument in line 2.

  2. Perl prints all the lines in the file emp.first by implicitly looping through the file one line at a time. (Windows users should enclose the statement in double quotes instead of single quotes.)

  3. Perl uses regular expression metacharacters to specify what patterns will be matched. The pattern Igor is placed within forward slashes and preceded by a caret (^). The caret is called a "beginning of line anchor." Perl prints only lines beginning with the pattern Igor. (Windows users should enclose the statement in double quotes instead of single quotes.)

Reading from a Pipe

Since Perl is just another program, the output of commands can be piped to Perl, and Perl output can be piped to other commands. Perl will use what comes from the pipe as input, rather than a file. The -n switch is needed so Perl can read the input coming in from the pipe.

Example 3.8.

(UNIX)
1   $ date | perl -ne 'print "Today is $_";'
2   Today is Mon Mar 12 20:01:58 PDT 2007


(Windows)
3   $ date /T | perl -ne "print qq/Today is $_/;"
4   Today is Tue 04/24/2007

Explanation

  1. The output of the UNIX date command is piped to Perl and stored in the $_ variable. The quoted string Today is and the contents of the $_ variable will be printed to the screen followed by a newline.

  2. The output illustrates that today's date was stored in the $_ variable.

  3. The Windows date command takes /T as an option that produces today's date. That ouput is piped to Perl and stored in the $_ variable. The double quotes are required around the print statement.

Perl can take its input from a file and send its output to a file using standard I/O redirection.

Example 3.9.

1   $ perl -ne 'print;' < emp.first
    Igor Chevsky:6/23/83:W:59870:25:35500:2005.50
    Nancy Conrad:6/18/88:SE:23556:5:15000:2500
    Jon DeLoar:3/28/85:SW:39673:13:22500:12345.75
    Archie Main:7/25/90:SW:39673:21:34500:34500.50
    Betty Bumble:11/3/89:NE:04530:17:18200:1200.75
2   $ perl -ne 'print' emp.first > emp.temp

Explanation

  1. Perl's input is taken from a file called emp.first. The output is sent to the screen. For Windows users, enclose the statement in double quotes instead of single quotes.

  2. Perl's input is taken from a file called emp.first, and its output is sent to the file emp.temp. For Windows users, enclose the statement in double quotes instead of single quotes.

3.3.3. The -c Switch

As we demonstrated earlier in this chapter, the -c switch is used to check the Perl syntax without actually executing the Perl commands. If the syntax is correct, Perl will tell you so. It is a good idea to always check scripts with the -c switch. This is especially important with CGI scripts written in Perl, because error messages that are normally sent to the terminal screen are sent to a log file instead. (See also the -w switch in Chapter 4.)

Example 3.10.

1   print "hello';  Search pattern not terminated at  line 1.
 Can't find string terminator '"' anywhere before EOF at test.plx
2   print "hello";
    test.plx syntax OK

Explanation

  1. The string hello starts with a double quote but ends with a single quote. The quotes should be matched; i.e., the first double quote should be matched at the end of the string with another double quote but instead ends with a single quote. With the -c switch, Perl will complain if it finds syntax errors while compiling.

  2. After correcting the previous problem, Perl lets you know that the syntax is correct.

Previous Page Next Page