tags:

views:

269

answers:

5

Inside a Perl script, I want to run a Java program that takes in 2 inputs, which will be passed by the command line.

So I do:

if (scalar @ARGV == 2)
{ print `java myProg $ARGV[0] $ARGV[1]`; } 
elsif (scalar @ARGV == 1) 
{ print `java myProg $ARGV[0]`; }

I works if I enter 2 args, but still hangs if I enter only 1 arg

How should I correct it?

BTW, the Java program works.


I changed my Perl script to:

print scalar @ARGV;
print `$ARGV[0]`;
print `$ARGV[1]`;

And if I run 'perl myPerl.pl abc def' in command line, it only prints 2. And not my two inputs. WHY!?

+3  A: 

The java program is hanging. Try it straight from the command line.

To check if there are enough args you can do:

 die "needs 2 args" unless (scalar @ARGV == 2);

Try this code to prove it is the java:

if (scalar @ARGV == 2)
{ 
    print `cat myProg $ARGV[0] $ARGV[1]`; 
} 
elsif (scalar @ARGV==1) 
{ 
    print `cat myProg $ARGV[0]`; 
}

If all you want to do is print the arguments:

if (scalar @ARGV == 2)
{ 
    print "arguments = [$ARGV[0]] [$ARGV[1]]\n"; 
} 
elsif (scalar @ARGV==1) 
{ 
    print "arguments = [$ARGV[0]]\n"; 
}
tster
Can shorten to `unless @ARGV == 2;` - `scalar()` isn't necessary in scalar context.
Chris Lutz
No reason to shorten things. Perl is already hard enough to understand.
tster
I don't see how it's any easier to understand. A curious person is still going to ask what `scalar()` does, and you're still going to have to explain scalar/list context to them.
Chris Lutz
I have:if (scalar @ARGV == 2){print `java myProg $ARGV[0] $ARGV[1]`;}elsif (scalar @ARGV==1){print `java myProg $ARGV[0]`;}I works if i enter 2 args, but still hangs if i enter only 1 arg.
Saobi
I'm telling you. THE JAVA PROGRAM IS HANGING!
tster
It's not, the java prog works i'm positive.
Saobi
Run the program, then do a "ps -ef | grep java" (if you are on *nix) otherwise open process explorer or task manager and look for the Java process. After that, if you are still sure java isn't hanging let me know.
tster
100 percent sure.
Saobi
change the call from java to something else. That will prove it is java.
tster
May I be the first (oddly) to ask "What are you trying to accomplish?"
Chris Lutz
For now , let's just say i am tryign to write a perl program that prints out all my command line inputs.
Saobi
Is there a reason you can't just do whatever you're trying to do at the start of your Java program?
Chris Lutz
@Chris Lutz: Yes, it does seem odd that Java's not being called directly. It's not like you can't do argument processing in Java, seeing as how arguments come into Java's main method as a String array.
R. Bemrose
He could be creating a command to run from the command line so you don't have to write "java " before the command.
tster
@tster - There are undoubtedly much better ways to do that.
Chris Lutz
meh, not really. I mean, you could do it with a shell script but is that really "better?"
tster
A: 

Use $#ARGV to find the index of the last array element of @ARGV. Add 1 to that and you can get the arg count.

my $argCount = $#ARGV + 1;
if ($argCount == 1)
{
    die "Please enter 2 arguments!\n";
}
bobbymcr
what if they enter 0 args?
tster
@tster: Then $#ARGV should be undef.
R. Bemrose
Of course it wouldn't. $#ARGV would be -1.
tster
That was just a sample showing how to use $#array; it is not meant to be the final production code to check the arguments.
bobbymcr
Just evaluate @ARGV in scalar context. No need to mess around with $#ARGV. `my $argCount = @ARGV;`
daotoad
+7  A: 

To answer your latest question:

print scalar @ARGV;
print `$ARGV[0]`;
print `$ARGV[1]`;

The above code doesn't print the data you expect because you are using backticks with some random input data. Perl is trying to execute the shell commands "abc" and "def" (as per your example) and captures the output of running those commands, of which there isn't any. Try printing the values directly:

print scalar @ARGV;
print $ARGV[0], "\n";
print $ARGV[1], "\n";
Greg Hewgill
Let's hope he really used "abc" and "def".
innaM
+2  A: 

The second code sample is heading off the rails, but the first code sample looks correct, more-or-less. Let's have a look at the Java code, your environment, or anything else that can help us figure out why

{ print `java myProg $ARGV[0] $ARGV[1]`; }

works and

{ print `java myProg $ARGV[0]`; }

doesn't.

mobrule
"off the rails". I love it, I'm going to have to use that tomorrow.
tster
A: 

Try this:

use strict;
use warnings;

my $BASE_COMMAND = 'java MyProgram';

die "Illegal argument count - must have one or two arguments."
    unless @ARGV == 1 or @ARGV == 2;

# Arrays interpolate into space separated strings (unless you change $" - see perlvar)
my $command = "$BASE_COMMAND @ARGV";

print "Running '$command'\n";
print `$command`;
daotoad