I've been learning C++ using cygwin on my Windows PC
and thought it would be handy to have a Perl script
that automated features of g++. The script takes one
to two invocation arguments:
./cedit somefile.C++
./cedit somefile.C++ outputfile
cedit runs g++. After the file is compiled,etc., any
error messages are stored in a text file called
"caught_error". cedit then opens caught_error, reads
its contents, and displays the contents to the user.
I did this so I could cut and paste error messages
from the file using a text editor. cedit then asks
the user if s/he would like to launch vi. If the user
selects "yes", s/he is given the option of entering a
line number. After entering the line number, vi
starts up and opens somefile.C++ and caught_error. In
somefile.C++, the cursor should be at the line
designated by the user. If the user did not designate
a line, vi would just start and open the two files.
#!/bin/perl -w
use strict;
sub choose_vi {
print "Use vi?\n";
print "Y/y for yes. Any other key quits: ";
chomp(my $answer = <STDIN>);
if ($answer =~ /^[yY]$/) {
print "Enter line number? (Y for yes):";
chomp(my $answer = <STDIN>);
print "\n";
if ($answer =~ /^[yY]$/) {
print "Enter line number: ";
chomp(my $line_number = <STDIN>);
system "vi", "+".$line_number, $ARGV[0],
"caught_errors";
} else {
system "vi", $ARGV[0], "caught_errors";
}
} else {
print "Goodbye!\n";
}
}
sub read_ce {
print "Errrors:\n";
open CAUGHT, "caught_errors" or die "Can't open
file: $!";
my @lines= readline(CAUGHT);
my $number_lines = scalar @lines;
for (my $i = 0; $i < $number_lines; $i++) {
print $lines[$i];
}
close CAUGHT;
}
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];
&read_ce;
&choose_vi;
} elsif ($number_of_args == 2) {
system "g++", $ARGV[0], "-o", $ARGV[1];
&read_ce;
&choose_vi;
} else {
print "Too many arguments!\n";
print "Usage: ./cedit somefile.C++\n";
print "Usage (optional): ./cedit somefile.C++
outputfile\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>