This isn't necessarily a Mac-specific question, but I've gotten rusty 
and I'm having a brain fart here.

How can a Perl script reliably, portably resolve the path inside which 
it is running? Not the PWD of the caller, mind you, but the actual 
current full path of the script itself?

Context: I have a pair of utility apps meant to be run in tandem. The 
first is a Pashua questionnaire that shows some forms and saves results 
to a file. The second is a Platypus script that gets admin access (hence 
needing another app -- I couldn't see how to get Pashua to prompt for 
admin access), then uses the results from the first app to do some 
`defaults write ...` & `sudo networksetup ...` type system calls.

Because the apps are meant to be distributed & run together, I've placed 
the second one inside the Contents/Resources/ folder of the first one, 
which finishes with (simplifying slightly):

  my $helper = "$ENV{'PWD'}/../Resources/Helper.app";
  system("/usr/bin/open "$helper'") and die "Couldn't run $helper: $!";

If I first put the first app in /Applications, this works fine.

The problem is I can't rely on $ENV{'PWD'} having something useful. If I 
move the parent app from /Applications (to the Desktop, a USB drive, a 
disk image, or a Samba volume), or if I invoke the Pashua script from a 
shell, then the $helper variable typically ends up with something 
useless (often but not always just "/../Resources/Helper.app") and the 
second app never executes.

I've thought of a few ways around this (e.g. wrap the whole thing in an 
installer package so I can force & depend on a single path), but they 
all seem cumbersome to varying degrees. Ideally, it should behave like, 
say, Firefox, where it will run the same way no matter where the user 
wanted to put (or not bother to put) the app bundle.

Is there a common way to do this? What $ENV variables can be relied on 
to have the full path to the running Perl script from which a working 
relative path can be derived? Is there some other way that this is 
already a Solved Problem, or should I just muddle through?

Any help very much appreciated :-)

Pashua:
http://www.bluem.net/en/mac/pashua/
http://macresearch.org/command_line_tutorial_part_iii_windows_of_opportunity

Platypus:
http://www.sveinbjorn.org/platypus
http://www.macresearch.org/command_line_tutorial_part_i_native_mac_apps_for_command_line_tools
http://www.macresearch.org/command_line_tutorial_part_ii_making_progress_and_finding_options



-- 
Chris Devers

Reply via email to