Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to determine absolute path of current Perl file?

by 1nickt (Canon)
on Feb 29, 2016 at 11:44 UTC ( [id://1156435]=note: print w/replies, xml ) Need Help??


in reply to How to determine absolute path of current Perl file?

Hi hakonhagland,

I use the following in scripts that I write on one system to run on another:

use Cwd qw/ realpath /; my $path = realpath($0);
(note that realpath() is just an alias for abs_path())

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: How to determine absolute path of current Perl file?
by hakonhagland (Scribe) on Feb 29, 2016 at 12:38 UTC
    Hi 1nickt! Thanks for your reply. Yes realpath($0) will give me the absolute path of $0 but it will be relative to the current directory, and not the initial current directory. For example, if the user script has changed directory with chdir it will not give a correct path name I think.
      hello, this i was used to do in the script perspective:

      my $origin = File::Spec->file_name_is_absolute($0) ? $0 : File::Spec-> +rel2abs($0) ; my ($drive,$directories,$file) = File::Spec->splitpath( $origin ); my $path = File::Spec->catdir($drive,$directories); ################ check starting path my $current = File::Spec->rel2abs('.'); if ($current ne $path) { (chdir $path and print "OK chdir to $path\n")|| die "FATAL una +ble to change directory to '$path'."; }

      But if now i understand, you want in a module.pm get the absolute path of the script.pl that used it.

      If so you can have in the module a BEGIN block that executes before any attempt for (user's) script to change directory. If in the script there is a chdir in a BEGIN block before loading your module.. you are lost.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Just FYI, in a module "included" by use, executable statements in "file scope" are being run inside a begin block.

        From the documentation for use, use Module LIST; is the same as:

        BEGIN { require Module; Module->import( LIST ); }

        The reason for using a BEGIN block in a module to execute some code before the module is completely compiled. Even without the BEGIN block, any "file scope" statements will get executed before anything after the use is compiled (then run).

        As for the desirability of file scope code in a module, by design, Perl expects a "used" module to run init/sanity-check code at "use time". This is why most modules end with 1;. Perl is expecting the module to "return" a true value if everything is "A-Okay". Otherwise, a false value tells Perl something went wrong. Fetching the current working directory is a reasonable init action. Of course, if you are more comfortable using a BEGIN block, that's fine, too. TIMTOWTDI.

      Then don't change the directory or try to find the current directory at program start by using a BEGIN block.

        Well, all you need to do is to make sure your PM is the first "required" in the list of modules... that should be enough to ensure correct cwd...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found