Perl模块是否知道其安装位置? [英] Does a Perl module know where it is installed?

查看:69
本文介绍了Perl模块是否知道其安装位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始创建一个包含默认电子邮件模板的Perl软件包.

I have started creating a Perl package that contains a default email template.

清单看起来像:

SendMyEmail.pm
SendMyEmail/defualt_email.tt

目前,我知道模块(和模板)在哪里-但是模块本身是否知道它在磁盘上的位置?那该模块可以在没有我帮助的情况下找到默认模板吗?

Currently I know where the module (and the template) are - but does the module itself know where on disk it is? So could the module find the default template without my help?

# This is what I would like to do.
package SendMyEmail;
sub new {
    my ($self, $template) = @_;
    $template ||= $dir_of_SendMyEmail .'/SendMyEmail/default_email.tt'; # ??
}

有没有更好的方式包含模板文本,或者有更好的放置模板的地方?

Is there a better way of including a templates text, or a better place to put the template?

任何对CPAN模块执行类似操作的引用都将受到欢迎.

Any references to CPAN modules that do something similar would be welcome.

谢谢.

推荐答案

每个perl文件都可以访问变量__FILE__,该变量告诉它完整的路径.这样的事情应该可以解决问题:

Each perl file has access to the variable __FILE__ which tells it its full path. Something like this should do the trick:

my $DEFAULT_TEMPLATE;
BEGIN {
    $DEFAULT_TEMPLATE = __FILE__;
    $DEFAULT_TEMPLATE =~ s,\.pm$,/default_email.tt,;
}

不过,如果您只想将数据"文件与模块捆绑在一起,请查看

Still, if all you want is to bundle 'data' files with your modules then take a look at File::ShareDir which provides a way for doing that.

这篇关于Perl模块是否知道其安装位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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