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

Hi monks

time again to ask for your knowledge. I am encountering the following problem with an executable created with pp on macOS. The executable needs to read files (for example parameters files) which are in the same directory of the executable. If I run the script from the command line, the files are read without problems. If I run the executable, the files cannot be read. So there must be some problem with my paths. To explain this, a simple Tk example with a button and an icon:

use strict; use warnings; use Tk; use Tk::PNG; my $png = 'myicon.png'; my $mw = Tk::MainWindow->new(); my $icon = $mw->Photo(-file => $png); my $btn = $mw->Button( -image => $icon, ); $btn->pack(); $mw->MainLoop();

I create my executable simply with the command:

pp -o TEST myscript.pl

When I run the executable I get the following error:

couldn't open "myicon.png": no such file or directory

This of course signals that it can not find the file. From the command line however it works.

What should I consider to solve this?

Replies are listed 'Best First'.
Re: pp macOS external file
by marto (Cardinal) on Apr 20, 2018 at 12:18 UTC

    I'm not a Tk guy at all, and I'm not mad on this work around, but you can detect if something is running via PAR/pp, and append the dynamic path it extracts includes to, here's a short example:

    use strict; use warnings; my $pardir; if (exists $ENV{PAR_PROGNAME}){ # running via PAR/pp $pardir = $ENV{PAR_TEMP}; } use Tk; use Tk::PNG; my $png = 'myicon.png'; if ( $pardir ){ $png = $pardir . '/inc/' . $png; } my $mw = Tk::MainWindow->new(); my $icon = $mw->Photo(-file=> $png); my $btn = $mw->Button( -image => $icon, ); $btn->pack(); $mw->MainLoop();

    Packaged with:

    pp -x -a "myicon.png" -o test tk.pl

    This works for me, perl v5.22, up to date pp. There's probably a better way to do this.

      Thank you, marto. Your solution works for me (perl v.5.16). However, for the sake of easiness (I have 50 or 60 files!), I would prefer the executable to read the files from an external folder. This is not a problem with pp on Windows, so - I guess - there should be a way to achieve this on macOS too.

        If you mean that you have a directory containing some resources (e.g. /app/logos/ or C:\productname\logos) detect OS ($^O) and prepend the path accordingly. Then you wouldn't need the environment variable stuff in that example.

Re: pp macOS external file
by learnedbyerror (Monk) on Apr 22, 2018 at 20:33 UTC

    I don't have much experience with pp, so the following may be of no help because of peculiarities of pp.

    The module FindBin may be of assistance. It's purpose is to locate the directory of the original perl script.

    use FindBin; my $script_dir = $FindBin::Bin; or use FindBind qw($Bin); my $script_dir = $Bin

    lbe