【问题标题】:diff behaviour between perl 5.8.8 and 5.10.1perl 5.8.8 和 5.10.1 之间的差异行为
【发布时间】:2012-02-10 08:48:53
【问题描述】:

我有这个脚本:

#!/usr/bin/perl
$LOGFILE = "Soccer.txt";
open(LOGFILE) or die("Could not open log file.");
foreach $line (<LOGFILE>) {
    chomp $line;
    ($hour, $match, $idh, $back1, $lay1, $idd, $backx, $layx, $ida, $back2, $lay2) = split(/\s*,\s*/,$line);
    $match =~ s/^(\d{2}):(\d{2}) //g; #/ # fix highlighting
    ($hteam,$ateam) = split(/\s*§\s*/,$match,2);
    $hteam = get_name($hteam);
    $ateam = get_name($ateam);
    $match = "$hteam - $ateam";

    $foo=qq($hour "$match" $idh $back1 $lay1 $idd $backx $layx $ida $back2 $lay2 \n) ;
    print $foo;
}
sub get_name {
# Return the full name for the team, if it exists, otherwise return the original
    my %alias = (
        "Aalen" => "Vfr Aalen",
        "Accrington" => "Accrington Stanley",
        "Accrington St" => "Accrington Stanley",
        "Adelaide Utd" => "Adelaide United Fc"
    );
    return $alias{$_[0]} // $_[0];
}

这个脚本在我的系统中完美运行:

perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi

现在我想在不同的系统中运行它:

perl, v5.8.8 built for x86_64-linux-thread-multi

当我尝试运行它时,我得到了这个错误:

Search pattern not terminated at ./scriptbd.robot.pl line 458.

为什么我会收到错误消息?

【问题讨论】:

  • 您发布的程序中没有 458 行。 :)
  • 如果将 '§' 替换为标准 ASCII 字符会怎样?
  • 你真的应该以use strict; use warnings;开始你的脚本

标签: perl


【解决方案1】:
return $alias{$_[0]} // $_[0];

// operator was added in 5.10。要使其适用于早期的 Perls,请重写它:

return (defined $alias{$_[0]}) ? $alias{$_[0]} : $_[0];

【讨论】:

  • 如果所有哈希值的计算结果都为 false,则可以使用逻辑或 (||) 将其写得更小。但这没什么意义,因为我们只是在考虑如何使用旧版 Perl 解决问题。
猜你喜欢
  • 2014-11-21
  • 1970-01-01
  • 2014-07-25
  • 2020-03-14
  • 2011-11-04
  • 2017-10-09
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多