Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / July 2004



Tip: Looking for answers? Try searching our database.

[OT?] Shebang question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wren Argetlahm - 22 Jul 2004 06:34 GMT
Sorry if this is off-topic, but I seem to recall
sometime fairly recently (less than a year, more than
a few weeks) someone mentioned a way to "autofind" the
path to perl on the shebang line so you don't need to
hardcode the path. I can't seem to find the post in
the archives. Does any one remember the method or what
thread it might have been mentioned in?

~wren

       
Paul McCann - 22 Jul 2004 07:37 GMT
Hi ~wren,
       you wrote...

> Sorry if this is off-topic, but I seem to recall
> sometime fairly recently (less than a year, more than
[quoted text clipped - 3 lines]
> the archives. Does any one remember the method or what
> thread it might have been mentioned in?

That would probably have been the use of the "env" utility: instead of

#!/usr/bin/perl

or similar you make the first line of your scripts read

#!/usr/bin/env perl

and it'll find the first perl in your environment. See

http://www.informit.com/articles/article.asp?p=29328&seqNum=3

for some more info.

Cheers,
Paul
Jeremy Mates - 24 Jul 2004 17:36 GMT
* Paul McCann <pmccann@maths.adelaide.edu.au>
> That would probably have been the use of the "env" utility: instead of
>
[quoted text clipped - 3 lines]
>
> #!/usr/bin/env perl

Which is not really portable with command line options, due to how
env(1) works on OS X:

$ <test
#!/usr/bin/env perl -wl
print "hello world"

$ ./test
env: perl -wl: No such file or directory

A different trick that I have not tested on other unix systems (as I
just use '#!/usr/bin/perl') is to do the following:

$ <test
exec perl -wl <<EOF
print "hello world"

$ ./test
hello world
Jarkko Hietaniemi - 25 Jul 2004 09:57 GMT
> Which is not really portable with command line options, due to how
> env(1) works on OS X:
[quoted text clipped - 5 lines]
> $ ./test
> env: perl -wl: No such file or directory

That is both against the documentation (env(1)) and the UNIX spec,
so I think a bug report to Apple is in order.  (Actually, env as such
seems to be working okay, so it is more likely a bug in the #! processing.)

> A different trick that I have not tested on other unix systems (as I
> just use '#!/usr/bin/perl') is to do the following:
[quoted text clipped - 5 lines]
> $ ./test
> hello world
Sherm Pendley - 25 Jul 2004 17:13 GMT
> That is both against the documentation (env(1)) and the UNIX spec,
> so I think a bug report to Apple is in order.  (Actually, env as such
> seems to be working okay, so it is more likely a bug in the #!
> processing.)

It's not unique to Mac OS X - I get the same results with Perl 5.8.3 on
Debian "Sarge".

In both cases, running 'env perl -v' from a shell prompt works as
expected.

sherm--
Edward Moy - 25 Jul 2004 19:09 GMT
>> Which is not really portable with command line options, due to how
>> env(1) works on OS X:
[quoted text clipped - 10 lines]
> seems to be working okay, so it is more likely a bug in the #!  
> processing.)

This is one of those historical worts.  See "man execve":

     An interpreter file begins with a line of the form:

           #! interpreter [arg]

     When an interpreter file is execve()'d, the system execve()'s runs  
the
     specified interpreter.  If the optional arg is specified, it  
becomes the
     first argument to the interpreter, and the name of the originally
     execve()'d file becomes the second argument; otherwise, the name  
of the
     originally execve()'d file becomes the first argument.  The  
original
     arguments are shifted over to become the subsequent arguments.  The
     zeroth argument, normally the name of the execve()'d file, is left
     unchanged.

Thus:

    #!/usr/bin/env perl -wl

is interpreted as:

    /usr/bin/env "perl -wl" scriptname

thus, the error message.
------------------------------------------------------------------------
--
Edward Moy
Apple Computer, Inc.
emoy@apple.com

(This message is from me as a reader of this list, and not a statement
from Apple.)
Sherm Pendley - 25 Jul 2004 19:36 GMT
> This is one of those historical worts.

I can ferment it to make beer? Cool! ;-)

sherm--
Rich Morin - 26 Jul 2004 06:26 GMT
>  #!/usr/bin/env perl -wl

Looking in the Camel, I'm not sure what the -l flag is supposed to
be doing for you.  You're not using it with -n or -p, so it isn't
auto-chomping the input lines; you didn't give it an argument, so
it isn't changing the output line terminator.  So, what's it for?

My Perl scripts, FWIW, all have the same general format:

#!/usr/bin/env perl
#
# foo - do fooish things
#
# ...

use Bar::Baz;            # bring in external modules
...

use strict;
use warnings;

{                # "main" routine
    our ($aa, ...        # globals used by main
        ...
        );

    my ($a, ...            # private variables
        ...
       );

    ...                # executable code
}

# foosub - do foosubbish things
#
sub foosub {

    my ($p1, ...) = @_;        # copies of input arguments

    our ($aa, ...        # globals used by foosub
         ...
        );

    my ($b, ...            # private variables
        ...
       );

    ...                # executable code
}

...                # more subs

The "use warnings" line takes the place of the "-w" flag.  The "use
strict" keeps me even more honest (:-).  Putting these lines AFTER
the "use Bar::Baz;" lines(s) prevents me from getting nastygrams
about coding practices in the imported module(s).

By putting the "main" code in a pair of brackets, I cause it to be
indented at the same starting level as the code in the subs.  This
also restricts the scope of its statements (e.g., "our"), so they
aren't global to the file.

To be totally honest, I often start out by using a single "our"
statement above the "main" block.  Then, when the code is stable,
I go back and replace this with specific "our" statements in the
"main" and sub blocks.  This gives me some freedom during the
early stages of development, but controls (and documents) the
visibility of the globals, while the code is in production use.

In any case, I try to keep the use of global variables down, as
overuse of globals can cause confusion...

-r
Signature

email: rdm@cfcl.com; phone: +1 650-873-7841
http://www.cfcl.com        - Canta Forda Computer Laboratory
http://www.cfcl.com/Meta   - The FreeBSD Browser, Meta Project, etc.
http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series

Ken Williams - 26 Jul 2004 13:47 GMT
>>  #!/usr/bin/env perl -wl
>
> Looking in the Camel, I'm not sure what the -l flag is supposed to
> be doing for you.  You're not using it with -n or -p, so it isn't
> auto-chomping the input lines; you didn't give it an argument, so
> it isn't changing the output line terminator.  So, what's it for?

It adds a newline after each print().

 -Ken
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.