Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture output from system call 1

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
I have a script and am wanting to run:

`perl -c $perl_file`
and
`perl -wc $perl_file`

and capture the output (preferably in an array). I've tried:

@resp = `perl -c $perl_file`;

with no luck! Any ideas?

Thanks much!
 
I wouldn't think it would be real efficient to shell out to call an another perl script. But if those are back ticks that's the way you capture system output.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Maybe it's not the best way. What's the alternative to run a perl command from within a perl script?
 
Are you trying to call a whole other script or call a perl command?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Perl command: 'perl -c' and 'perl -wc'
 
are you writing a script to syntax check another script or something? I'm pretty confused at this point.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You probably need to use the full path to perl. The path depends on the operating system perl is insalled on.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry. Yes, I'm trying to run a syntax check from one perl script on another perl script.

I "think" I figured out the problem. It seems that 'perl -c' outputs to STDERR. With that being said, I guess I'll need to redirect STDERR to a file, then read that file into an array.

Does anyone see a better solution to what I am trying to do? I'm WAY open to ideas!!!!
 
It works for me when I use the correct path to perl inside the backtiks.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, can you give me the exact line of code you are using?

Thanks!
 
I can't, for the life of me, figure this out! Did I mention that I'm running this on Windows? Would that make a difference?
 
Code:
my $com = 'c:\perl\bin\perl.exe -c c:\perl_test\foo.pl';
@foo = `$com`;
print "@foo";

output:

c:\perl_test\foo.pl syntax OK

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It shouldn't make a difference. I use the technique Kevin is showing in windows so I can say it does work with the exception of using a variable in a system call. Realy the only difference I use between Linux and Windows has been the sha-bang line and my end of line characters and the ocational different switch when using a system call or exec.
 
Kevin...that is not working for me at all!?!?!? Here's my code:

Code:
$com = 'C:\Perl\bin\perl.exe -c C:\Users\Administrator\Desktop\Work\tester_syntax.pl';
@synlines = `$com`;
$status = $?;
print "$status\n";
foreach $syn (@synlines) {
  print "$syn\n";
}

The output goes to the screen not into the array. The output looks correct on the screen.

?????
 
Try this one ... I am on my computer with perl so I ran it through and this worked for me.

Code:
my $com = `C:/Perl/bin/perl.exe -c C:/Users/Administrator/Desktop/Work/tester_syntax.pl`;
my @synlines = $com;
my $status = $?;
print "$status\n";
foreach my $syn (@synlines) {
  print "$syn\n";
}

 
Never mind ... I ran it threw checking everything and realized that the same thing is happening. The input is still going to screan and not to the variable $com.
 
The output should go to @synlines not $com. If it is not working I am not sure what the problem is, something to do with your specific Windows environment more than likely.

Go to the DOS prompt and try the code/syntax I posted previously:

Code:
c:\> c:\perl\bin\perl.exe -c c:\perl_test\foo.pl

See if that works or not. Make sure the path to perl is the same on your PC as has been posted, which is the default install for activeperl.

In the DOS windows you must use backslashes in the directory paths.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, everything works just fine...except that the output does not go to the array, it goes to the screen.
 
Perl:
my $com = qx{C:/Perl/bin/perl.exe -c C:/Users/Administrator/Desktop/Work/tester_syntax.pl [red]2>&1[/red]};
Thank you, I'll be here all week...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Sorry, that'll teach me to cut and paste someone else's code...
Perl:
my [red]@[/red]com = qx{C:/Perl/bin/perl.exe -c C:/Users/Administrator/Desktop/Work/tester_syntax.pl [red]2>&1[/red]};

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top