Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm developing a perl script which expected to be downloaded by Mac users with a very small knowledge of shell, linux etc, let's say office managers and accountants.

After the downloading the script should be executed just by double-clicking via GUI.

My goal is to make this as painless as possible to non-tech-savvy user.

My doubts are:

  • after the downloading the script won't have the executable bit
  • if the perl executable is not at the default location then I should write something instead of #!/usr/bin/perl. What should I write there? Is there any other way except open a console and type perl ./script.pl ?
share|improve this question
4  
Does env come standard on OSX? If so, you could use #!/usr/bin/env perl which searches the path to find the perl executable. –  John1024 Feb 5 at 20:00
1  
See perldoc perlrun –  Stephane Chazelas Feb 5 at 20:08
    
@John1024, yes - env comes standard with OSX. –  grebneke Feb 5 at 20:16
add comment

2 Answers

up vote 6 down vote accepted

Or, you can have sh take care of it for you:

 #!/bin/sh
 exec perl -x "$0" "$@"

 #!/usr/bin/perl
 ...

Yes, that's sh and Perl all in one file.

From man perlrun:

-x
tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with "#!" and contains the string "perl". Any meaningful switches on that line will be applied.

This approach only assumes the path of sh (which should be the same on any POSIX-compliant OS) and that a non-interactive instance of sh has perl somewhere in its PATH.

As for ensuring the script has the executable bit set, you can always distribute it as a tarball and have your users "right click, extract here" from the GUI. If the tarball contained the script with the executable bit set, the extracted script should have the executable bit set.

share|improve this answer
1  
Note that POSIX doesn't specify the path of sh. /bin/sh may also be a Bourne shell (not on OS/X though) where that syntax should also work except in very old versions where you need ${1+"$@"} instead of "$@" (${1+"$@"} will also work in POSIX shells, but not in some old versions of zsh (/bin/sh was zsh on OS/X at some point)). –  Stephane Chazelas Feb 7 at 13:46
    
@StephaneChazelas "POSIX doesn't specify the path of sh". That's very weird. Thanks for the info, though. –  Joseph R. Feb 7 at 21:19
add comment

For the shebang line, use:

#!/usr/bin/env perl

env will search the PATH at execute the first perl that it finds. If you want to specify any options for perl, just add them to the end of the line above.

If more than one perl is installed, env will run the first one it finds in PATH.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.