perl scripts fail to read commandline arguments

R

reeddeer

Using XP, SP2, all my previously working perl scripts now fail to
notice any of the commandline arguments. I've never heard of this
happening. I've rebooted the box and removed, then reinstalled
ActivePerl-5.8.1.807-MSWin32-x86.msi, tested again and rebooted again.
I've wondered if SYSIN could be mis-directed? Batch files accept
arguments readily.

Here is a testarg.pl program which fails to detect any arguments:

use strict;
use warnings;

# -------------------------------------
# Main Program
# -------------------------------------

if(@ARGV > 0 )
{
print "1st arg: ~~$ARGV[0]~~ \n";

if ($#ARGV > 1 ) {
print "2nd arg: ~~$ARGV[1]~~ \n";
}
if ($#ARGV > 2 ) {
print "3rd arg: ~~$ARGV[2]~~ \n";
}
print "Done, read ~~$#ARGV~~ args.\n";

}

else
{
die "No arguments were seen! \n";
}
# -- end ------------------------------


I try it with:
testarg argue1 param2
and get: No arguments were seen!
testarg "argue1" "param2"
and get: No arguments were seen!

My co-workers have similar machines and they don't have the problem,
the same test script prints the arguments given.

Any suggestions? tips?
Thanks,
Reeddeer
 
R

reeddeer

I found the problem: in "File Types" the .PL extension was opened by
"\path\perl.exe" instead of by: "\path\perl.exe" "%1" %* so it did
not open the perl script _with_ the arguments that were passed. Sorry
for using the bandwidth, but perhaps this will help someone else in the
future.

Thanks,
reeddeer
 
M

Martin Kissner

reeddeer wrote :
Using XP, SP2, all my previously working perl scripts now fail to
notice any of the commandline arguments. I've never heard of this
happening. I've rebooted the box and removed, then reinstalled
ActivePerl-5.8.1.807-MSWin32-x86.msi, tested again and rebooted again.
I've wondered if SYSIN could be mis-directed? Batch files accept
arguments readily.

Here is a testarg.pl program which fails to detect any arguments:

[snipped]

look at this, I hope it helps:

shell > perl -e 'if($#ARGV > -1) { print $#ARGV +1," arguments were passed\n"}
else { print "no arguments were passed\n"}' hello world
2 arguments were passed

shell > perl -e 'if($#ARGV > -1) { print $#ARGV +1," arguments were passed\n"}
else { print "no arguments were passed\n"}'
no arguments were passed

The index of the last argument is in $#ARGV, so $#ARGV +1 returns the
number of arguments

Best regards
Martin
 
A

A. Sinan Unur

The index of the last argument is in $#ARGV, so $#ARGV +1 returns the
number of arguments

And, of course, one could just use @ARGV in scalar context to get the
number of arguments passed.

Sinan
 
M

Martin Kissner

A. Sinan Unur wrote :
And, of course, one could just use @ARGV in scalar context to get the
number of arguments passed.

Yes, of course. Thanks for pointing out.
This is actually what the OP does in the first if query 'if(@ARGV > 0 )'
(not exactly. but kind of).

But then he does

if ($#ARGV > 1 ) {
print "2nd arg: ~~$ARGV[1]~~ \n";
}

where the query should be

if ($#ARGV > 0 )
or
if ($#ARGV => 1 )
or even
if (@ARGV > 1 )

and so on.
Finally

print "Done, read ~~$#ARGV~~ args.\n";

should be

print "Done, read ~~",$#ARGV+1,"~~ args.\n";

to return the number of arguments correctly (of course scalar @ARGV
would return the same result as $#ARGV+1 does).
The rest of the script seems all right to me.

Best regards
Martin
 
M

Martin Kissner

Abigail wrote :
Martin Kissner ([email protected]) wrote on MMMMCCXI September MCMXCIII
in <URL:??
?? where the query should be
??
?? if ($#ARGV > 0 )
?? or
?? if ($#ARGV => 1 )

I guess you mean

if ($#ARGV >= 1 )

Yes, I do; sorry for the typo.

Best regards
Martin
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top