Other Command-Line Stuff

 <  Day Day Up  >  

The debugger isn't the only feature of the Perl interpreter that can be activated by command-line switches. In fact, many useful Perl programs can be written just at the command prompt.

By the Way

Macintosh users should run these command-line examples by selecting 1-liners from the Script menu and then typing the command in the dialog box.


One-Liners

The key to such programs is the -e switch given to Perl on the command line. Following the -e can be any Perl statements at all, as in this example:

 C:\>  perl -e "print 'Hello, world';"  Hello, world 

You can use multiple -e statements to insert multiple statements or separate them with semicolons, as shown here:

 C:\>  perl -e "print 'Hello, world';" -e "print 'Howzit goin?'"  Hello, worldHowzit goin? 

A word of caution: Most command interpreters have rules about quotation marks. The Windows/MS-DOS command interpreter ”command.com or the NT command shell ”allows you to use double quotes to group words ”such as print and Hello, World in the preceding example ”but you cannot easily put double quotes inside other double quotes. Nor can you easily put > , < , , or ^ inside the double quotes. Consult your operating system's manual for a complete list of MS-DOS/Windows quoting rules.

Under Unix, generally as long as the quotes are balanced ”every open quote has a close quote ”and embedded quotation marks have a \ in front of them, you should be okay:

 $ perl -e 'print "Hello, World\n";' -e 'print "Howzit goin?\n"' 

The preceding example works under most Unix shells ” csh , ksh , bash , and so on ”and prints the message with the correct newlines. For a complete list of your shell's quoting rules, consult your shell's online manual page.

One frequent ”and very useful ”use of the -e switch is to combine it with -d and drop into Perl's debugger directly ”without having a program to debug:

 C:\>  perl -d -e 1  Loading DB routines from perl5db.pl version 1 Emacs support available. Enter h or `h h' for help. main::(-e:1):   1   DB<1> __ 

The debugger is now waiting for your commands. This particular incantation is useful for testing Perl syntax without your actually having to write a whole program, test, debug, edit, test, debug, and so on. Simply try your statements in the debugger until they work. The 1 on the command line is a minimalist Perl program; it's an expression that evaluates to ”and returns ”1.

Other Switches

The -c switch to the Perl interpreter causes Perl to examine your code for syntax problems ”but not actually run the program:

 C:\>  perl -c Example.pl  Example.pl syntax OK 

If a syntax error occurs, Perl may print a message like this:

 C:\>  perl -c Example.pl  syntax error at Example.pl line 5, near "print" Example.pl had compilation errors 

Combined with -w , the -c switch compiles your program and shows any warning messages that Perl thinks is appropriate.

When you're asking a more knowledgeable Perl user or your system administrator about debugging code, you are often requested to supply the version of the interpreter being used. The version of the Perl language that's in use primarily is Perl 5. The interpreter itself has a version that you can gather by using the -v switch on the command line, as shown here:

 C:\>  perl -v  This is perl, version 5.004_02 Copyright 1987-1997, Larry Wall 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.0 source kit. 

In the preceding example, the version of the Perl interpreter is 5.004_02. To get even more detailed information about how the interpreter was built, when it was built, and so on, you can run the interpreter with the -V switch, like this:

 C:\>  perl -V  Summary of my perl5 (5.0 patchlevel 4 subversion 02) configuration:   Platform:     osname=MSWin32, osvers=4.0, archname=MSWin32     : Compiler:     cc='bcc32', optimize='-O', gccversion=     : Characteristics of this binary (from libperl):   Compile-time options: DEBUGGING   Built under MSWin32   Compiled at Aug  9 1997 21:42:37   @INC:    C:\PERL\lib\site    C:\PERL\lib     c:\perl\lib    c:\perl\lib\site    c:\perl\lib\site  . 

This output might be useful if you're trying to debug a problem with the interpreter itself ”perhaps a problem with the installation. At the end, note the values for @INC . This particular installation of Perl expects to find its modules in these directories. (Modules will be discussed in Hour 14, "Using Modules.") After Perl is installed, it cannot simply be moved from one directory to another. The interpreter itself has a built-in idea of where to find its modules, and moving it causes Perl to look in the wrong place for its modules.

Empty Angle Brackets and More One-Liners

The angle operator ( <> ) discussed so far have had a couple of functions:

  1. With a filehandle, the angle operators allow you to read the filehandle ”for example, <STDIN> .

  2. With a pattern, the angle operators return a list of files matching that pattern, called a glob ”for example, <*.bat> .

The angle operators have yet another function: A set of angle operators with nothing between them reads all the contents of all the files on the command line or, if no filenames are given, from the standard input. Sometimes an empty angle operator is called a diamond operator (the name comes from its shape). For example, examine this small Perl program:

 #!/usr/bin/perl -w while(<>) {     print $_; } 

If you save the preceding program as Example.pl , then running the program with the command line

 C:\>  perl -w Example.pl file1 file2 file3  

would cause the <> to read the contents of file1 one line at a time, then file2 , and then file3 . If no files are specified, the angle operators read from the STDIN filehandle. This behavior mimics Unix utilities ”such as sed, awk, and so on ”that read input from files if given on the command line, and otherwise use standard input.

By the Way

After the Perl interpreter switches ( -w , -c , -d , -e , and so on) are stripped away, the arguments to the Perl program are stored in an array called @ARGV . For example, for the previous snippet's arguments, $ARGV[0] would contain file1 , $ARGV[1] would contain file2 , and so on.


The -n switch to Perl wraps up any following statements on the command line into this small program:

 LINE: while(<>) { ... # Command-line Perl statements here. } 

So, to create a one-liner to remove leading spaces from input, you could write the following:

 C:\>  perl -n -e 's/^\s+//g; print $_;' file1  

The preceding command actually runs a Perl program that looks like this:

 LINE: while(<>) {     s/^\s+//g;     print $_; } 

In the preceding snippet, the file named file1 is opened and assigned to $_ in the while loop, one line at a time. The line is edited with s/^\s+//g and then printed.

The -p switch is identical to -n except that lines are printed automatically after your statements are executed. So, rewriting the previous example would yield the following line:

 C:\>  perl -p -e 's/^\s+//g' file1  

When you're editing a file with a Perl one-liner, you must be careful not to open the file for reading while trying to write to it at the same time, as in this example:

 C:\>  perl -p -e 's/\r//g' dosfile > dosfile  

The preceding snippet tries to remove carriage returns from a file called dosfile. The problem is that dosfile is overwritten by > dosfile before the Perl command is even processed . The correct way to edit a file is to redirect the output to another file and rename it to the original name, like this:

 C:\>  perl -p -e 's/\r//g' dosfile > tempfile  C:\>  rename tempfile dosfile  

By the Way

Writing short "one-liners" is considered a pastime for some Perl enthusiasts ; the more convoluted and capable the program, the better. The Perl Journal , a quarterly magazine for Perl, scatters one-liners throughout each issue.


 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

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