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.
|