【问题标题】:Tutorialspoint.com: Perl - Formats: Scalar found where operator expectedTutorialspoint.com:Perl - 格式:标量在操作员预期的地方找到
【发布时间】:2019-10-05 02:21:04
【问题描述】:

根据this tutorial,这应该可以工作。

我在 5.14(和 5.16)中对此进行了测试,它似乎可以工作。这在 5.28 中不起作用。然而,在perldoc perlform 中,它们似乎仍被记录为工作。

#!/usr/bin/perl

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<< 
$name $age
@#####.##
$salary
===================================
.

select(STDOUT);
$~ = EMPLOYEE;

@n = ("Ali", "Raza", "Jaffer");
@a  = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);

$i = 0;
foreach (@n) {
   $name = $_;
   $age = $a[$i];
   $salary = $s[$i++];
   write;
}

在 5.28,我得到

Scalar found where operator expected at ./test.pl line 6, near "$name $age"
        (Missing operator before $age?)
syntax error at ./test.pl line 6, near "$name $age"
Execution of ./test.pl aborted due to compilation errors.

此功能在哪个版本上进行了修改?这是 Perl 中记录的更改吗?

【问题讨论】:

  • 你少了一个逗号 - $name, $age
  • 你真的在 5.14 和 5.16 中尝试过吗?两者都说不推荐使用无逗号变量列表。所以,是的,缺少的逗号自 5.12 以来已被弃用。添加它,我怀疑它适用于 5.28
  • 我也认为应该是$~ = 'EMPLOYEE';。当然use strict; use warnings; 是你的朋友。

标签: perl


【解决方案1】:

这里的删除是在无逗号的变量列表中,它触发了一个远在 5.14 的警告。

第 6 行不推荐使用无逗号变量列表。

添加逗号使其在 5.26 中工作,

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<< 
$name, $age
@#####.##
$salary
===================================
.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多