perldancerplack

How to pass a command line option in Perl Dancer App executed by plackup


If I want to start a Perl Dancer app, I have to run the following command:

perl app.psgi

If I want to pass an option to the application and access it inside the script from @ARGV, I can do it like this:

perl app.psgi --option1 --option2

I can run this app using also "plackup", however I cannot pass the options like when I am running the script using Perl. The @ARGV parameters array is empty.

What can I do? How can I pass command line options to the "app.psgi" script started from "plackup"?

Below is the file of how the script approximately looks like:

#!/usr/bin/env perl


use Dancer2;
use Data::Dumper;
use MIME::Base64 qw( encode_base64 );

use POSIX;

my $system = shift @ARGV || 'default_system';

print "SYSTEM: $system\n";

my $host = '127.0.0.1';
my $port = 5000;

set host => $host;
set port => $port;

get '/expenses' => sub {
    my %params = params;
    return to_json {status => 'OK'};
};

post '/expenses' => sub {
    my %params = params;
    return to_json {status => 'OK'};
};


dance;

Solution

  • It seem like plackup is running the app in a sandbox environment where @ARGV is being erased.

    You can still try use environment variables instead of arguments on the command line. For example, using MY_SYSTEM as an example:

    #!/usr/bin/env perl
    use Dancer2;
    use Data::Dumper;
    use MIME::Base64 qw( encode_base64 );
    use POSIX;
    
    print "SYSTEM: $ENV{MY_SYSTEM}\n";
    # [...]
    

    and then run the app using:

    $ MY_SYSTEM=Foo plackup app.psgi