Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: pp macOS external file

by marto (Cardinal)
on Apr 20, 2018 at 13:27 UTC ( [id://1213246]=note: print w/replies, xml ) Need Help??


in reply to Re^2: pp macOS external file
in thread pp macOS external file

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.

Replies are listed 'Best First'.
Re^4: pp macOS external file
by IB2017 (Pilgrim) on Apr 20, 2018 at 14:03 UTC

    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.

      "So, the path for this icon should already be correct (and from the Terminal it works okay"

      Not when you run it. What follows is a sort of high level explanation of how this works. An executable generated by pp is essentially a self extracting/executing zip file. You can use the normal unzip command to extract it. The first time it executes it gets extracted to a temporary area. For your problem I had:

      marto@Shemp:~/perlmonks/pp$ ls myicon.png tk.pl

      When packed (as above) and executed this gets extracted to a temporary area, in this case: /tmp/par-6d6172746f/cache-b3da3d80145db3849552916091b87f92bea49883

      In the work around above this is accessible via $ENV{PAR_TEMP}. Within this directory you'll find the various bits & bobs, modules, libraries and so on that are required to run your code without needing perl (and all of those other things) installed.

      Within this is a directory inc:

      drwxr-xr-x 4 marto marto 4096 Apr 20 13:02 . drwx------ 3 marto marto 4096 Apr 20 13:02 .. drwxr-xr-x 16 marto marto 4096 Apr 20 13:02 lib -rw-r--r-- 1 marto marto 7189 Apr 20 13:02 MANIFEST -rw-r--r-- 1 marto marto 212 Apr 20 13:02 META.yml -rw-rw-r-- 1 marto marto 13052 Apr 20 13:02 myicon.png drwxr-xr-x 2 marto marto 4096 Apr 20 13:02 script

      here we see myicon.png. the actual script is in the sub directory 'script'. Another method I've seen people use is to embed the images within their code, and use the Tk::Photo -data option (rather than -file).

Re^4: pp macOS external file
by IB2017 (Pilgrim) on Apr 20, 2018 at 14:22 UTC

    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();

      Are you sure this is the solution you are looking for? If I move this exe into a different location from all the source (the perl and png file) I get:

      marto@Shemp:~/perlmonks$ ./test2 Cannot open '/home/marto/perlmonks/myicon.png' in mode 'r' at Tk/Image +.pm line 21

        I understand what you mean. However, the problem I am facing has a more general character: in the version created with pp I have the problem to "link" any sort of file to my executable (for example a SQLite database the app creates the first time it is run). This fails because of the issue with the path. I actually would like the executable to behave like my Terminal script in terms of paths. Just a side note: on Windows I do not have this issue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1213246]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-07-15 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.