http://qs321.pair.com?node_id=1210960

rje has asked for the wisdom of the Perl Monks concerning the following question:

Many moons ago, I saw a clever Perl programmer use 'which'... ON THE FIRST LINE OF HIS SCRIPT ... to find the perl executable.

In other words, instead of:
#!/usr/bin/perl

He did SOMETHING LIKE
#!which perl

...except it wasn't exactly like that. It was sort of like that. But not exactly.
So I'm trying to figure out what that command was.
I figure you guys probably know it.
Any ideas? Thanks.

Replies are listed 'Best First'.
Re: How do I use 'which' in the hash-bang line?
by haukex (Archbishop) on Mar 15, 2018 at 14:33 UTC

    I usually use

    #!/usr/bin/env perl

    which runs the (first) perl found in the PATH environment variable. See env.

      Note that env does not allow the passing of parameters to the target, so that #!/usr/bin/env perl -w does not work. At least, this was true the last time I tried.

      Regards,

      John Davies

        <update2> Sorry, I got so caught up in the point I was trying to make - that Perl scans the shebang line even when running the script as perl test.pl - that I missed the limitation that the AM pointed out below, that you are right that #!/usr/bin/env perl -w does in fact not work when invoking the script via ./test.pl. </update2>

        so that #!/usr/bin/env perl -w does not work

        It does for me:

        $ cat test.pl #!/usr/bin/env perl -wls print $x+undef; $ perl test.pl -x=5 Use of uninitialized value in addition (+) at test.pl line 2. 5

        From perlrun: 'The #! line is always examined for switches as the line is being parsed. ... Parsing of the #! switches starts wherever "perl" is mentioned in the line.'

        (I'm not sure about the history of this feature at the moment.) Update: The description of this feature was in perl-5.000, although apparently the support for which options are supported there has changed, for example perl58delta says "The command-line options -s and -F are now recognized on the shebang (#!) line." or perl5100delta says "The -C option can no longer be used on the #! line." (Although as choroba pointed out and as the docs say: "It wasn't working there anyway, since the standard streams are already set up at this point in the execution of the perl interpreter.")

      I usually use

      #!/usr/bin/env perl

      Is there any difference from using "which"?

      From the man page of "which" I would expect exactly the same behaviour:

      Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1).

      Update
      difference is run vs. print to stdout.

        which just prints it, env runs it. Big difference.

      I bet that was it! Thanks!