Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

#! question

by rbc (Curate)
on Jan 17, 2002 at 03:19 UTC ( #139379=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I looked at some perl script at work and
I noticed that some of them use this ...

#!/bin/sh -- perl eval 'exec perl -S $0 ${1+"$@"}' if 0;

... is this safe ... am I just
being paranoid?


Replies are listed 'Best First'.
Re: #! question
by VSarkiss (Monsignor) on Jan 17, 2002 at 03:57 UTC

    This is actually for systems that don't support shebang execution.

    If you look very carefully, you'll notice that the eval string is a shell command, not Perl. On systems that don't have shebang, this gets handed to a shell, which hopefully has Perl in its PATH. That shell then exec's perl -S script args where the name was $0 and the args, if any, came from the ${1+"$@"}.

    Now, when it does get handed to Perl, the if 0 on the next line stops it from doing the eval (of course, the shell is line-oriented and never sees the if!) This would also happen if the system started supporting shebang execution and the file was handed to Perl directly in the first place.

    Nasty but effective, no?

      Just in addition to VSarkiss' excellent explanation, I thought I would just add that this is discussed in the perlrun man page, specifically under the -S switch.


      perl -e 's&&[@.]/&&s&.com.&_&&&print'

Re: #! question
by Chmrr (Vicar) on Jan 17, 2002 at 03:32 UTC

    The purpose of this to ensure that it runs correctly on machines which have a broken shell which does not respect the #! line. Thus, one can run it as ./ or perl and it wull function identically.

    It does look rather strange, though, as most versions that do this have #!perl as the first line -- this one seems to be forcing the shell to exec it explicitly. Strange, but I see no reason why it wouldn't work. It might confuse some webservers that arn't overly smart, though.

    Update: I sit corrected. I'd never seen this construction used in that way before, only for broken-shell prevention.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Actually no, not really. The purpose of this is to set a perl script to run on a given machine without knowing where the binary lives, with the limitation that it be in your PATH.

      UPDATE: Further, as covered elsewhere #! is the kernel's responsibility not the shell's.

      perl -pe "s/\b;([st])/'\1/mg"

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2023-12-04 02:48 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (20 votes). Check out past polls.