Solved

Can't get system command response to return?

Posted on 2003-11-30
8
456 Views
Last Modified: 2008-03-17
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.
0
Comment
Question by:ZiaTioN
8 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9846193
> 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
 
LVL 28

Expert Comment

by:FishMonger
ID: 9846385
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
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847151
Yeah I tried using the match (=~) function also but had same issue. None of it seems to work.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847209
It appears that $test is blank when I try to find out what content is in the variable. Why would $test be blank?
0
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847297
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
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9847361
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
 
LVL 20

Accepted Solution

by:
jmcg earned 100 total points
ID: 9847498
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
 
LVL 5

Author Comment

by:ZiaTioN
ID: 9848056
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question