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

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.

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

Replies are listed 'Best First'.
Re^2: finding the absolute path to a module, from the module's, perspective
by skazat (Chaplain) on Sep 12, 2004 at 04:36 UTC

    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.

        This is pretty weird, but I think I've figured it out;

        doing a,

        print __FILE__;
        inside my module will return something like:
         
        /lib/Stuff/Template.pm
        
        File::Spec has a method called, file_name_is_absolute(), which basically on Unix, looks for a '/' as the first character ->
        sub file_name_is_absolute { my ($self,$file) = @_; return scalar($file =~ m:^/:s); }

        File::Spec will consult this method before it tries to piece together a absolute path; thus, File::Spec isn't even attempting to make an absolute path from the information from __FILE__

        The workaround I've done is simply to hack the first, '/' off.

        Changing my FOO.pm to something like:

        package FOO; use File::Spec; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(bar); sub bar { print __FILE__; print '<p>'; print File::Spec->rel2abs(__FILE__) . "\n"; } 1;
        Will print:

        lib/FOO.pm

        /usr/home/user/www/cgi-bin/lib/FOO.pm

        So, it doesn't look like it's *really* File::Spec's fault, but what would cause Perl to put a '/' at the beginning of the path in my module?

        Hmm...

         

        -justin simoni
        !skazat!