how to use shebang with @ARGV?

M

Mark_Galeck

Look at foobar.pl: (on WinXP and Strawberry Perl)

#!perl
print $#ARGV + 1 . " command-line arguments.\n";


D:\Perl>perl foobar.pl 1 1
2 command-line arguments.

D:\Perl>foobar.pl 1 1
0 command-line arguments.



Why is this so? How can I use @ARGV and shebang?

Thank you for your insights! Mark
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Mark_Galeck wrote:

Look at foobar.pl: (on WinXP and Strawberry Perl) #!perl print $#ARGV + 1 . " command-line arguments.\n"; D:\Perl>perl foobar.pl 1 1 2 command-line arguments. D:\Perl>foobar.pl 1 1 0 command-line arguments. Why is this so? How can I use @ARGV and shebang? Thank you for your insights! Mark

What makes you think that Windows cares about shebang?
 
M

Mark_Galeck

What makes you think that Windows cares about shebang?--Andrew DeFaria

That #!perl works at all.
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Mark_Galeck wrote: What makes you think that Windows cares about shebang?--Andrew DeFaria
That #!perl works at all.
#!perl doesn't work at all in Windows. You must have a file association for .pl files. Try this. Rename that file to remove it's .pl extension - let's call it say mark_galeck - no extension at all. Then try running it. My bet would be that it doesn't run at all.

So then your Perl script is not being exec(1)'ed in the "standard" way thus I would not expect it to behave in a standard fashion.

Another test: take your Perl script to a Unix system. Bet it works there. That's because a true exec(1) is being used.
 
F

fred

Look at foobar.pl: (on WinXP and Strawberry Perl)

#!perl
print $#ARGV + 1  . " command-line arguments.\n";

D:\Perl>perl foobar.pl 1 1
2 command-line arguments.

D:\Perl>foobar.pl 1 1
0 command-line arguments.

Why is this so?  How can I use @ARGV and shebang?

Thank you for your insights!  Mark


The #! line is not used by Windows to find the interpreter.
On Windows you need to associate the ".pl" file extension
with your perl.exe which your 2nd example shows you've done.

The problem you have shown is that the command line arguments
are not being passed to the interpreter.

Fire up regedit, and find the key:

HKEY_CLASSES_ROOT\Applications\perl5.10.0.exe\shell\open\command

(yours might be for perl.exe or perl5.8.7.exe etc.)

It needs to be of the form:

"C:\strawberry\perl\bin\perl5.10.0.exe" "%1" %*

(obviously you'll need to use the path to your perl.exe :)

It's the "%*" that represents the comand line arguments that
get passed to the interpreter.

Additionally, you may want to add ".pl" to your PATHEXT environment
variable so that you can then run "foobar" rather than "foobar.pl";
and by adding the directory containnig foobar.pl to your PATH you
will be able to run it as "foobar" from any directory.

HTH.
 
S

sln

The #! line is not used by Windows to find the interpreter.
On Windows you need to associate the ".pl" file extension
with your perl.exe which your 2nd example shows you've done.

The problem you have shown is that the command line arguments
are not being passed to the interpreter.

Fire up regedit, and find the key:

HKEY_CLASSES_ROOT\Applications\perl5.10.0.exe\shell\open\command

(yours might be for perl.exe or perl5.8.7.exe etc.)

It needs to be of the form:

"C:\strawberry\perl\bin\perl5.10.0.exe" "%1" %*

(obviously you'll need to use the path to your perl.exe :)

It's the "%*" that represents the comand line arguments that
get passed to the interpreter.

Additionally, you may want to add ".pl" to your PATHEXT environment
variable so that you can then run "foobar" rather than "foobar.pl";
and by adding the directory containnig foobar.pl to your PATH you
will be able to run it as "foobar" from any directory.

HTH.

Checklist:
1. Add association file extension .pl "%1" %*
2. Add .pl to PATHEXT (now an executable)
3. Add directory to PATH
4. Open a cmd window, type perl foobar
5. Double click .pl file from explorer to edit

Very nice, no confusion whatsoever.
-sln
 
M

Mark_Galeck

thank you everybody, this is great, I have another rather silly (I am
sure) question about sockets, in a moment
 
S

sln

thank you everybody, this is great, I have another rather silly (I am
sure) question about sockets, in a moment

Hey I got a socket question for you. Besides handshaking, what layers
if any do you send packetised over the connection. Do you ever do
private socket talk?

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top