Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can't get system command response to return?

Posted on 2003-11-30
8
Medium Priority
?
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

715 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