in reply to Do modules know the callers full path?

As you can tell from the others' replies, the answer is not so simple, especially if the code is changing the current working directory. So my question is: what's wrong with the second return value from caller? If you show us an SSCCE that's representative of your actual code and that has a problem using caller, we can hopefully suggest a fix that works for you.

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

Replies are listed 'Best First'.
Re^2: Do modules know the callers full path?
by LanX (Saint) on Feb 15, 2023 at 20:27 UTC
    > 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

      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

Re^2: Do modules know the callers full path?
by Anonymous Monk on Feb 15, 2023 at 20:58 UTC
      As you can tell from the others' replies, the answer is not so simple, especially if the code is changing the current working directory. So my question is: what's wrong with the second return value from caller?

    The second return value from caller is the name of the script, with no path, same as $0. I see that Cwd's cwd, getcwd and abs_path return the path to the script with no script name, which is what I want. abs_path($0) returns the full path and script name.

    I'd like this to be as bulletproof as possible. Should I use cwd, getcwd or abs_path? Should I use Cwd in a BEGIN block to assign the value of one of those functions to a global variable and if so what's the best was to do all that? Thanks

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

      Again, please show some representative code - see also SSCCE. Functions like getcwd won't be the script's directory if the script is invoked with e.g. perl ../script.pl.

      I'd like this to be as bulletproof as possible.

      I know the feeling well and I often preach to make code as robust a possible. But this is one of those cases where experience has taught me that in 99% of my code, it's in fact ok to rely on $0 and caller, since most users are just going to be calling my script in a completely normal manner like perl script.pl.

      In any case, code like I showed here will work a good portion of the time, important exceptions being when the script is read from STDIN or it's an -e oneliner. As I mentioned here, it would also be possible to adapt FindBin's code to work on caller.

      But I suspect this may also be an XY Problem. Perhaps you can explain why you (think you) need this. A module shouldn't normally need to know the full filename of its caller. Looking at it another way, why can't the script use the more reliable FindBin to determine its own path, and then pass that as an argument to the module?

          But I suspect this may also be an XY Problem. Perhaps you can explain why you (think you) need this. A module shouldn't normally need to know the full filename of its caller. Looking at it another way, why can't the script use the more reliable FindBin to determine its own path, and then pass that as an argument to the module?

        I have a module that outputs a hash of useful information. The info is embedded in the module DATA but it changes (annually I think). The info can be found online so I'm trying to figure out a sane mechanism to update it without having to reinstall the module.

      > 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

          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!