develooper Front page | perl.beginners | Postings from December 2002

Re: what is this?

Thread Previous | Thread Next
December 5, 2002 19:05
Re: what is this?
Message ID:
>From the docs... 

 -S   makes perl use the PATH environment variable to  search
          for  the  script  (unless the name of the script starts
          with a slash).  Typically this is used  to  emulate  #!
          startup  on machines that don't support #!, in the fol-
          lowing manner:

               eval "exec /usr/bin/perl -S $0 $*"
                    if $running_under_some_shell;

          The system ignores the first line and feeds the  script
          to  /bin/sh,  which proceeds to try to execute the perl
          script as a  shell  script.   The  shell  executes  the
          second  line as a normal shell command, and thus starts
          up the perl interpreter.  On some  systems  $0  doesn't
          always  contain the full pathname, so the -S tells perl
          to search for the  script  if  necessary.   After  perl
          locates  the  script,  it  parses the lines and ignores
          them because the variable $running_under_some_shell  is
          never  true.   A  better  construct  than  $*  would be
          ${1+"$@"}, which handles embedded spaces  and  such  in
          the  filenames, but doesn't work if the script is being
          interpreted by csh.  In order to  start  up  sh  rather
          than  csh, some systems may have to replace the #! line
          with a line containing just a colon, which will be pol-
          itely  ignored  by  perl.   Other systems can't control
          that, and need a totally devious  construct  that  will
          work  under any of csh, sh or perl, such as the follow-

               eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
               & eval 'exec /usr/bin/perl -S $0 $argv:q'
                    if 0;

On Fri, 2002-12-06 at 12:49, Tom Allison wrote:
> eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
>      if 0; # not running under some shell
> -----
> What does this do and why?
> -- 
> mummy, n.:
> 	An Egyptian who was pressed for time.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About