Re-exec with custom perl
1 direct reply — Read more / Contribute
|
by hv
on Sep 08, 2026 at 09:41
|
I have a bunch of mathematical projects, including lots of perl code. For these I usually use a custom-built perl optimized for speed and with a bunch of useful modules installed - locally that lives as a symlink under '/opt/maths/perl'.
That then leaves me with a tension: for my own convenience I want the shebang line on the perl programs to point to this custom perl, but that then makes like difficult for others when I make the code available on github, particularly since they are designed to be run in-repository without an installation step.
Today I finally decided to do something about that. I want the designated scripts (and only those) to use the custom perl when I run them, but to be usable by others using standard setups, and to be fairly robust against user error. And I want any preamble to be as terse as possible. After a bunch of back and forth with both Claude and Gemini, I ignored all their suggestions and went with this instead.
In a script:
#!/usr/bin/env perl
BEGIN { do "./lib/reexec" for grep $_, $ENV{MATHPERL} }
And in 'lib/reexec':
BEGIN {
if ($ENV{REEXEC_LOOP}) {
delete $ENV{REEXEC_LOOP};
} elsif ($^O =~ /MSWin32/) {
warn "Ignoring re-exec under Windows";
} else {
local $ENV{REEXEC_LOOP} = 1;
exec($_, $0, @ARGV);
die "Could not exec $_";
}
}
I don't know how easy it would be to extend this to work safely under Windows: probably calling system() and then unmunging the exit value would work better then messing with ShellQuote.
Suggestions for improvement welcomed. :)
|