Perl Script Portability and Future Testing

Due to pressure from our group, we have to port over a hundred Perl scripts from Sparc to x86. This means changing dozens of shebang lines from #!/home/Perl/bin/perl -wto something else, which is real pain. What is a good way to do this (I can't find anything on Lycos )?

And what happens when we are forced to switch from x86 to something else (like Cray, I suppose)? Is there a way of “future proof”?

+5
source share
4 answers

Changing shebang lines in bulk is not so bad:

#! /usr/bin/perl

use warnings;
use strict;

use File::Find;

sub usage { "Usage: $0 dir ..\n" }

my @todo;
sub has_perl_shebang {
  return unless -f;
  open my $fh, "<", $_ or warn "$0: open $File::Find::name: $!", return;
  push @todo => $File::Find::name
    if (scalar(<$fh>) || "") =~ /\A#!.*\bperl/i;
}

die usage unless @ARGV;
find \&has_perl_shebang => @ARGV;

local($^I,@ARGV) = ("",@todo);
while (<>) {
  s[ ^ (\#!.*) $ ][#! /usr/bin/env perl]x
    if $. == 1;
  print;
}
continue {
  close ARGV if eof;
}

, , s/// , -T, shebang.

, redo:

my $dryrun;
{
  die usage unless @ARGV;
  $dryrun = shift @ARGV, redo if $ARGV[0] eq "-n";
}

find \&has_perl_shebang => @ARGV;
if ($dryrun) {
  warn "$0: $_\n" for @todo;
  exit 1;
}
+4

, #!/usr/bin/env perl #!/usr/bin/perl:

+11

Perl -. XS , .., .

:

  • shebang (perl yourscript.pl).
  • find . -name '*pl' | xargs sed 's/#!\/home\/Perl\/bin\/perl -w/#!\/usr\/bin\/env perl/

, shebang , , , .

+5

Another alternative (maybe it’s easier, although I would be embarrassed to say “better”), of course, to have a soft link /home/Perl/bin/perlto where the actual Perl binary in new systems will be ... this is only possible if you have effective administration tools for the main system, which should be the most normal companies.

+1
source

All Articles