Using the env Prog ram

The magic string method is nonportable. If the perl command is moved to another location or the script is used on another machine, the script will not work.

A solution to this problem is to use the helper program env. This involves putting a new magic string at the top of the script:

#!/usr/bin/env perl

When the script is run, the env program starts up. Its job is to look at the rest of the command line and run the command specified. So in this case, the first perl in your path will be executed.

Advantages of the env approach include

  • Simple

  • Portable

The disadvantages are

  • Won’t work for setuid scripts

  • Won’t work if the perl command is not in your path

Get Perl for C Programmers 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.