【问题标题】:HelloWorld example for Perl gives two errors when not specifying use 5.30.0未指定使用 5.30.0 时,Perl 的 HelloWorld 示例给出了两个错误
【发布时间】:2019-05-31 17:42:15
【问题描述】:

NEWBIE:所以今天我开始了一个学习 Perl 的教程,并且在我开始使用 #.###;

之前做得很好

有人能解释一下省略版本时 Perl 的默认值是什么吗?

当我输入 use 5.30.0; 的值时,示例将运行。但是,如果我根本不指定行,我会根据 main 的位置和对 sayit() 的调用得到以下两个错误。

第一个错误如果包 main;打个招呼::sayit()... 在文件顶部。

无法在 helloWorld.pl 第 7 行通过包“hello::sayit”找到对象方法“say”(也许您忘记加载“hello::sayit”?)。

#!/usr/bin/perl
use strict;
#use warnings;
use warnings FATAL => 'all';
# default namespace is main
package main;
say hello::sayit();
say world::sayit();

# new namespace called hello
package hello;
sub sayit {
    return "hello";
}

# new namespace called world
package world;
sub sayit {
    return "world";
}

第二个错误如果包 main;打个招呼::sayit()... 在文件底部。

在 helloWorld.pl 第 20 行,“say hello::sayit”附近的操作员预期的位置找到了裸字

#!/usr/bin/perl
use strict;
#use warnings;
use warnings FATAL => 'all';

# new namespace called hello
package hello;
sub sayit {
    return "hello";
}

# new namespace called world
package world;
sub sayit {
    return "world";
}

# default namespace is main
package main;
say hello::sayit();
say world::sayit();

【问题讨论】:

  • 你运行的是什么版本的 Perl?键入“perl --version”。 “use”命令应该只需要最低限度,并且可能还指定某个兼容性级别,例如当“say”被引入 Perl 时。默认兼容级别不能包含say。如果您不想包含“使用”行,请尝试“打印”而不是“说”。
  • 试试use feature qw(say)。请参阅perldoc feature 了解更多信息
  • 我安装的是 5.30.0 版。

标签: perl version


【解决方案1】:

谁能解释一下省略版本时 Perl 的默认值是什么?

use VERSION;有三个用途:

  • 它执行 Perl 版本的编译时检查。
  • [仅当VERSION 为 v5.10+ 时] 启用功能就像指定了 use feature ":VERSION";
  • [仅当VERSION 为 v5.12+ 时] 它启用限制,就像指定了 use strict; 一样。

默认是不执行任何版本检查,并且不启用任何featuresstrictures


这里我解释一下为什么你发布的 sn-ps 会导致错误。

say 添加到 Perl 时,向后兼容性阻止了它在默认情况下全局可用。它会破坏具有名为say 的子的脚本和模块。因此,在使用之前必须采取措施使say 可用。

say 可以使用use feature qw( say ); 提供。

say 也可以使用use 5.10.0;(及更高版本)提供,因为这会为您启用say 功能(除其他外)。这就是 use 5.30.0; 为您工作的原因。

或者,无需启用该功能即可使用CORE::say 而不是say。 (这需要 5.12+。)

$ perl -e'say "foo"'
String found where operator expected at -e line 1, near "say "foo""
        (Do you need to predeclare say?)
syntax error at -e line 1, near "say "foo""
Execution of -e aborted due to compilation errors.

$ perl -e'use feature qw( say ); say "foo"'
foo

$ perl -e'use 5.10.0; say "foo"'
foo

$ perl -e'CORE::say "foo"'
foo

这是documented

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多