我是perl的新手,我正在尝试做这个练习,但它不起作用.
这是我创建的模块.
#!/usr/bin/perl
use warnings;
use strict;
package Prepare;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw( clean my_print );
sub clean{
return chomp($_[0]);
}
sub my_print{
return print("The Results: $_[0]\n");
}
1;
Run Code Online (Sandbox Code Playgroud)
这是我的脚本test_lib.pl
#!/usr/bin/perl
use warnings;
use strict;
use lib '/home/foobar/code';
use My::Prepare;
print "Enter a word: ";
my $input=<STDIN>;
print "You entered: $varia";
clean($input);
my_print($input);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Undefined subroutine &main::clean called at ./test_lib.pl line 13, <STDIN> line 1.
Run Code Online (Sandbox Code Playgroud)
zdi*_*dim 10
在包命名方面,需要同意三件事:
他们需要"同意"如下.
如果其文件中的包声明是package My::Package;那么包需要用作use My::Package,并且其文件Package.pm在目录中My.
该目录My本身需要位于解释器将搜索的位置,或者我们需要通知它在哪里查找.自定义包通常不在默认搜索的目录中,这是lib pragma的用途:使用您的
use lib '/home/foobar/code';
Run Code Online (Sandbox Code Playgroud)
我期望My目录,用Package.pm它,是在/home/foobar/code目录中.
然后,这是您的示例,具有固定名称和更多调整.
档案/home/foobar/code/My/Prepare.pm:
package My::Prepare;
use warnings;
use strict;
use Exporter qw(import);
our @EXPORT_OK = qw( clean my_print );
sub clean { return chomp $_[0] }
sub my_print { print "The Results: $_[0]\n" }
1;
Run Code Online (Sandbox Code Playgroud)
以及使用此模块的脚本
#!/usr/bin/perl
use warnings;
use strict;
use lib '/home/foobar/code';
use My::Prepare qw(clean my_print);
print "Enter a word: ";
my $input = <STDIN>;
print "You entered: $input";
my $cleaned_input = clean($input);
my_print($cleaned_input);
Run Code Online (Sandbox Code Playgroud)
请通过添加或删除适当的路径组件,将上面的路径调整到您的实际目录结构.这My::特别突出.
几点说明.
#!/usr/bin/perl模块中不需要"shebang"行()
以上出口商的使用有点现代化
我强烈建议使用@EXPORT_OK(而不是@EXPORT),以便所有列出的符号必须由模块的用户专门导入.这对每个人来说都更好
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |