The UNIX/Linux Magic String

One way of changing a Perl script into a UNIX or Linux command is to make the first line of the script a “magic string,” which tells the operating system that this is a Perl script.

For example, if your perl executable is located in /usr/local/bin, you turn your script into a program by putting the line

#!/usr/local/bin/perl

at the top of your program. (It must be the first line and must not be preceded by any comments.)

You then need to tell the system that this is an executable program by setting the execute bit for the owner, owner’s group, and all other people:

$ chmod a+x script.pl

Now you can execute the script directly:

$ script.pl

You might want to rename your file from script.pl to script to be consistent with the standard UNIX naming convention as well. For example:

$ mv script.pl script

Note that if your Perl interpreter requires any flags, such as –I (specify module directory) or –T (turn on security checks), they can be included on the top line as well:

#!/usr/local/bin/perl –T –I/home/oualline/local/lib

The advantages to doing things this way include

  • Simple

  • Works for setuid scripts

The disadvantage is:

  • Nonportable

It is nonportable because the exact path to the perl command must be specified, and this can vary from system to system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset