morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have a script that I run via plackup and want to run it on different machines with the different directory-layouts.

The script accesses some other resources can be found relative to the script-location.

I'd like to be able to simply move the script around and wonder if there is a way for the running script to find it's location.

Or to clarify: Is there a way for a script that is run via "plackup /path/to/somewhere/myscript.psgi" to figure out /path/to/somewhere at runtime in a handler?

Many thanks!

  • Comment on does a script run via plackup knows where it is?

Replies are listed 'Best First'.
Re: does a script run via plackup knows where it is?
by Your Mother (Archbishop) on May 14, 2018 at 16:21 UTC

    Maybe, probably–

    # app.psgi, cp or mv it around and plackitup. :P use Path::Tiny; my $location = path(__FILE__)->absolute; sub { [ 200, [], ["OHAI $location!"] ] }

    Update: I actually think a command line argument or plackup wrapper might be a better choice here. Multiple copies of code is code smell.

      Yep, thanks a lot.

      The only thing you need watch out for is that your above $location is actually an object and when you return that from a Dancer2 handler it confuses Dancer greatly.

      But it solves my my need in an already bad-smelling hack, so never mind code-smell...

        That's partly why I quoted it in the example. It stringifies the object. You can, as you probably already found, do $location->stringify too.

Re: does a script run via plackup knows where it is?
by dsheroh (Monsignor) on May 15, 2018 at 07:24 UTC
    Doesn't FindBin work normally with plackup? I'm not able to easily test this at the moment, but the PSGI/Plack example in CGI::Alternatives uses FindBin in the code and plackup for its start command, which seems like a pretty strong indication that they should play nice together.