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

> what's wrong with the second return value from caller?

in my tests it's relative to the CWD at start, pretty much like $0.

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

  • Comment on Re^2: Do modules know the callers full path?

Replies are listed 'Best First'.
Re^3: Do modules know the callers full path?
by haukex (Archbishop) on Feb 15, 2023 at 21:03 UTC
    in my tests it's relative to the CWD at start

    Right, which is why abs_path((caller)[1]) should work in a lot of cases, but certainly not all, which is why I asked OP if perhaps they have one of those cases.

    Of course, since FindBin tries to give a correct answer, it should be possible to simply steal its code, change it so it doesn't just work on $0, and then apply it to caller in a module's import - if someone hasn't done this already.

      > Right, which is why abs_path((caller)[1]) should work in a lot of cases,

      Please correct me, but how is this better than abs_path($0) ?

      AFAIK is abs_path the problematic part, because the CWD might have changed after a chdir

      I suppose that $0 is even better, since caller might only show an intermediate module instead of the root script (untested)

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

        Please correct me, but how is this better than abs_path($0) ?

        Because it answers the OP's question... "Is there a way to get a module to return the full system path of the script using the module?"

        $ cat lib/Foo/Bar.pm package Foo::Bar; use warnings; use strict; use Cwd qw/abs_path/; sub foo { my $callerfile = (caller)[1]; print "DEBUG: caller=<$callerfile> \$0=<$0>\n"; return abs_path($callerfile); } 1; $ cat y.pl use warnings; use strict; use Foo::Bar; print Foo::Bar::foo(), "\n"; $ cat x.pl use warnings; use strict; do './y.pl'; $ perl -Ilib x.pl DEBUG: caller=<./y.pl> $0=<x.pl> /tmp/y.pl