1

I am writing code for work and have run into an interesting issue. I have searched my go-to resources on the topic and have not found a clear answer.

When writing a sub function in a Perl module, is a semicolon required after the closing brace?

As in:

sub printFoo {

print "foo";

};

Is this somehow different than a sub function in a regular Perl script?

When I use the above function in a module without the semicolon I get an error referencing an "undefined subroutine."

I feel confident that in the past I have used similar pieces of code without the semicolon following the closing brace but now I am no longer positive.

A push in the right direction would be great!

4

2 回答 2

5

如果您正在编写匿名函数,那么您需要分号,即。

my $func = sub {
   print "foo";
};

至于普通函数,不需要分号,实际上解析器适用于:

perl -MO=Deparse semi
sub printFoo {
    print 'foo';
}

-

cat semi
sub printFoo {

print "foo";

};
于 2013-05-07T13:24:50.530 回答
0

分号不是必需的,不会影响任何内容。

从同一模块的代码或从使用该模块的主程序调用子程序时,您是否得到“未定义的子程序”?

如果是后者,很可能是因为您没有导出子名称。

于 2013-05-07T13:28:52.070 回答