in reply to Re^2: Do modules know the callers full path?
in thread Do modules know the callers full path?

> I see that Cwd's cwd, getcwd and abs_path return the path to the script with no script name,

No it returns the current directory where you invoked perl. Well at best, a previous chdir will mess it up.

you should test with perl starting from another dir.

perl \tmp\script.pl

my $script = abs_path($0) is your best bet yet, just cut of the filename if you want the dir only

I don't think you need a BEGIN block if you put this at the very start of your module.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: Do modules know the callers full path?
by Anonymous Monk on Feb 15, 2023 at 21:42 UTC
      No it returns the current directory where you invoked perl.

    I finally changed dir and can now see what you mean! Scary. Of all the things I was trying only abs_path($0) and abs_path((caller)[1]) DWIM. Didn't realize I was opening Pandora's Box. It's fun watching you guys hash it out though, and have more interesting nodes to spend your votes.

    Thank you!

      > Of all the things I was trying only abs_path($0) and abs_path((caller)[1]) DWIM.

      Really? Careful!

      They are different if your module is used by another intermediate module and not the root script.

      > It's fun watching you guys hash it out though,

      This is also hard, because you leave room for interpretation.

      Only you can tell us what you exactly want.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

          > Of all the things I was trying only abs_path($0) and abs_path((caller)[1]) DWIM.

          Really? Careful! They are different if your module is used by another intermediate module and not the root script.

        As I'm testing it now, the module is used by a script. But it will also be used by an intermediate module! Without testing it I'm not sure which will work in both cases, or if each case requires its own solution...