Current Issue


Table of contents

CD-ROM

Sys Admin and The Perl Journal CD-ROM version 12.0

Version 12.0 delivers every issue of Sys Admin from 1992 through 2006 and every
issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Sys Admin Magazine > Archives > 2005 > March 2005

Understanding the Command Line

Randal L. Schwartz

In past columns, I've talked a lot about the Perl language, but have never said much about perl at the Unix shell command line. So, let's fix that by looking at some commonly used command-line constructs for Perl.

Let's take the simplest invocation:

perl my-script
This invokes my-script, using the relative or absolute path to the script as given, thus not using the PATH in any way. We can include arguments to the script:

perl my-script arg1 arg2 arg3
This sets up @ARGV to be the three individual values of arg1, arg2, and arg3, as if we had said:

@ARGV = qw(arg1 arg2 arg3);
If we want a space within one of the values, we need to use shell quoting rules:

perl my-script 'arg1a arg1b' arg2
This passes two arguments now, not three. We get the same result with:

perl my-script arg1a\ arg1b arg2
using a backslash to quote the space between the arguments. If there are any shell wildcard ("glob") characters, the shell expands them before calling our program:

perl my-script *.html
which might turn into (given three matching files):

@ARGV = qw(index.html problem.html results.html);
Note that Perl has no clue that a shell wildcard was involved here; it's as if we had typed the three names individually.

Perl doesn't interpret the @ARGV values in any particular way. They could be keywords, filenames, or some combination of the two. Traditionally, leading @ARGV elements that begin with a minus are considered "options", which we can process with modules such as Getopt::Std or Getopt::Long.

We can also have options to Perl itself by placing leading-minus values to the left of the script name.




MarketPlace

"Six Sigma" Certification
Villanova Six Sigma Certification 100% Online Program - Free Info.

PMP Certification-Online
Nation's Leading Online PMP Course From Villanova-Find Out More Now.

Flowcharts from C/C++ code -- Free trial download
Understand C/C++ code in less time. A new team member ? Inherited legacy code ? Get up to speed faster with Crystal Flow for C/C++. Code-formatting improves readability. Flowcharts are integrated with code browser. Export flowcharts to Visio.

Automate Software Builds with Visual Build Pro
Easily create an automated, repeatable process for building and deploying software.

Wanna see your ad here?