home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / preambulate < prev    next >
Encoding:
Text File  |  1991-07-08  |  2.2 KB  |  97 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # preambulate -- wrap a perl program with a system-independent preamble.  
  4. #
  5. # Tom Christiansen tchrist@convex.com
  6.  
  7. $mypath = '/usr/local/bin/perl';
  8. ($myversion, $mypatchlevel) = $] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
  9.  
  10.  
  11. unless (@ARGV) {
  12.     unshift(@ARGV, '-');
  13.     $was_stdin++;
  14. }
  15.  
  16. while ($file = shift) {
  17.     open file || die "can't open $file: $!";
  18.     $mode = (stat(file))[2] & 0777;
  19.     if ($file ne '-') {
  20.     $TMP = "$file.tmp";
  21.     open (TMP, ">$TMP") || die "can't create $TMP: $!"; 
  22.      } else {
  23.     open (TMP, ">&STDOUT");
  24.      } 
  25.      print TMP <<'EOF';
  26. #!/bin/sh -- need to mention perl here to avoid recursion
  27.  
  28. 'true' || eval 'exec perl -S $0 $argv:q';
  29. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
  30. & eval 'exec perl -S $0 $argv:q'
  31.         if 0;
  32.  
  33. VERSION_SANITY: {
  34.     package VERSION_SANITY;
  35.     local($_);
  36. EOF
  37.     print TMP <<"EOF";
  38.     \$PERL_PATH = '$mypath';
  39.     local(\$version, \$patchlevel) = ($myversion, $mypatchlevel);
  40. EOF
  41.  
  42.     print TMP <<'EOF';
  43.  
  44.     local($want_vp) = sprintf("%d.%03d", $version, $patchlevel);
  45.     local($need_perl) = $PERL_PATH . $want_vp;
  46.  
  47.     die "can't get version" 
  48.     unless $] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
  49.  
  50.     sub try_version {
  51.         warn "current perl version ($got_vp) too low, execing $need_perl\n";
  52.         exec $need_perl, $0, @ARGV;
  53.         warn "exec of $need_perl failed: $!";
  54.     }
  55.  
  56.     if ($1 < $version || $2 < $patchlevel) { 
  57.     local($got_vp) =  sprintf("%d.%03d", $1, $2);
  58.         &try_version if -x $need_perl;
  59.         for $need_perl (reverse sort </usr/local/bin/perl* /usr/bin/perl*>) {
  60.             next unless $need_perl =~ /perl(\d+)\.(\d+)$/;
  61.             &try_version if $1 >= $version && $2 >= $patchlevel;
  62.         }
  63.         warn "WARNING: perl version too low: $got_vp < $want_vp; good luck...\n";
  64.     } 
  65.  
  66. eval <<'___VERSION_SANITY___';
  67.  
  68. # BEGIN REAL PROGRAM
  69.  
  70. EOF
  71.  
  72.     while (<file>) {
  73.     print TMP;
  74.     } 
  75.  
  76.     print TMP <<'EOF';
  77.  
  78.  
  79. # END REAL PROGRAM
  80.  
  81. ___VERSION_SANITY___
  82.  
  83. die "$0: OOPS, YOU LOSE: $@" if $@;
  84.  
  85. exit 0;
  86.  
  87. EOF
  88.  
  89.     unless ($file eq '-') {
  90.     close TMP;
  91.     chmod $mode, $TMP;
  92.     rename ($file, "$file.bak") || die "can't rename $file to $file.bak: $!";
  93.     rename ($TMP, $file) || die "can't rename $TMP to $file: $!";
  94.     }
  95. }
  96.