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

Hi experts,
i am not a professional perl programer. how to interpret the following one.
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' & eval 'exec perl -S $0 $argv:q' if 0;
what is the meaning of $?0, $argv:q, and what is the sequence of the execution of the above script.

Thanks in advance
Chinna Ande

Edit by tye

Replies are listed 'Best First'.
(tye)Re: Confusing 'eval' code
by tye (Sage) on Dec 19, 2001 at 01:40 UTC

    That isn't (mostly) Perl code. That is a "shell script". I think the first eval is trying to figure out what class of shell you are using. The second eval is for Bourne and compatible shells (/bin/sh, ksh, bash). The third eval is for the C shell.

    "$@" is a way of passing in a bunch of arguments with each argument surrounded by quotes so that spaces and other special characters in the arguments don't get reinterpretted. Unfortunately, that passes in the single argument "" if no arguments were specified. ${1+...} means only do the "..." part if $1 is set so ${1+"$@"} is just the standard /bin/sh hack for passing the arguments that were passed to the shell script on to some other program without reinterpretting them.

    $argv:p is the C shells way of doing the same thing.

    eval is used so that Perl can parse that code well enough to see "if 0;" which tells it to not execute any of that code. The shells won't see "if 0;" until they've already run that code, by which point it will be too late because the shell will have been replaced by perl due to one of the execs.

    This looks like a fairly standard "polyglot" (code that is valid in more than one language at once) kluge to get Perl to run a script even when faced with a Unix kernel and shell that are too stupid to recognize the #!/usr/bin/perl line at the top of the script. See perldoc perlrun for more info on this.

            - tye (but my friends call me "Tye")
      Modern shells tend to understand that if $# is zero, "$@" (in quotes) should evaporate entirely. But the first Bourne shell I used would make "$@" an empty string when $# was zero. Thus the need for ${1+"$@"}.

      Bleh.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Short Shameful Confession
by chip (Curate) on Dec 19, 2001 at 14:17 UTC
    I invented the "eval 'exec'" hack.

    I am filled with shame.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Hi Monks,

      I invented the "eval 'exec'" hack

      Don't know about you guys, but little snippets of history like this, are what makes Perl Monks so special to me.

      I am filled with shame.

      Why. Isn't a hack which stands the test of time a Good Hack (tm).

      It reminds me of a statement which Guido van Rossum attributes to his father.

      "There is nothing more permanent than a temporary solution"

      Long life to your hack, Chip. :-)

      cheers

      thinker
        Well, if wetting my pants in public saved the world, I'd do it, but I'd still be pretty ashamed about it....

        As for history: I found Perl while using SCO Xenix/286. I actually did the original port of Perl to a mixed 16/32-bit architecture -- but that's probably a Meditation for another day. SCO Xenix was thoroughly behind the times -- it didn't support the "#!" feature until long after BSD-based systems did. So I started trying to figure out how to run a Perl program as easily as the users of more modern systems because I was, yes, too lazy to type "perl programname" a hundred times a day or create two files for each script. The breakthrough was realizing that Perl and shell both understood eval STRING, but they had different ideas about where statements end.

        BTW, SCO's Bourne shell would use the C shell to run any script whose first character was a "#". That's why Perl to this day ignores a first line that starts with a colon....

            -- Chip Salzenberg, Free-Floating Agent of Chaos

        Hi Chip and Thinker, its a fine philosophical discussion, but i am too young to understand your philosophy. Please come to my world and teach your lessons. Thank you
The Origins of "eval 'exec'" Revealed
by chip (Curate) on Dec 22, 2001 at 00:24 UTC