2

If I have both /usr/bin/perl and /usr/local/bin/perl available on a system, which one should I use?

CJ7
  • 829

2 Answers2

5

It depends: on some systems, e.g. BSD packaging, /usr/local/bin may be the directory where packages install executables. In that case, it would be newer (and perhaps better), but /usr/bin/perl would be more stable.

You can use perl -V to see which has a newer version number. Sometimes the contents of /usr/local/bin (on multi-user systems) isn't really newer.

The usual compromise is to let the user decide, by using env in the shebang/hashbang line (but again, the path to env can vary by system). The shebang line refers to the first line of a script, which may look like one of the following:

#!/usr/bin/perl
#!/usr/bin/perl -w
#!/usr/bin/env perl

The first two specify the directory from which to run Perl. As a bonus, this form allows passing an option, e.g. -w (used to turn on warnings). The last line uses the env program to pick the first occurrence of perl by searching the directories listed in the user's PATH environment variable.

It is also possible to run a script like this:

perl myscript

which (like env) tells the shell to pick the first occurrence of perl from the directories listed in PATH.

An explicit

./perl myscript

of course overrides the PATH. But scripts with shebang lines are the way most people use Perl scripts (Perl one-liners are a different matter).

Further reading:

Thomas Dickey
  • 76,765
0

Who is it for? If it is just you, as long as both your versions of perl are new enough it does not matter. Is it for a work environment? If so check your deployment policies as they may have the answer. If it is for distribution to the general public, I would recommend varying based upon package type, specifically use /usr/bin/perl for debs (per the debian guidelines) and rpms (as Red Hat specifically disallows rpms to install to /usr/local), but /usr/bin/env perl for tar and shar packages as it has the best chance of working on random unix computers.

hildred
  • 5,829
  • 3
  • 31
  • 43