在 pm 模块中包含文件

发布于 2024-11-17 05:25:00 字数 1020 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-11-24 05:25:00

我通常创建自己的模块前缀——以项目或我工作的地方命名。例如,您可以将所有内容都放在 Mu 下,其模块名称类似于 Mu::FooMu::Bar。使用多个模块(不要尝试将所有内容都保存在一个文件中)并使用 *.pm 后缀命名模块。

然后,如果 Mu 目录与您的程序位于同一目录中,则只需执行以下操作:

use Mu::Foo;
use Mu::Bar;

如果它们位于其他目录中,您可以执行以下操作:

use lib qw(/path/to/other/directory);
use Mu::Foo;
use Mu::Bar;

是否可以在 pm 文件中包含 pm 文件?

为什么肯定是的。

所以,我想在某种可以包含的目录结构中创建文件。我希望我包含的文件完全像我执行包含操作的文件中写入的文本一样。

这是一个非常非常糟糕的主意。你最好使用包机制。也就是说,将每个模块声明为单独的包名称。否则,您的模块中将包含一个变量或函数,您的脚本将覆盖该变量或函数,而您将永远不会知道它。

在 Perl 中,您可以通过在模块名称前面加上前缀来引用模块中的变量。 (例如 File::Find 就是这样做的。例如 $File::Find ::Name 是找到的文件的名称,这不会污染您的命名空间。

如果您确实希望模块的函数和变量位于您的命名空间中,请查看 @EXPORT_OK Exporter 中的列表变量。您想要导入到模块的命名空间中的所有变量和函数,但是,这不是自动的,您必须将它们列在 use 语句旁边,这样您就更有可能导入它们。了解它们。 Exporter 并不太难,在您的模块中,您可以输入:

package Mu::Foo;
use Exporter qw(import);
our EXPORT_OK = qw(convert $fundge @ribitz);

然后,在您的程序中,您可以输入:

use Mu::Foo qw(convert $fundge @ribitz);

现在您可以访问 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 and Mu::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:

use Mu::Foo;
use Mu::Bar;

If they're in another directory, you can do this:

use lib qw(/path/to/other/directory);
use Mu::Foo;
use Mu::Bar;

Is it possible at all including pm files in pm files?

Why certainly yes.

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.

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:

package Mu::Foo;
use Exporter qw(import);
our EXPORT_OK = qw(convert $fundge @ribitz);

Then, in your program, you'd put:

use Mu::Foo qw(convert $fundge @ribitz);

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 from Mu::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.)

两相知 2024-11-24 05:25:00
if (    'I want the files that I include will be exactly like if the text were written in the file where I do the include.' 
     && 'have to add a lot of scripts and over time it will grow and grow') {
    warn 'This is probably a bad idea because you are not creating any kind of abstraction!';
}

看看Exporter,它可能会给你一个很好的解决方案!

if (    'I want the files that I include will be exactly like if the text were written in the file where I do the include.' 
     && 'have to add a lot of scripts and over time it will grow and grow') {
    warn 'This is probably a bad idea because you are not creating any kind of abstraction!';
}

Take a look at Exporter, it will probably give you a good solution!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文