home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeJava and XSLTSearch this book

Chapter 3. The Perl Executable

The perl executable is normally installed in /usr/bin or /usr/local/bin on your machine. Some people often refer to perl as the Perl interpreter, but this isn't strictly correct, as you'll learn shortly.

Every Perl program must be passed through the Perl executable to be executed. The first line in many Perl programs is something like:

#!/usr/bin/perl

For Unix systems, this #! (hash-bang or shebang) line tells the shell to look for the /usr/bin/perl program and pass the rest of the file to that /usr/bin/perl for execution. Sometimes, you'll see different pathnames to the Perl executable, such as /usr/local/bin/perl. You might see perl5 or perl6 instead of perl on sites that still depend on older versions of Perl.

Often, you'll see command-line options tacked on the end of perl, such as the notorious -w switch, which produces warning messages. But almost all Perl programs on Unix start with some variation of #!/usr/bin/perl.

If you get a mysterious "Command not found" error on a Perl program, it's often because the path to the Perl executable is wrong. When you download Perl programs off the Internet, copy them from one machine to another, or copy them out of a book (like this one!). The first thing you should do is make sure that the #! line points to the location of the Perl executable on your system. If you're on a Win32 platform, where the shebang path is used only to check for Perl switches, you should make sure that you run pl2bat.bat on the program so you can run it directly from the command line.

So what does the Perl executable do? It compiles the program internally into a parse tree and executes it immediately. Because the program is not compiled and executed in separate steps, Perl is commonly known as an interpreted language, but this is not quite true.[5]

[5]Unlike strictly compiled languages, the compiled form of a Perl program is not stored as a separate file. However, Versions 5.6 and later give you the option of using a standalone Perl compiler that creates bytecode to be executed separately. We'll say more about the compiler later in this chapter.

So do you call something a Perl "script" or a Perl "program"? Typically, the word "program" is used to describe something that needs to be compiled into assembler or bytecode before executing, as in the C language. The word "script" is used to describe something that runs through an executable on your system, such as the Bourne shell. For Perl, you can use either phrase and only offend those Perl programmers who care about semantics more than you do.

What does all this mean for you? When you write a Perl program, you can just give it a correct #! line at the top of the script, make it executable with chmod +x, and run it. For 95% of Perl programmers in this world, that's all they care about.

3.1. Command Processing

In addition to specifying a #! line, you can specify a short script directly on the command line. Here are some of the possible ways to run Perl:

  • Issue the perl command, writing your script line by line via -e switches on the command line:

    perl -e 'print "Hello, world\n"'    # Unix
    perl -e "print \"Hello, world\n\""  # Win32 or Unix
    perl -e "print qq[Hello, world\n]"  # Also Win32
  • Issue the perl command, passing Perl the name of your script as the first parameter (after any switches):

    perl testpgm
  • On Unix systems that support the #! notation, specify the Perl command on the #! line, make your script executable, and invoke it from the shell (as described above).

  • Pass your script to Perl via standard input. For example, under Unix:

    echo "print 'Hello, world'" | perl -
    % perl
    print "Hello, world\n";
    ^D
  • On Win32 systems, you can associate an extension (e.g., .plx) with a file type and double-click on the icon for a Perl script with that file type. Or, as mentioned earlier, do this:

    (open a "DOS" window)
    C:\> (edit your Perl program in your favorite editor)
    C:\> pl2bat yourprog.plx
    C:\> .\yourprog.bat
    (program output here)

    If you are using the ActiveState version of Win32 Perl, the installer normally prompts you to create the association.

  • On Win32 systems, if you double-click on the icon for the Perl executable, you'll find yourself in a command-prompt window with a blinking cursor. You can enter your Perl commands, indicating the end of your input with Ctrl-Z, and Perl will compile and execute your script.

Perl parses the input file from the beginning, unless you've specified the -x switch (see Section 3.2, "Command-Line Options" later in this chapter). If there is a #! line, it is always examined for switches as the line is being parsed. Thus, switches behave consistently regardless of how Perl was invoked.

After locating your script, Perl compiles the entire script into an internal form. If there are any compilation errors, execution of the script is not attempted. 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.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru