Can't get system command response to return?

I want to have the results of the following system command be loaded into a scalar but it does not seem to do it either way I have tested it.

Here is the code:

----------code-----------------------
sub interpret {
   my $test = `perl -c syn_check`;
   #chomp(my $test = system('perl -c syn_check'));
   $info = "Checking script syntax.";
   open (IN, ">syn_check");
   print IN $t->get("1.0", "end");
   close(IN);
   $info = "Syntax Tested\!";
   if ($test eq "syn_check syntax OK") {
      $status->insert("end", "Syntax passed!\n");
   }else{
      print "\$test equals => " . $test . "\n";
      $status->insert("end", $test);
   }
}
--------------------end code----------------------

All I can get to return is the function status (either a 1 or zero) and not the actual text returned from the perl interpreter. Is there anyway I can make this work? I addes the print statement in the else section to see what the value of $test was at that point so I could see why the script was jumping to that section every time.
LVL 5
ZiaTioNAsked:
Who is Participating?
 
jmcgConnect With a Mentor OwnerCommented:
When you invoke perl with the "-c" flag, the message with "syntax OK" is printed to STDERR, not to STDOUT. That's why it is not appearing in your $test string or in the results.txt file.

Try doing it with file descriptor explicitly redirected; this snippet works okay for me:

   $test = `perl -c syn_check 2>&1`;
   $info = "Checking script syntax.";
   if ($test eq "syn_check syntax OK\n") {
      print "Syntax checked OK\n";
   }else{
      print "\$test equals => ", $test, "\n";
   }

I suspect this approach may not work if you're not on Unix/Linux, since it depends on shell syntax being recognized.
0
 
ahoffmannCommented:
> my $test = `perl -c syn_check`;
should work

> if ($test eq "syn_check syntax OK") {
most likely does not work ('cause $test ends with \n in most cases)
0
 
FishMongerCommented:
You could try changing:

   if ($test eq "syn_check syntax OK") {

to this:

   if ($test =~ /syn_check syntax OK/i) {

or this:

   if ($test =~ /syntax OK/i) {
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZiaTioNAuthor Commented:
Yeah I tried using the match (=~) function also but had same issue. None of it seems to work.
0
 
ZiaTioNAuthor Commented:
It appears that $test is blank when I try to find out what content is in the variable. Why would $test be blank?
0
 
ZiaTioNAuthor Commented:
Hmm.. I also tried the following to make sure something was being returned:

------code-------------
my $test = system('perl -c syn_check > results.txt');
----end code----------

The file results.txt is blank after the script is ran. It is almost like seeing how envoking the perl interpretor is not a direct system call or command that the script is not capturing the response from the interpretor. Would calling the perl interpretor send a response back through a different median than a normal system call like "dir" would? This is the only thing I can think of. Is this a bug in perl itself?

0
 
ZiaTioNAuthor Commented:
Ok it does seem that there is no value being assigned to the variable $test. Maybe because of the reasons in my last post. Here is the newest revision to my code that proves this finding.

---------code--------------------
sub interpret {
   $info = "Checking script syntax.";
   open (IN, ">syn_check");
   print IN $t->get("1.0", "end");
   close(IN);
   #my $dir = system('dir > dir.txt');
   my $test = `perl -c syn_check`;
   #my $test2 = system('perl -c syn_check > results.txt');
   $info = "Syntax Tested\!";
   if ($test) {
      if ($test eq "syn_check syntax OK") {
          $status->insert("end", "Syntax passed!\n");
      }else{
         #print "\$test equals => " . $test . "\n";
         $status->insert("end", $test);
      }
   }else{
      $status->insert("end", "There was an error while receiving response from interpretor!\n");
   }
}
--------end code-------------------------

The script always jumps to the "else" section meaning that $test has no value.
0
 
ZiaTioNAuthor Commented:
That was exactly it man! Good call. Oh and yes your method does work on Win32 also. This was probably one of the most annoying and ongoing problems I have had working with any perl script ever. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.