在 pm 模块中包含文件
我对 Perl/Fastcgi 完全陌生。
我有一些 pm 模块,必须向其中添加大量脚本,并且随着时间的推移,它会不断增长。因此,我需要一个使管理更容易的结构。
因此,我想在某种可以包含的目录结构中创建文件。我希望我包含的文件与我执行包含的文件中写入的文本完全相同。
我尝试过“做”、“使用”和“要求”。我想要包含的实际文件位于 Perl 正在查找的目录之一中。(使用 perl -V 验证)
我已经在 BEGIN {} 内部和外部尝试过。
我该怎么做?是否有可能在 pm 文件中包含 pm 文件?它必须是我包含的 pm 文件还是可以是任何扩展名?
我尝试了多种方法,下面是我最后的尝试。
Config.pm
package Kernel::Config;
sub Load {
#other activities
require 'severalnines.pm';
#other activities
}
1;
multiplenines.pm
# Filter Queues
$Self->{TicketAcl}->{'ACL-hide-queues'} = {
Properties => {
},
PossibleNot => {Ticket => { Queue =>
['[RegExp]^*'] },
},
};
1;
我在 Apache 的 error_log 中没有收到任何与此相关的错误。尽管如此,代码仍然无法被识别,就像我将其放入 Config.pm 文件中一样。
我不打算开始大量编程,只是在第三方应用程序中进行一些管理。尽管如此,我还是四处搜寻,试图了解它如何与包含文件一起工作。 multiplenines.pm 是否被认为是一个 perl 模块,我是否需要使用像 h2xs 或类似的程序才能“创建”该模块(告诉你,完全是新手......)?
提前致谢!
I am totally new to Perl/Fastcgi.
I have some pm-modules to which will have to add a lot of scripts and over time it will grow and grow. Hence, I need a structure which makes the admin easier.
So, I want to create files in some kind of directory structure which I can include. I want the files that I include will be exaclty like if the text were written in the file where I do the include.
I have tried 'do', 'use' and 'require'. The actual file I want to include is in one of the directories Perl is looking in. (verified using perl -V)
I have tried within and outside BEGIN {}.
How do I do this? Is it possible at all including pm files in pm files? Does it have to be pm-files I include or can it be any extension?
I have tried several ways, included below is my last try.
Config.pm
package Kernel::Config;
sub Load {
#other activities
require 'severalnines.pm';
#other activities
}
1;
severalnines.pm
# Filter Queues
$Self->{TicketAcl}->{'ACL-hide-queues'} = {
Properties => {
},
PossibleNot => {Ticket => { Queue =>
['[RegExp]^*'] },
},
};
1;
I'm not getting any errors in the Apache's error_log related to this. Still, the code is not recognized like it would be if I put it in the Config.pm file.
I am not about to start programming a lot, just do some admin in a 3rd party application. Still, I have searched around trying to learn how it works with including files. Is the severalnines.pm considered to be a perl module and do I need to use a program like h2xs, or similar, in order to "create" the module (told you, totally newbie...)?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常创建自己的模块前缀——以项目或我工作的地方命名。例如,您可以将所有内容都放在 Mu 下,其模块名称类似于
Mu::Foo
和Mu::Bar
。使用多个模块(不要尝试将所有内容都保存在一个文件中)并使用*.pm
后缀命名模块。然后,如果
Mu
目录与您的程序位于同一目录中,则只需执行以下操作:如果它们位于其他目录中,您可以执行以下操作:
为什么肯定是的。
这是一个非常非常糟糕的主意。你最好使用包机制。也就是说,将每个模块声明为单独的包名称。否则,您的模块中将包含一个变量或函数,您的脚本将覆盖该变量或函数,而您将永远不会知道它。
在 Perl 中,您可以通过在模块名称前面加上前缀来引用模块中的变量。 (例如 File::Find 就是这样做的。例如
$File::Find ::Name
是找到的文件的名称,这不会污染您的命名空间。如果您确实希望模块的函数和变量位于您的命名空间中,请查看 @EXPORT_OK Exporter 中的列表变量。您想要导入到模块的命名空间中的所有变量和函数,但是,这不是自动的,您必须将它们列在
use
语句旁边,这样您就更有可能导入它们。了解它们。 Exporter 并不太难,在您的模块中,您可以输入:然后,在您的程序中,您可以输入:
现在您可以访问
convert
、$fundge
和@ribitz
就好像它们是主程序的一部分一样。但是,您现在已经记录了您正在从Mu::Foo
中提取这些子例程和变量。(如果您认为这很复杂,那么很高兴我没有告诉您您确实应该在模块中使用面向对象的方法。这确实是最好的方法。)
I usually create my own module prefix -- named after the project or the place I worked. For example, you might put everything under Mu with modules named like
Mu::Foo
andMu::Bar
. Use multiple modules (don't try to keep everything in one single file) and name your modules with the*.pm
suffix.Then, if the
Mu
directory is in the same directory as your programs, you only need to do this:If they're in another directory, you can do this:
Why certainly yes.
That's a bad, bad idea. You are better off using the package mechanism. That is, declare each of your module as a separate package name. Otherwise, your module will have a variable or function in it that your script will override, and you'll never, ever know it.
In Perl, you can reference variables in your modules by prefixing it with the module name. (Such as File::Find does. For example
$File::Find::Name
is the found file's name. This doesn't pollute your namespace.If you really want your module's functions and variables in your namespace, look at the @EXPORT_OK list variable in Exporter. This is a list of all the variables and functions that you'd like to import into your module's namespace. However, it's not automatic, you have to list them next to your
use
statement. That way, you're more likely to know about them. Using Exporter isn't too difficult. In your module, you'd put:Then, in your program, you'd put:
Now you can access
convert
,$fundge
and@ribitz
as if they were part of your main program. However, you now have documented that you're pulling in these subroutines and variables fromMu::Foo
.(If you think this is complex, be glad I didn't tell you that you really should use Object Oriented methods in your Modules. That's really the best way to do it.)
看看Exporter,它可能会给你一个很好的解决方案!
Take a look at Exporter, it will probably give you a good solution!