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

But FindBin emphasizes that it should not be used in modules, for the reasons they gave in their documentation. And the OP requested a way to find the path to the calling script from a module ... so as good as your solution is within a script, it's not the recommended solution to put into a module.
  • Comment on Re^3: Do modules know the callers full path?

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

    That's true, but it is not the whole truth. The documentation says (emphasis mine):

    Which also means that you should avoid using FindBin in modules that you plan to put on CPAN.

    Using FindBin in a module works fine. If you use FindBin::again();, then you are safe. The reason why one should not use it on CPAN is because other users of that module might not be aware that they also need to call FindBin::again(); in their code to be safe ... which, on close inspection, they only need for use cases like mod_perl where the "scripts" aren't actually fed to the interpreter on the command line.

    While we are looking at the fine print: Like abs_path($0), FindBin relies on $0. So if used in programs which modify $0 (like e.g. plackup does), then one should hope that it does so in a sensible way.

Re^4: Do modules know the callers full path?
by LanX (Saint) on Feb 15, 2023 at 20:40 UTC
    I looked into FindBin and it's essentially applying Cwd inside a BEGIN block to grab it as soon as possible.

    So it's prone to the same (potential) problem of a chdir prior at compile-time.

    That's a built-in race condition, how soon is soon enough and you can't avoid someone to be sooner ....

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