[Chicago-talk] Use vi in perl
Randal L. Schwartz
merlyn at stonehenge.com
Tue May 24 14:01:33 PDT 2011
>>>>> "tiger" == tiger peng <tigerpeng2001 at yahoo.com> writes:
tiger> I am trying to use vi as a UI to edit/check dynamic created perl code segment.
tiger> When user save and quite the vi, the script will check its syntax and force to
tiger> open the file again if there are any compiling error. I'd like to have the error
tiger> message show in the new opened vi interface but don't know how to do it. Is here
tiger> any suggestion?
tiger> Thanks,
tiger> Below is the segment invoke vi and do the check?
tiger> my $msg;
tiger> do {
tiger> if (defined $msg) {
tiger> warn "$msg";
tiger> my $dummy = <STDIN>;
tiger> }
tiger> system("/bin/vi", $tmpfile) == 0 or die $!;
tiger> $msg = `/usr/bin/perl -c $tmpfile`;
tiger> } while ($? !=
tiger> 0);_______________________________________________
Maybe something like (untested, but I usually get this right):
LOOP: {
system "vi", $tempfile and die "vi exited badly";
my $pid = open ERRS, "-|";
die "can't fork: $!" unless defined $pid;
unless ($pid) { # child does:
open STDERR, ">&STDOUT"; # merge stderr to stdout
exec "perl", "-c", $tempfile;
}
my $errs;
$errs .= $_ while <ERRS>;
close ERRS;
if ($errs) { # we saw something wrong
local *ARGV; # prepare for in-place edit
@ARGV = ($tmpfile);
while (<>) {
print;
## insert error messages at end of data
print "\n\n=for COMPILER_ERRORS\n\n$errs\n\n=cut\n\n" if eof;
}
redo LOOP;
}
} # done
The first person to say "No, you should have used a lexical filehandle
there" will get a free copy of my "Perl Second Best Practices" slide
deck, subtitled "Why Damian's guidelines are just that... guidelines".
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
More information about the Chicago-talk
mailing list