Perl-软件包/模块问题 [英] Perl - Package/Module Issues
问题描述
使用Perl模块阅读的所有内容中,基本用法是:
From everything I've read on using Perl modules, the basic usage is:
- 扩展名为
.pm
的模块文件,其中包含语句package <name>
,其中<name>
是不带扩展名的模块的文件名. - 使用模块的代码文件包含语句
use <name>;
.
- Module file with
.pm
extension, which includes the statementpackage <name>
, where<name>
is the filename of the module without the extension. - Code file that uses module contains the statement
use <name>;
.
我正在编码的应用程序具有一个主要的代码脚本,该脚本使用大约5个模块.我忘记了在模块中包含package <name>
语句,但是我的代码仍然可以在use <name>
语句中正常运行.我开始使用其中一个模块收到Undefined subroutine
错误,因此我将package语句添加到每个模块中.现在,这些模块的其余部分已停止.有什么作用?
The application I'm coding has one main code script which uses about 5 modules. I had forgotten to include the package <name>
statement in the modules, but my code still ran just fine with the use <name>
statement. I started receiving Undefined subroutine
errors with one of the modules, so I added the package statement to each of the modules. Now the rest of those modules stopped working. What gives?
示例:
mainapp.pl
mainapp.pl
#!/usr/bin/perl
use UtyDate;
my $rowDate = CurrentDate("YYYYMMDD");
UtyDate.pm
UtyDate.pm
#!/usr/bin/perl
package UtyDate;
sub CurrentDate
{
#logic
}
return 1;
运行上面的代码时,出现错误Undefined subroutine &main::CurrentDate called at...
.但是,如果我从UtyDate.pm中删除package UtyDate;
行,则不会出现任何错误.这种情况存在于我的几个但不是全部模块中.
When I run the above code, I get the error Undefined subroutine &main::CurrentDate called at...
. However, if I remove the package UtyDate;
line from UtyDate.pm, I get no error. This situation exists for several but not all of my modules.
显然我没有显示更多的代码,但是我很困惑我没有显示的任何代码会如何影响我在此处显示的包/使用结构.
There's obviously a lot more code I'm not showing, but I'm confused how any of the code I'm not showing could affect the package/use constructs I've shown here.
推荐答案
使用模块时,模块中的代码在编译时运行.然后,在模块的程序包名称上调用import.因此,use Foo;
与BEGIN { require Foo; Foo->import; }
When you use a module, the code in the module is run at compile time. Then import is called on the package name for the module. So, use Foo;
is the same as BEGIN { require Foo; Foo->import; }
您的代码无需使用package
声明即可工作,因为所有代码均在程序包main
下执行,该程序包由主应用程序代码使用.
Your code worked without the package
declarations because all the code was executed under the package main
, which is used by the main application code.
当添加package
声明时,它停止工作,因为您定义的子例程不再在main
中定义,而是在UtyDate
中定义.
When you added the package
declarations it stopped working, because the subroutines you defined are no longer being defined in main
, but in UtyDate
.
您可以通过使用完全限定的名称UtyDate::CurrentDate();
来访问子例程,也可以在use
模块时通过将子例程导入当前名称空间来访问子例程.
You can either access the subroutines by using a fully qualified name UtyDate::CurrentDate();
or by importing the subroutines into the current name space when you use
the module.
UtyDate.pm
UtyDate.pm
package UtyDate;
use strict;
use warnings;
use Exporter 'import';
# Export these symbols by default. Should be empty!
our @EXPORT = ();
# List of symbols to export. Put whatever you want available here.
our @EXPORT_OK = qw( CurrentDate AnotherSub ThisOneToo );
sub CurrentDate {
return 'blah';
}
sub AnotherSub { return 'foo'; }
主程序:
#!/usr/bin/perl
use strict;
use warnings;
use UtyDate 'CurrentDate';
# CurrentDate is imported and usable.
print CurrentDate(), " CurrentDate worked\n";
# AnotherSub is not
eval { AnotherSub() } or print "AnotherSub didn't work: $@\n";
# But you can still access it by its fully qualified name
print UtyDate::AnotherSub(), " UtyDate::AnotherSub works though\n";
有关更多信息,请参见导出器文档.
See Exporter docs for more info.
这篇关于Perl-软件包/模块问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!