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

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.