Perl 模块创建 - 未定义的子程序 [英] Perl Module Creation - Undefined subroutine

查看:28
本文介绍了Perl 模块创建 - 未定义的子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 perl 新手,我正在尝试做这个练习,但它不起作用.

这是我创建的模块.

#!/usr/bin/perl使用警告;使用严格;包准备;要求出口商;我们的@ISA = qw(Exporter);我们的@EXPORT = qw(clean my_print);子清洁{返回 chomp($_[0]);}子 my_print{return print("结果:$_[0]
");}1;

这是我的脚本 test_lib.pl

#!/usr/bin/perl使用警告;使用严格;使用 lib '/home/foobar/code';使用我的::准备;print "请输入一个词:";我的 $input=;打印您输入:$varia";清洁($输入);my_print($input);

我收到此错误:

<前>未定义的子程序 &main::clean 在 ./test_lib.pl 第 13 行,第 1 行调用.

解决方案

说到包命名,三件事需要达成一致:

  • 包文件的位置和名称

  • 包文件中 package 语句中的名称(命名空间)

  • use 声明用于使用它的代码中的包

他们需要同意";如下.

如果其文件中的包声明为package My::Package;,则该包需要作为use My::Package使用,其文件为My 目录下的 >Package.pm.

这个目录My本身需要位于解释器将搜索的位置,或者我们需要通知它在哪里查找.自定义包通常不在默认搜索的目录中,这就是 lib pragma 的作用: 用你的

使用 lib '/home/foobar/code';

我希望 My 目录(其中包含 Package.pm)位于 /home/foobar/code 目录中.

这里是您的示例,名称固定并进行了一些调整.

文件 /home/foobar/code/My/Prepare.pm :

package My::Prepare;使用警告;使用严格;使用导出器 qw(import);我们的@EXPORT_OK = qw(clean my_print);子清洁 { chomp(@_);返回 @_ }sub my_print { 打印结果:$_[0]
"}1;

以及使用该模块的脚本

#!/usr/bin/perl使用警告;使用严格;使用 lib '/home/foobar/code';使用 My::Prepare qw(clean my_print);print "输入一个词:";我的 $input = <STDIN>;打印您输入:$input";我的 $cleaned_input = clean($input);my_print($cleaned_input);

请根据您的实际目录结构调整上面的路径,方法是添加或删除合适的路径组件.My:: 这个名字特别引人注目.

一些注意事项.

  • 不需要shebang"模块中的行 (#!/usr/bin/perl)

  • 使用上面的 Exporter 更现代一些

  • 我强烈推荐使用@EXPORT_OK(而不是@EXPORT),这样所有列出的符号都必须由模块的用户专门导入.这对每个人都更好

I'm new in perl and I'm trying to do this exercise but it doesn't work.

This is my module that I created.

#!/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]
");
}

1;

And this my script 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);

I get this error:

Undefined subroutine &main::clean called at ./test_lib.pl line 13,  line 1.

解决方案

When it comes to package naming, three things need to agree:

  • location and name of the package file

  • name in the package statement in the package file (the namespace)

  • the use statement for the package in the code that uses it

They need to "agree" as follows.

If the package declaration in its file is package My::Package; then the package need be used as use My::Package, and its file is Package.pm in the directory My.

This directory My itself need be in a location that the interpreter will search, or we need to inform it where to look. Custom packages are usually not in directories that are searched by default, which is what lib pragma is for: With your

use lib '/home/foobar/code';

I'd expect My directory, with Package.pm in it, to be in /home/foobar/code directory.

Then here is your example, with fixed names and with a few more adjustments.

File /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 { chomp(@_); return @_ }

sub my_print { print "The Results: $_[0]
" }

1;

And a script that uses this module

#!/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);

Please adjust paths above to your actual directory structure, by adding or removing path components as suitable. The name My:: is sticking out in particular.

A few notes.

  • there is no need for the "shebang" line (#!/usr/bin/perl) in a module

  • use of Exporter above is a bit more modern

  • I strongly recommend using @EXPORT_OK (instead of @EXPORT), so that all listed symbols must be specifically imported by the user of the module. That is just better for everybody

这篇关于Perl 模块创建 - 未定义的子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆