Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

calling perl via env....

by perlknight (Pilgrim)
on May 21, 2007 at 17:59 UTC ( #616613=perlquestion: print w/replies, xml ) Need Help??

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

All, calling perl via:
#!/usr/bin/env perl
How do you pass the argument to perl using the syntax above, for example if I wanted to call perl via:
#!/usr/bin/perl -wT
then what is equivalent of it using env command? Or is this not possible? Thanks.

Replies are listed 'Best First'.
Re: calling perl via env....
by jdporter (Chancellor) on May 21, 2007 at 18:03 UTC
Re: calling perl via env....
by almut (Canon) on May 22, 2007 at 00:29 UTC

    The short answer is: this isn't possible with env — at least not in any portable way...

    The problem is that the kernel (which is handling shebang execution) typically only has very rudimentary argument parsing facilities, i.e. it parses the shebang line into an interpreter and one optional argument. In your specific case, this means that /usr/bin/env would be passed "perl -wT" as a single argument, if you were to try something like

    #!/usr/bin/env perl -wT

    as you might have extrapolated from studying the env manpage.

    (Though, as far as I remember, there's one notable exception - Solaris - which does actually handle this correctly, by passing multiple arguments to the interpreter.)

    Thing is that env is not doing any further argument splitting on its own, so it would try to execute a program named "perl -wT", which would of course fail under normal circumstances...  There isn't much you can do about it.

    However, if the idea is to avoid hardcoding the absolute path to Perl, you could use the shell (instead of /usr/bin/env), in combination with Perl's option -x, e.g.

    #!/bin/sh exec perl -Sx -T $0 "$@" #!perl -w # Perl code starts here print "interpreter: $^X\n"; print "script: $0\n"; print "arguments:\n"; printf " '%s'\n", $_ for @ARGV;

    -x makes Perl skip any non-perl garbage up to #!perl. -S would make the script be found along the search path (if so desired).

    Option -T would have to go on the exec perl ... command line (as shown), while other options (like -w) could also go on the #!perl line.

    Also see perlrun for other similar hacks/wrappers...

Re: calling perl via env....
by ikegami (Patriarch) on May 22, 2007 at 05:43 UTC

    I heard env's location is no more standard than perl's. Do you actually have a reason to use env?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://616613]
Approved by shigetsu
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2023-03-22 02:54 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.