简体   繁体   English

如何在perl的另一个模块中使用一个模块?

[英]How to use one module in another module in perl?

Iam writing a perl script ,in which iam using a module utils.pm and in utils.pm iam using another module DB.pm in which i have a sub routine connetToDB() .我正在编写一个 perl 脚本,其中我使用了一个模块utils.pm,而在utils.pm中我使用了另一个模块DB.pm ,其中我有一个子例程connetToDB()

in utils.pm iam writing在 utils.pm 我正在写作

use DB qw (connectToDB());

and below iam calling that subroutine as在下面我将该子程序称为

my $connection=DB::connectToDB();         (This is line 30)

it is giving an error like follows.它给出了如下错误。 Can someone pls help?有人可以帮忙吗?

Undefined subroutine &DB::connectToDB called at utils.pm line 30.在 utils.pm 第 30 行调用了未定义的子程序 &DB::connectToDB。

you can see the DB.pm code here你可以在这里看到 DB.pm 代码

The direct error in the shown code is that inside qw() you need names .所示代码中的直接错误是在qw()需要names The use pragma使用 pragma

Imports some semantics into the current package from the named module将一些语义从命名模块导入当前包

(my emphasis). (我的重点)。 The "connectToDB()", with parentheses, is not the correct name for the subroutine.带括号的“connectToDB()”不是子例程的正确名称 The error message simply says that it didn't find such a sub.错误消息只是说它没有找到这样的子。

So just drop the parens, use DB qw(connectToDB);所以只需删除括号, use DB qw(connectToDB); . .


The code for the package was added to the question and here are some comments.包的代码已添加到问题中,这里有一些评论。

A similar fix is needed with your @EXPORT : you need the subroutine names (lose & ).您的@EXPORT需要类似的修复:您需要子例程名称(丢失& )。

Perhaps more importantly, you defined the sub using prototypes .也许更重要的是,您使用prototypes定义了 sub。 Your sub is consistent with the prototype you use so I'll assume that it's done on purpose.您的 sub 与您使用的原型一致,所以我假设它是故意完成的。

This is a very advanced (mis?)feature, which is very different from similar looking devices in other languages and is normally not needed.这是一个非常高级(错误?)的功能,它与其他语言中外观相似的设备非常不同,通常不需要。 Chances are that you expect wrong things from prototypes.很可能你期望从原型中得到错误的东西。 Go search for it.去寻找吧。 I'd advise against.我建议反对。

A side note: the prototype-related () and & are not a part of the subroutine name.旁注:与原型相关的()&不是子程序名称的一部分。

The last executed statement that returns in a module must return true, or code won't compile.在模块中返回的最后执行的语句必须返回 true,否则代码将无法编译。 The convention to ensure this is to put 1;确保这一点的惯例是把1; at the end of the package.在包的末尾。

Finally, you shouldn't name the module DB as that namespace is used internally by Perl.最后,您不应该命名模块DB ,因为 Perl 在内部使用该命名空间。 Also, such a generic name is just not good for a module -- it makes it easy to run into conflicts.此外,这样的通用名称对模块不利——它很容易遇到冲突。

use DB qw(connectToDB);使用 DB qw(connectToDB);

my $connection=DB->connectToDB();我的$connection=DB->connectToDB();

or或者

if you have defined a constructor "new" in DB.pm module then如果您在 DB.pm 模块中定义了一个构造函数“new”,那么

my $connection=DB->new();我的$connection=DB->new();

my $result = $connection->connectToDB();我的$result = $connection->connectToDB();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM