0

I have a C# executable that I will call like this:

    UpdateData.exe -getid projectName testName idValue

Internally this calls in my methods...

   GetId(string projectName, string testName, out string idValue);

My problem is I need to call this executable from a Perl script. I am new to Perl, and I can't figure out how to call this function. Here is what I tried.

my $idValue = 0;
my ($cmd) = "UpdateData.exe -getid $projectName $testName idValue";
my $out = `$cmd`;
chomp $out;
print $idValue

And the above line still prints 0. What am I doing wrong here?

4
  • 2
    You never set $idvalue. Out parameters do not work on cli Commented Sep 4, 2014 at 23:03
  • Mr.Tux: UpdateData.exe actually returns int return code for success/errors. I don't see anything in $out. Commented Sep 4, 2014 at 23:05
  • 1
    The only way to get this to work is to write the resulting id to stdout and get this from $out in perl. Commented Sep 4, 2014 at 23:06
  • Okay. This works for me. But is there a better way to deal with this in perl? or is my approach wrong? Commented Sep 4, 2014 at 23:20

1 Answer 1

1

There are several ways depending upon what you require. In your sample above, you are capturing the output from the program (i.e. STDOUT) and placing it into the variable. Depending upon what is written to STDOUT, you would have to parse the results to determine success or failure. But it depends on the program producing output which indicates success or failure as a precondition.

If you know the UpdateDate.exe returns 0 on success and other values on failure (precondition: if it return the same value regardless, it may not be worth capturing any information) and you are not interested in the output, then you can use the function system. Using your example from above, it would look something like:

my $idValue = 0;
my ($cmd) = "UpdateData.exe -getid $projectName $testName idValue";
# Save the returned value from the program execution
$idValue = system($cmd);
# Print out the returned value.
print $idValue;
# shift value by 8 to get the exit code.
if ( 0 != ($idValue >> 8) )
{
   # Issue and take action if required
} 

Here is a link to which gives a excellent explanation of system and back tick differences:

What's the difference between Perl's backticks, system, and exec?

Sign up to request clarification or add additional context in comments.

4 Comments

Been a while, but don't you have to shift (bit-shift?) the system() output to get the real return status? Also, why do the compare like if (0 != $idValue) instead of if ($idValue != 0)? I know in Perl TIMTOWTDI for everything, but wondered if there's an advantage to your syntax.
Yes, based on the documentation you are correct. I will edit and make the correction.
As for the syntax, in this case, probably not. It is a habit a picked up for a colleague in using C/C++. I place constants on the left. Consider the case of if ( $idvalue == 0) but instead you leave out a = by accident { if ($idValue = 0 )}. It becomes an assignment. The value zero is assigned and then evaluated as 0, which is false to the loop will fail. If you have if ( 0 = $idValue) you get a syntax error as you cannot assign the value of $idValue to a constant. I have seen a case where something of this nature occurred. I just thought it was a good idea to do in general.
Thanks for the answer. I didn't get a chance to try this out. I tried the comment solution to write to console and capture that. I will try this soon. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.