Perl脚本可移植性和未来打样

Ari*_*old 5 scripting perl portability future-proof

由于来自我们团队外部的压力,我们必须将超过一百个Perl脚本从Sparc移植到x86.这意味着将数十条绳子线#!/home/Perl/bin/perl -w改为其他东西,这真是一种痛苦.有什么好方法可以做到这一点(我在Lycos上找不到任何东西)?

当我们被迫从x86转移到其他东西(比如Cray,我想)时会发生什么?有没有办法"面向未来"?

Cha*_*ens 11

这是许多人提倡使用的原因之一,#!/usr/bin/env perl而不是#!/usr/bin/perl:


Ped*_*lva 5

Perl是跨平台的.除非您的代码使用已XS编译的代码或系统特定的路径,设施等,否则您应该没问题.

您有两种选择:

  1. 不要使用shebang lines(perl yourscript.pl).
  2. find . -name '*pl' | xargs sed 's/#!\/home\/Perl\/bin\/perl -w/#!\/usr\/bin\/env perl/

无论如何,shebang系列与你正在运行的硬件平台无关,而且所有这些都与你正在运行的shell有关.


Gre*_*con 4

集体改变 shebang 线并没有那么糟糕:

#! /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;
}
Run Code Online (Sandbox Code Playgroud)

根据您所拥有的,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;
}
Run Code Online (Sandbox Code Playgroud)