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

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

Replies are listed 'Best First'.
Re^6: Do modules know the callers full path?
by haj (Vicar) on Feb 15, 2023 at 22:00 UTC

    Your emphasis the script using the module demonstrates the ambiguity of the OP's question. Is this about the script (which we usually identify as whatever is in $0) or is this the file from which a particular subroutine was called, as returned by caller? "Using a module" and "calling a subroutine in a module" are different things and can happen in different files.

      Your emphasis the script using the module demonstrates the ambiguity of the OP's question.

      You're absolutely right of course, which is why in my other reply I asked for even more clarification.

      > is this the file from which a particular subroutine was called,

      NB: caller is not only useful inside a subroutine, at the beginning of a module (i.e. outside any sub) it will report the point of use or require

      That's the basic mechanism for (original) modulinos.

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

Re^6: Do modules know the callers full path?
by LanX (Saint) on Feb 15, 2023 at 21:28 UTC
    Well semantics, but I see your point

    "The script" for me is the root file executed.

    YMMV

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