home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / perl / 6925 < prev    next >
Encoding:
Text File  |  1992-11-10  |  4.8 KB  |  166 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!darwin.sura.net!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: scripts should indicate the Perl version (a proposal)
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Nov10.154230.3963@news.eng.convex.com>
  8. Date: Tue, 10 Nov 1992 15:42:30 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <mcook.721326723@fendahl.dev.cdx.mot.com>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 148
  17.  
  18. From the keyboard of mcook@fendahl.dev.cdx.mot.com (Michael Cook):
  19. :How about this idea: There should be a way to specify in a Perl script what
  20. :version of Perl the script was written for.  Then, as new keywords and other
  21. :non-backwardly-compatible functionality are added, old scripts could still
  22. :likely work correctly.  For example, these two scripts might behave
  23. :differently:
  24. :
  25. : #! /usr/local/bin/perl
  26. : version 2.0;
  27. : print "1" | "2";
  28. :
  29. : #! /usr/local/bin/perl
  30. : version 4.0;
  31. : print "1" | "2";
  32. :
  33. :because the | operator's meaning changed between Perl versions 2.0 and 4.0 (or
  34. :whenever).  With the first script, perl would see the "version 2.0" statement,
  35. :and disable that new meaning associated with the | operator.
  36. :
  37. :Make sense?
  38.  
  39. The problem with this is that if you introduce this with perl5,
  40. none of those work with earlier released of perl because they don't 
  41. grok 'version'.  
  42.  
  43. I think it's time to reintroduce my preamulate script, which 
  44. you run on a perl script to make another one which has version 
  45. sanity checking in it.  
  46.  
  47. Of course, like most of my interesting programs, this one is 
  48. subject to the evil "Malformed cmd links" aborting behavior.
  49. Some scripts have the problem, some don't.  If you get a little
  50. one that does, Larry would love to have it.  For example, my
  51. 'man' program works fine this way, but 'zap' blows up.  Sure
  52. wish I knew why, as do many others, so badly it's now a FAQ.
  53.  
  54. #!/usr/local/bin/perl
  55. #
  56. # preambulate -- wrap a perl program with a system-independent preamble.  
  57. #
  58. # Tom Christiansen tchrist@convex.com
  59.  
  60. $mypath = '/usr/local/bin/perl';
  61. ($myversion, $mypatchlevel) = $] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
  62.  
  63.  
  64. unless (@ARGV) {
  65.     unshift(@ARGV, '-');
  66.     $was_stdin++;
  67. }
  68.  
  69. while ($file = shift) {
  70.     open file || die "can't open $file: $!";
  71.     $mode = (stat(file))[2] & 0777;
  72.     if ($file ne '-') {
  73.     $TMP = "$file.tmp";
  74.     open (TMP, ">$TMP") || die "can't create $TMP: $!"; 
  75.      } else {
  76.     open (TMP, ">&STDOUT");
  77.      } 
  78.      print TMP <<'EOF';
  79. #!/bin/sh -- need to mention perl here to avoid recursion
  80.  
  81. 'true' || eval 'exec perl -S $0 $argv:q';
  82. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
  83. & eval 'exec perl -S $0 $argv:q'
  84.         if 0;
  85.  
  86. VERSION_SANITY: {
  87.     package VERSION_SANITY;
  88.     local($_);
  89. EOF
  90.     print TMP <<"EOF";
  91.     \$PERL_PATH = '$mypath';
  92.     local(\$version, \$patchlevel) = ($myversion, $mypatchlevel);
  93. EOF
  94.  
  95.     print TMP <<'EOF';
  96.  
  97.     local($want_vp) = sprintf("%d.%03d", $version, $patchlevel);
  98.     local($need_perl) = $PERL_PATH . $want_vp;
  99.  
  100.     die "can't get version" 
  101.     unless $] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
  102.  
  103.     sub try_version {
  104.         warn "current perl version ($got_vp) too low, execing $need_perl\n";
  105.         exec $need_perl, $0, @ARGV;
  106.         warn "exec of $need_perl failed: $!";
  107.     }
  108.  
  109.     if ($1 < $version || $2 < $patchlevel) { 
  110.     local($got_vp) =  sprintf("%d.%03d", $1, $2);
  111.         &try_version if -x $need_perl;
  112.         for $need_perl (reverse sort </usr/local/bin/perl* /usr/bin/perl*>) {
  113.             next unless $need_perl =~ /perl(\d+)\.(\d+)$/;
  114.             &try_version if $1 >= $version && $2 >= $patchlevel;
  115.         }
  116.         warn "WARNING: perl version too low: $got_vp < $want_vp; good luck...\n";
  117.     } 
  118.  
  119. @VS'info = (__FILE__, __LINE__); eval <<'___VERSION_SANITY___';
  120.  
  121. # BEGIN REAL PROGRAM
  122.  
  123. EOF
  124.  
  125.     while (<file>) {
  126.     print TMP;
  127.     } 
  128.  
  129.     print TMP <<'EOF';
  130.  
  131.  
  132. # END REAL PROGRAM
  133.  
  134. ___VERSION_SANITY___
  135.  
  136.  
  137. if ($@) {
  138.     $_ = $@;
  139.     ($file, $line) =  @VS'info;
  140.     s/ file \(eval\) at line (\d+)/" $file at line " .  ($1 + $line)/eg;
  141.     s/ at \(eval\) line (\d+)/" in $file at line " .  ($1 + $line)/eg;
  142.     die $_;;
  143. }
  144.  
  145. exit 0;
  146.  
  147. EOF
  148.  
  149.     unless ($file eq '-') {
  150.     close TMP;
  151.     chmod $mode, $TMP;
  152.     rename ($file, "$file.bak") || die "can't rename $file to $file.bak: $!";
  153.     rename ($TMP, $file) || die "can't rename $TMP to $file: $!";
  154.     }
  155. }
  156.  
  157. __END__
  158.  
  159. --tom
  160. -- 
  161.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  162. The cause of the riots were the rioters
  163.                 --Vice President Dan Quayle giving an intelligent 
  164.                   analysis of the LA riots.
  165.