Here is some code I am working on:
#!/bin/perl -w use strict;
my $number_of_args = @ARGV;
open STDERR, ">./caught_errors" or die "Can't create caught_errors: $!";
if ($number_of_args == 0) {
print "Not enough arguments!\n";
print "Usage: ./cedit somefile.C++\n";
print "Usage (optional): ./cedit somefile.C++
outputfile\n";
} elsif ($number_of_args == 1) {
system "g++", $ARGV[0];
open CAUGHTERRORS, "caught_errors" or die "Can't
open file: $!";
chomp(my $line = <CAUGHTERRORS>);
until (my $line =~ /^\s*$/) {
chomp($line = <CAUGHTERRORS>);
print $line."\n";
} close CAUGHTERRORS; } elsif ($number_of_args == 2) {
system "g++", $ARGV[0], "-o", $ARGV[1];
} else {
print "Too many arguments!\n";
print "Usage: ./cedit somefile.C++\n";
print "Usage (optional): ./cedit somefile.C++
outputfile\n";
}
Basically, I am trying automate g++. However, I don't want any error messages printed to the screen. I want them saved in a text file called caught_errors. That part works. After g++ does its business, I want the script to open the text file and print out any messages that it sees in there. This is the part I am having trouble with! This is frustrating because I know I have done something like this before! Right now, in caught_errors, I am getting the following error messages:
hello.c++: In function `int main()': hello.c++:7: error: syntax error before `return' Use of uninitialized value in pattern match (m//) at /home/Christstopher Spears/C++_code/tools/cedit line 16, <CAUGHTERRORS> line 1.
The first two are obviously from the C++ program I wrote incorrectly to test the script. I don't understand the third error message. Any hints?
The third message is from perl. When you redirect STDERR then all of perl's warning and error messages go to the file as well as all child (g++) warning and error messages. There is a FAQ on redirecting STDERR:
perldoc -q "How can I capture STDERR from an external command"
Also you are not reading from the file correctly:
> open CAUGHTERRORS, "caught_errors" or die "Can't > open file: $!"; > chomp(my $line = <CAUGHTERRORS>); > until (my $line =~ /^\s*$/) { > chomp($line = <CAUGHTERRORS>); > print $line."\n"; > } > close CAUGHTERRORS;
Should be:
open CAUGHTERRORS, "caught_errors" or die "Can't open file: $!"; while ( my $line = <CAUGHTERRORS> ) { last if $line =~ /^\s*$/; print $line; } close CAUGHTERRORS;
Which will read from the file without producing the warning message above.
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>