SPUG: Buggy shebang-line parsing

David Dyck david.dyck at fluke.com
Sat Jan 15 20:35:11 PST 2005

On Sat, 15 Jan 2005 at 14:20 -0800, Tim Maher <tim at consultix-inc.com> wrote:

> Earlier today I posted a detailed reply to David's reply to my
> message, but it has never appeared. Anyway, I've attached sample
> programs that illustrate the problem to this message (one of
> which has reported bug-id #30660). Enjoy!


At first I thought that one vs. two spaces shouldn't make a difference,
till I saw in after the -s myself.  I think the problem has to do
with the way perl processes the command line on the script itself.

I started by using strace (on linux) to look at what command line
arguments are being passed to perl, then I fired up gdb
(I saw where the loop that processed the #!perl line breaks
out of the loop when it sees the space, but I haven't thought
of a fix for it yet)

For a while I was confused because I thought the -s would trigger
further -arguments "on the #! line" to become switches to the
program, but now I see that the -s is trying to say to get the switches
from the "real" command line arguments.

In the past, submitted perl5 bugs would make it to the perl5-porters
mailing list, but I didn't see your's their yet.


dd:bugs$ cat 2swx
#! /usr/bin/perl -s  -w
$switch == 1;
warn "Arguments are: @ARGV";
warn "saw -w\n"  if defined $w;
warn "saw -foo\n"  if defined $foo;
while (<>) { print }

dd:bugs$ ./2swx -w -foo bar
Arguments are: bar at ./2swx line 3.
saw -w
saw -foo
data in bar
dd:bugs$ perl ./2swx -w -foo bar
Arguments are: bar at ./2swx line 3.
saw -w
saw -foo
data in bar

More information about the spug-list mailing list