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

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

问题描述

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

这是我创建的模块.

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

这是我的脚本 test_lib.pl

#!/usr/bin/perl使用警告;使用严格;使用库'/home/foobar/code';使用我的::准备;print "输入一个单词:";我的 $input=<STDIN>;print "你输入了:$varia";清洁($输入);我的打印($输入);

我收到此错误:

<上一页>在 ./test_lib.pl 第 13 行第 1 行调用了未定义的子例程 &main::clean.

解决方案

说到包命名,需要约定三点:

  • 包文件的位置和名称

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

  • 使用它的代码中的包的 use 语句

他们需要同意"如下.

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

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

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

我希望包含 Package.pmMy 目录位于 /home/foobar/code 目录中.

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

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

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

还有一个使用这个模块的脚本

#!/usr/bin/perl使用警告;使用严格;使用库'/home/foobar/code';使用 My::Prepare qw(clean my_print);print "输入一个单词:";我的 $input = <STDIN>;打印您输入:$input";我的 $cleaned_input = clean($input);我的打印($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天全站免登陆