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

Ok... in our codebase our Perl code calls to a dynamic link to get to perl so we can point it to a different version whatever whenever we want... that's fine...

I find, however, that I want to change which perl I am executing only for my sandbox so I can work on switching codebase from perl 5.6.1 to 5.8.3 while allong everyone else to go on their merry way.

It occurred to me that I should be able to replace the dynamic link with a perl or shell script, or C program that could choose which perl to run based on an environment variable that gets set for sandbox code already.

I tried the following:

Wrapper

#!/prod/gnu/bin/perl exec '/prod/gnu/bin/perl', @ARGV;

Caller

#!/home/ant/test/perl print "Hello world\n";
I get
[ant@gums2-sun]$ ./test2.pl ./test2.pl: print: command not found
What am I doing wrong.. or stupid? Worse comes to worse I can search replace all the shebang lines... but I'd like to avoid that. Not really worried about efficiency, this is just temporary.

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re: Conditionally executing different versions of Perl
by blokhead (Monsignor) on Oct 18, 2006 at 21:46 UTC
    To the best of my knowledge, you cannot have two levels of shebang-activated programs. So if you run ./test.pl which starts with #!/some/program, then /some/program cannot also be a shebang program.

    The solution (or at least a solution) is to call /usr/bin/env, which itself does shebang-emulation, instead of calling your wrapper directly.

    /path/to/perlwrapper:

    #!/usr/bin/perl print "wrapper about to execute @ARGV\n"; exec "/usr/bin/perl", @ARGV;
    test.pl:
    #!/usr/bin/env /path/to/perlwrapper print "hello world!\n";
    And then executing ./test.pl should work (at least, it did for me). Changing all your scripts to use env in their shebang line is a pain (you cannot just repace /usr/bin/perl with your wrapper), but it's the only way you can emulate nested shebangs. Otherwise, you'd have to make this version-delegation wrapper into a compiled binary.

    There are also further complications: It appears that if the shebang line doesn't contain the word "perl", then perl itself will try to emulate shebangs and start an infinite loop (re-invoking your version-delegation wrapper). So I got weird results when I first named it "wrapper" instead of "perlwrapper"...

    blokhead

      ++

      You learn something new every day. Have to file this away in my bag-o-tricks.

      --MidLifeXis