О КОПИРАЙТАХ |
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом. |
|
|
|
|
Previous | Next
Contents:
Cooperating with Command Interpreters
Cooperating with Other Processes
Cooperating with Strangers
Cooperating with Other Languages
Languages have different personalities. You can classify computer
languages by how introverted or extroverted they are; for instance, Icon
and Lisp are stay-at-home languages, while Tcl and the various shells
are party animals. Self-sufficient languages prefer to compete with other
languages, while social languages prefer to cooperate with other
languages. As usual, Perl tries to do both.
So this chapter is about relationships. Until now we've looked inward
at the competitive nature of Perl, but now we need to look outward
and see the cooperative nature of Perl. If we really mean what we
say about Perl being a glue language, then we can't just talk about
glue; we have to talk about the various kinds of things you can glue
together. A glob of glue by itself isn't very interesting.
Perl doesn't just glue together other computer languages. It also glues
together command line interpreters, operating systems, processes,
machines, devices, networks, databases, institutions, cultures, Web
pages, GUIs, peers, servers, and clients, not to mention people like
system administrators, users, and of course, hackers, both naughty and
nice. In fact, Perl is rather competitive about being cooperative.
So this chapter is about Perl's relationship with everything in the world.
Obviously, we can't talk about everything in the world, but we'll try.
It is fortunate that Perl grew up in the UNIX world--that means
its invocation syntax works pretty well under the command interpreters
of other operating systems too. Most command interpreters know how to
deal with a list of words as arguments, and don't care if an argument
starts with a minus sign. There are, of course, some sticky spots where
you'll get fouled up if you move from one system to another. You can't
use single quotes under MS-DOS as you do under UNIX, for instance. And
on systems like VMS, some wrapper code has to jump through hoops to
emulate UNIX I/O redirection. Once you get past those issues, however,
Perl treats its switches and arguments much the same on any
operating system.
Even when you don't have a command interpreter, per se, it's easy to execute a Perl script from another program, such as the inet
daemon or a CGI server. Not only can such a server pass arguments in
the ordinary way, but it can also pass in information via environment
variables and (under UNIX at least) inherited file descriptors. Even
more exotic argument-passing mechanisms may be encapsulated in a module
that can be brought into the Perl script via a simple use directive.
Perl parses command-line switches in the standard fashion.[1]
That is, it expects any switches (words beginning with a minus) to
come first on the command line. After that comes the name of the script
(usually), followed by any additional arguments (often filenames) to be
passed into the script. Some of these additional arguments may be
switches, but if so, they must be processed by the script, since Perl
gives up parsing switches as soon as it sees a non-switch, or the
special "- -" switch that terminates switch processing.
Perl gives you some flexibility in how you supply your program. For
small, quick-and-dirty jobs, you can program Perl entirely from the
command line. For larger, more permanent jobs, you can supply a Perl
script as a separate file. Perl looks for the script to be specified in
one of three ways:
- Specified line by line via -e switches on the command line.
- Contained in the file specified by the first filename on the command line.
(Note that systems supporting the #! shebang notation invoke
interpreters this way on your behalf.)
- Passed in implicitly via standard input. This only works if there are
no filename arguments; to pass arguments to a standard-input script you
must explicitly specify a "-" for the script name. For example,
under UNIX:
echo "print 'Hello, world'" | perl -
With methods 2 and 3, Perl starts parsing the input file from the
beginning, unless you've specified a -x switch, in which case it
scans for the first line starting with #! and containing the word
"perl", and starts there instead. This is useful for running a script
embedded in a larger message. (In this case you might indicate the end
of the script using the _ _END_ _ token.)
Whether or not you use -x, the #! line is always examined for
switches as the line is being parsed. Thus, if you're on a machine that
only allows one argument with the #! line, or worse, doesn't even
recognize the #! line as special, you still can get consistent switch
behavior regardless of how Perl was invoked, even if -x was used to
find the beginning of the script.
WARNING:
Because many versions of UNIX silently chop off kernel interpretation of
the #! line after 32 characters, some switches may be passed in on the
command line, and some may not; you could even get a "-" without its
letter, if you're not careful. You probably want to make sure that all
your switches fall either before or after that 32-character boundary.
Most switches don't actually care if they're processed redundantly, but
getting a "-" instead of a complete switch could cause Perl to try to
execute standard input instead of your script. And a partial -I switch
could also cause odd results. Of course, if you're not on a UNIX system,
you're guaranteed not to have this problem.
Parsing of the switches on the #! line starts wherever "perl" is
mentioned in the line. The sequences "-*" and "- " are specifically
ignored for the benefit of emacs users, so that, if you're
so inclined, you can say:
#!/bin/sh -- # -*- perl -*- -p
eval 'exec perl -S $0 ${1+"$@"}'
if 0;
and Perl will see only the -p switch. The fancy "-*- perl
-*-" gizmo tells emacs to start up in Perl mode; you don't
need it if you don't use emacs. The -S mess is explained below.
If the #! line does not contain the word "perl", the program
named after the #! is executed instead of the Perl interpreter.
For example, suppose you have an ordinary Bourne shell script out there
that says:
#!/bin/sh
echo "I am a shell script"
If you feed that file to Perl, then Perl will run /bin/sh for you.
This is slightly bizarre, but it helps people on machines that don't
recognize #!, because--by setting their
SHELL environmental variable--they can tell a program (such as a mailer)
that their shell is /usr/bin/perl, and Perl will then dispatch the
program to the correct interpreter for them, even though their kernel is
too stupid to do so. Classify it as a strange form of cooperation.
But back to Perl scripts that are really Perl scripts.
After locating your script, Perl compiles the entire script to an
internal form. If any compilation errors arise, execution of the
script is not attempted (unlike the typical shell script,
which might run partway through before finding a syntax error).
If the script is syntactically correct, it is executed. If the script
runs off the end without hitting an exit or die operator, an implicit
exit(0) is provided to indicate successful completion.
A single-character switch with no argument may be combined (bundled)
with the following switch, if any.
#!/usr/bin/perl -spi.bak # same as -s -p -i.bak
Switches are also known as options, or flags.
Perl recognizes these switches:
- - -
-
Terminates switch processing, even if the next argument starts with a
minus. It has no other effect.
- -0[octnum]
-
Specifies the record separator ($/) as an octal number. If octnum
is not present, the null character is the separator. Other switches may
precede or follow the octal number. For example, if you have a version of
find (1) that can print filenames terminated by the null character, you
can say this:
find . -name '*.bak' -print0 | perl -n0e unlink
The special value 00 will cause Perl to slurp files in paragraph mode,
equivalent to setting the $/ variable to "".
The value 0777 will cause Perl to slurp files whole since there is no
legal ASCII character with that value. This is equivalent to undefining
the $/ variable.
- -a
-
Turns on autosplit mode when used with a -n
or -p. An implicit
split command to the @F array is done as the first thing inside the
implicit while loop produced by the
-n or -p. So:
perl -ane 'print pop(@F), "\n";'
is equivalent to:
while (<>) {
@F = split(' ');
print pop(@F), "\n";
}
A different field delimiter may be specified using -F.
- -c
-
Causes Perl to check the syntax of the script and then exit without
executing it. Actually, it will execute any BEGIN blocks and
use directives, since these are considered to occur before the
execution of your program. It also executes any END blocks, in case
they need to clean up something that happened in a corresponding
BEGIN block. The switch is more or less equivalent to having an
exit(0) as the first statement in your program.
- -d
-
Runs the script under the Perl debugger. See "The Perl Debugger" in
Chapter 8, Other Oddments.
- -d:foo
-
Runs the script under the control of a debugging or tracing module
installed in the Perl library as Devel::foo. For example,
-d:DProf executes the script using the
Devel::DProf profiler. See also the debugging section in Chapter 8, Other Oddments.
- -Dnumber
-Dlist
-
Sets debugging flags. (This only works if debugging is compiled into
your version of Perl via the -DDEBUGGING C compiler switch.) You may
specify either a number that is the sum of the bits
you want, or a list of letters. To watch how it executes your script,
for instance, use -D14 or -Dslt. Another nice value is -D1024
or -Dx, which lists your compiled syntax tree. And -D512 or
-Dr displays compiled regular expressions. The numeric value is
available internally as the special variable $^D. Here are the
assigned bit values:
|