未指定使用5.30.0时,Perl的HelloWorld示例给出两个错误

Eli*_*jah 1 perl version

NEWBIE:所以今天我开始学习Perl的教程,并且在我开始使用#。###之前还不错

有人可以解释省略版本时Perl的默认值吗?

当我把使用值设为5.30.0时;该示例将运行。但是,如果我根本不指定该行,则根据main的位置和对sayit()的调用,将出现以下两个错误。

如果主包,则发生第一个错误;在文件顶部说hello :: sayit()...。

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

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

如果软件包为main,则会发生第二个错误;在文件底部打个招呼:: sayit()...。

Bareword在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();
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 9

有人可以解释省略版本时Perl的默认值吗?

use VERSION; 有三个目的:

  • 它执行Perl版本的编译时检查。
  • [仅当VERSIONv5.10 +时有效]它启用功能,就像use feature ":VERSION";指定了功能一样。
  • [仅在VERSIONv5.12 +时有效]它可以启用约束,就像use strict;已经指定了一样。

默认设置是不执行任何版本检查,并且不启用任何功能限制


在这里,我解释了为什么您发布的摘录导致错误。

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
Run Code Online (Sandbox Code Playgroud)

这是有据可查的