SPUG: Buggy shebang-line parsing

Tim Maher tim at consultix-inc.com
Sun Jan 16 10:22:38 PST 2005

On Sat, Jan 15, 2005 at 08:35:11PM -0800, David Dyck wrote:
> 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.
>   David
> 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

You don't have a test for the -w option being recognized by
Perl as enabling warnings there, so you're missing the bug demo.

The bug I reported in 7/04 was that the -w on the shebang line with
two spaces before it wouldn't get recognized as a request for enabling
warnings, which can be demonstrated (using 5.8.0 on this box) like so:

tim$ cat no_w	# 2 spaces before -w
#! /usr/bin/perl -s  -w
foo;	# should trigger a warning
print "\$switch is: $switch\n"

tim$ no_w -switch=here	# -s recognized, but not -w
$switch is: here

This time the -s, which has the 2 spaces before it, is overlooked:

tim$ cat no_s
#! /usr/bin/perl -w  -s
foo;	# should trigger a warning
print "\$switch is: $switch\n"

tim$ no_s -switch=here	# -w recognized, but not -s
Unquoted string "foo" may clash with future reserved word at /tmp/no_s line 2.
Useless use of a constant in void context at /tmp/no_s line 2.
Name "main::switch" used only once: possible typo at /tmp/no_s line 3.
Use of uninitialized value in concatenation (.) or string at /tmp/no_s line 3.
$switch is: 

| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
|      Watch for my upcoming book: "Minimal Perl for UNIX/Linux People"    |
| Classes:  2/14: Minimal Perl  2/15: Hashes & Arrays  2/16-18: Int. Perl  |

More information about the spug-list mailing list