in reply to Confusing 'eval' code

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")

Replies are listed 'Best First'.
Re: (tye)Re: Confusing 'eval' code
by chip (Curate) on Dec 21, 2001 at 04:17 UTC
    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