Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Perl module know where it is installed?

Tags:

module

perl

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

The MANIFEST looks something like:

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?

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

Thanks in advance.

like image 539
CoffeeMonster Avatar asked Apr 13 '09 05:04

CoffeeMonster


People also ask

How do I find out where a Perl module is installed?

instmodsh command provides an interactive shell type interface to query details of locally installed Perl modules. It is a little interface to ExtUtils::Installed to examine locally* installed modules, validate your packlists and even create a tarball from an installed module.

How do Perl modules work?

There are two main ways of loading a module in Perl. You can do it at compile time and at run time. The perl1 compiler (yes, there is a compiler although it's interpreted language) loads files, compiles them, then switches to run time to run the compiled code.

Where are Perl modules installed in Ubuntu?

You'll find them in /usr/lib/perl/{VERSION}/ as well as /usr/lib64/perl/{VERSION}/ . {VERSION} corresponding to the version of Perl. You can get it with perl --version .


1 Answers

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.

like image 77
Emmanuel Rodriguez Avatar answered Oct 05 '22 01:10

Emmanuel Rodriguez