[oak perl] Perl invocation - a related bit of scripting

Michael Paoli mp at rawbw.com
Sat Sep 10 12:09:40 PDT 2005


While not exactly (mostly) Perl code, I managed to create this bit of code
for dealing with a rather annoying problem.

The problem - lots of systems, all of which theoretically have a working
Perl installed ... at least somewhere, ... but which due to history and
(mis-)administration also contain many broken or semi-broken Perl installations.
Thus the matter of easily determining and using a reasonably sane and
apparently/presumably non-broken Perl (e.g. one that can handle:
$ perl -e '{use strict;}'
without barfing and throwing various errors).

Anyway, I came up with a bit of a long invocation at the start of the scripts.
References, some notes, ... then the code.
references:
perlrun(1)
sh(1)

Notes: How and where a "working" perl is checked for is likely highly
environment dependent.  This code has been quite suitable for at least
one particular environment, but no general guarantees are made (suitable
tweaking/expansion may be appropriate for other environments).

And the code:
:
# try to find and use a reasonably sane perl:
eval '
    # we do not want to use shell variables / named parameters here,
    # as those could conflict with environment variables,
    # so that makes this code a bit longer than it might otherwise be

    # first try what (if anything) our PATH finds first
    if >>/dev/null 2>&1 perl -e "{use strict;}"; then
        #that seems good - go for it
        exec perl -S "$0" ${1+"$@"}
    else
        # and if that does not work, then ...
        case X`uname -s` in
            # for flavors that do not provide (non-ancient) Perl with the
            # operating system, try /usr/local/bin/perl next
            # for completeness, these should probably be tweaked for various
            # uname -s and uname -r versions, and other version/revision factors
            XHP-UX|XSunOS)
                if >>/dev/null 2>&1 /usr/local/bin/perl -e "{use strict;}"; then
                    exec /usr/local/bin/perl -S "$0" ${1+"$@"}
                elif >>/dev/null 2>&1 /usr/bin/perl -e "{use strict;}"; then
                    exec /usr/bin/perl -S "$0" ${1+"$@"}
                fi
            ;;
            # for flavors that do provide (non-ancient) Perl with the
            # operating system, try /usr/bin/perl next
            XLinux|XBIG-IP|*)
                if >>/dev/null 2>&1 /usr/bin/perl -e "{use strict;}"; then
                    exec /usr/bin/perl -S "$0" ${1+"$@"}
                elif >>/dev/null 2>&1 /usr/local/bin/perl -e "{use strict;}"; then
                    exec /usr/local/bin/perl -S "$0" ${1+"$@"}
                fi
            ;;
        esac
        # if we succeeded above, the use of exec should render this unreachable:
        1>&2 echo "$0: failed to find good perl, aborting."
        exit 1
    fi
'
    if $running_under_some_shell;

# and then the real perl code starts ...



More information about the Oakland mailing list