Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by hakonhagland (Scribe)
on Feb 29, 2016 at 12:38 UTC ( [id://1156441]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: How to determine absolute path of current Perl file?
by Discipulus (Canon) on Feb 29, 2016 at 12:52 UTC
    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.

        thanks for your clarification RonW

        So using a BEGIN block inside a module to be used it's like to say: as soon as possible or even sooner.

        #TestBegin.pm use strict; use warnings; BEGIN{print "inside BEGIN in the module ",__FILE__,"\n"} print "in the module ",__FILE__,"\n"; 1; perl -e "print qq(in the program $0\n);BEGIN{print qq(BEGIN in the pr +ogram $0\n)}" -MTestBegin inside BEGIN in the module TestBegin.pm in the module TestBegin.pm BEGIN in the program -e in the program -e
        And the only possibility to fail is:
        perl -e "print qq(in the program $0\n);BEGIN{print qq(BEGIN in the pr +ogram\n)}use TestBegin;" BEGIN in the program inside BEGIN in the module TestBegin.pm in the module TestBegin.pm in the program -e

        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.
Re^3: How to determine absolute path of current Perl file?
by Corion (Patriarch) on Feb 29, 2016 at 12:39 UTC

    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://1156441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found