http://www.perlmonks.org?node_id=1213239


in reply to pp macOS external file

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.

Replies are listed 'Best First'.
Re^2: pp macOS external file
by Anonymous Monk on Apr 20, 2018 at 13:11 UTC

    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.

        I am a bit confused. The .png file is in the same directory as the executable (I can not have absolute paths, but only relative ones). So, the path for this icon should already be correct (and from the Terminal it works okay). How should the relative path differ in the executable? the OS is darwin.

        Added: the problem should be that the executable sees the relative path starting from its "internal" (I do not how to call it) position that a simple:

        use File::Basename; my $dirname= dirname(__FILE__);

        inside my original script reveals to be different than the physical position of the executable on the machine.

        I have solved it in this way, even if I am not very happy to use the $0 variable

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