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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3606175
  • 2,013
  • 1
  • 13
  • 16

1 Answers1

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?

Community
  • 1
  • 1
Glenn
  • 1,169
  • 1
  • 14
  • 23
  • 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. – jimtut Sep 05 '14 at 05:07
  • Yes, based on the documentation you are correct. I will edit and make the correction. – Glenn Sep 05 '14 at 05:30
  • 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. – Glenn Sep 05 '14 at 05:33
  • 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. – user3606175 Sep 08 '14 at 21:43