Which of /usr/bin/perl or /usr/local/bin/perl should be used?

9,460

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:

Share:
9,460

Related videos on Youtube

CJ7
Author by

CJ7

Updated on September 18, 2022

Comments

  • CJ7
    CJ7 over 1 year

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

  • CJ7
    CJ7 about 8 years
    If I run perl from my home directory, which perl will be executed?
  • muru
    muru about 8 years
    @CJ7 Run type perl or command -v perl to find out
  • muru
    muru about 8 years
    @CJ7 as Thomas has already said, the PATH environment variable.
  • Mayur A Muley
    Mayur A Muley about 8 years
    This is the first time I've heard of the shebang referred to as a "hash-tag line"...
  • schaiba
    schaiba about 8 years
    As other have said, echo $PATH should show you which location is used first.
  • Angel Todorov
    Angel Todorov about 8 years
    To examine the PATH, I like to use tr : '\n' <<<":$PATH"
  • Marius
    Marius about 8 years
    hashtag is a neologism when used in the sense @Bob is familiar. It's derived from one sense of the use of hashmark (with which some commenters also are apparently unfamiliar). For examining the PATH, I use a script except when explaining how PATH works. ymmv.