Path: utzoo!mnetor!uunet!pwcmrd!skipnyc!skip From: skip@skipnyc.UUCP (Skip Gilbrech) Newsgroups: comp.sources.d Subject: Re: Using perl without "#!" Message-ID: <231@skipnyc.UUCP> Date: 12 Feb 88 04:38:54 GMT References: <184@ateng.UUCP> <1291@devvax.JPL.NASA.GOV> Reply-To: skip@skipnyc.UUCP (Skip Gilbrech) Organization: PaineWebber, New York City Lines: 62 In article <1291@devvax.JPL.NASA.GOV> lwall@devvax.JPL.NASA.GOV (Larry Wall) writes: >I'd suggest a few improvements, however. First, the parens around the 0 >are unnecessary. Second, why not make it self-documenting: > > eval "exec /bin/perl $0 $*" > if $running_via_sh; > >Third, rather than putting it *instead* of #!/bin/perl, put it after that line >if your sh understands # comments. That way the script is more portable. Thanks to Chip & Larry for these ideas: I was planning to suggest to Larry that he consider hacking perl to get it to ignore a line like: exec /bin/perl $0 $* if it was the first non-comment line in a script, but Chip's way doesn't require perl changes, is much more clever, & is even cleaner in an off- white sort of way (:-> I do want to point out some portability problems, though, with leaving #!/bin/perl as the very first line of the script if you're running csh under Xenix or (at least one small implementation of) 'real' System V. Under Xenix, '#!whatever' not only doesn't exec 'whatever', but explicitly tells csh that it should do this script. From the Xenix csh man page: "... C shell will execute ... a standard shell if the first character of a script IS NOT a #". The same man page from Microport System V/AT reads: "... C shell will execute ... a 'standard' shell if the first character of a script IS a ':' or a newline" (my emphasis on IS NOT and IS). Leaving aside the infuriatingly typical inconsistency, the effect is almost the same as under Xenix, except that Chip's original idea to begin the script with 'eval ...' works under Xenix but probably won't under uport unless there's a blank line before it, since csh doesn't understand 'eval'. The trick, of course, under both systems is first to get /bin/sh to read the script, after which everything works fine. Unfortunately, the recommended way to accomplish this with both systems (begin the script with ':') isn't appreciated by perl: syntax error in file TEST at line 1, next token ":" Execution aborted due to compilation errors. The only thing that seems to make everybody happy is a blank line, but of course that screws up the '#!' for those systems that use it, so out comes the editor whenever these files are moved... If anybody comes up with a way to make shell and (by extension & with the above hack) perl scripts portable with NO source changes, I'd love to hear about it! >Now I just have to decide if a2p should put the above eval in for systems >without #!. I do feel sorry for all victims of NIH. Please do (include the 'eval', I mean!)... -- Skip Gilbrech UUCP: uunet!pwcmrd!skip PaineWebber, NYC attmail!skipnyc!skip