Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl scripts fail to read commandline arguments

Reply
Thread Tools

perl scripts fail to read commandline arguments

 
 
reeddeer
Guest
Posts: n/a
 
      03-11-2005
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

 
Reply With Quote
 
 
 
 
reeddeer
Guest
Posts: n/a
 
      03-12-2005
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

 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      03-12-2005
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

--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-12-2005
Martin Kissner <> wrote in
news::

>
> 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
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      03-12-2005
A. Sinan Unur wrote :
> Martin Kissner <> wrote in
> news::
>
>>
>> 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.


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

--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
 
Reply With Quote
 
Martin Kissner
Guest
Posts: n/a
 
      03-13-2005
Abigail wrote :
> Martin Kissner () wrote on MMMMCCXI September MCMXCIII
> in <URL:news:> :
> ??
> ?? 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

--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
want to show list of available options and arguments in my commandline utility Santosh Kumar Python 2 09-16-2012 07:29 AM
How to pass commandline arguments to a file loaded with "irb -rfile" ? IƱaki Baz Castillo Ruby 2 12-23-2009 01:30 PM
Passing all extra commandline arguments to python program, Optparseraises exception sapsi Python 4 04-21-2009 07:59 PM
how to write a program that takes arguments from commandline? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 26 02-14-2006 10:23 AM
seLinux in FC3 can make all my shell and perl scripts fail Ross Perl Misc 1 08-19-2005 08:00 PM



Advertisments