Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

finding the absolute path to a module, from the module's, perspective

by skazat (Chaplain)
on Sep 12, 2004 at 02:01 UTC ( [id://390358]=perlquestion: print w/replies, xml ) Need Help??

skazat has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I'm currently converting a templating system that has HTML mixed up with Perl code (yuck!) with HTML::Template.

I'm currently doing this in a few steps:

1) Convert my code in HTML::Template code inline

2) Moving this inline code into a separate file in a separate directory

#1 Was nothing. Cake. All done.

#2 is more difficult, as I don't want to have another Config variable need to be set for my program.

My idea for a solution to this problem was to store the templates in a directory below where my module is, for example, if the module is at:

myprogram/lib/Stuff/MyTemplate.pm

I wanted the templates to be at:

myprogram/lib/Stuff/templates

(at the moment, I don't want the templates servicable, only outside of my program's Perl code)

My idea for a solution to this problem was to find the absolute path of where the Templating functions are (in this case, they're in a separate module, not a part of the *.cgi file)

The problem with this route is that I can't for the life of me, figure out how to find the absolute path to my module; __FILE__ only stores something like,

lib/Stuff/MyTemplate.pm,

not

/home/me/lib/stuff/MyTemplate.pm

This:

use File::Spec; print File::Spec->rel2abs(__FILE__);
Gives the same thing.

So, is there a way to find the absolute path to a module, from the module's, perspective (not from the perspective of the perl script which will use()/require() it)?

 

-justin simoni
!skazat!

  • Comment on finding the absolute path to a module, from the module's, perspective
  • Download Code

Replies are listed 'Best First'.
Re: finding the absolute path to a module, from the module's, perspective
by Zaxo (Archbishop) on Sep 12, 2004 at 02:14 UTC

    The values in the %INC hash contain the paths to the loaded modules.

    $ perl -MFile::Spec -e'print $INC{"File/Spec.pm"},$/' /usr/lib/perl5/5.8.4/File/Spec.pm $

    After Compline,
    Zaxo

Re: finding the absolute path to a module, from the module's, perspective
by PodMaster (Abbot) on Sep 12, 2004 at 02:11 UTC
    This:
    use File::Spec; print File::Spec->rel2abs(__FILE__);
    Gives the same thing.
    Try it again, because that works. If it doesn't work for you, upgrade File::Spec.

    BTW - an absolute path has no "perspective", its an absolute path.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Quote: BTW - an absolute path has no "perspective", its an absolute path.

      What I meant by that statement is that I don't want the absolute path of the script that's calling the module, I want the absolute path of the module the script is calling - that's the difference in perspective.

      Quote: Try it again, because that works. If it doesn't work for you, upgrade File::Spec.

      Well, here's proof of what you're saying, I give you FOO.pm:

      package FOO; use File::Spec; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(bar); sub bar { print File::Spec->rel2abs(__FILE__) . "\n"; } 1;
      And here's a test cgi script:
      #!/usr/bin/perl use FOO; use CGI qw(:standard); print header(); FOO::bar();

      Prints: /home/user/www/cgi-bin/test.pl

      But (of course, sigh), my not as tiny program is still giving me the not-so-absolute path. It's almost as if it thinks the absolute path *starts* at the lib directory.

      Hmm. I'll come post back if I can figure something else out, must be some weird gremlin hunting about.

       

      -justin simoni
      !skazat!

        Well it works for me. File::Spec 0.88, Cwd 2.20.

        What does it print for you from the commandline?

        Are you sure what you think is getting loaded is in fact getting loaded. Try appending

        print "$_\n" for %INC";
        Also, try just printing __FILE__

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://390358]
Approved by Crackers2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found