#!/usr/bin/perl -w # # stdinToEditor: Pipe stdin straight to an editor. # # 2007-12-17: Written by Steven J. DeRose, sderose@acm.org. # 2008-01-16 sjd: Clean up temp file. # 2008-09-23 sjd: Option to choose/keep temp file. # 2010-08-10 sjd: perldoc, -k, close ofh. Background the editor. # 2010-09-12 sjd: Cleanup. # # To do: # use strict; use Getopt::Long; my $version = "2010-09-12"; my $keep = 0; my $outfile = ""; my $quiet = 0; my $verbose = 0; # Process options # Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help|?" => sub { system "perldoc stdinToEditor"; exit; }, "keep!" => \$keep, "o=s" => \$outfile, "q|quiet!" => \$quiet, "v|verbose+" => \$verbose, "version" => sub { print "Version of $version by Steven J. DeRose\n"; exit; } ); ($result) || die "Bad options.\n"; ############################################################################### # my $tfile = $outfile; if ($tfile eq "") { $tfile = "/tmp/stdinToEditor_" . int(rand(100000)); ($verbose) && warn "Temp file is at '$tfile'.\n"; } open my $ofh, ">$tfile" || die "Unable to open temp file at '$tfile'.\n"; while (<>) { print $ofh $_; } close $ofh; system "$ENV{EDITOR} $tfile &"; if (!$keep) { system "sleep 3"; system "rm $tfile"; } else { print "stdinToEditor saved input to '$tfile'.\n"; } exit; ############################################################################### # sub showUsage() { warn " =head1 Usage toedit [options] [file] Copies STDIN to a temp file, and launches your \$EDITOR ($ENV{EDITOR}) on it. In other words, lets you pipe straight into your editor. =head1 Options =over =item B<-k> Don't delete the temp file afterwards. =item B<-o file> Use thie file instead of a temp file. =item B<-version> Display version info and exit. =back =head1 Ownership This work by Steven J. DeRose is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. For further information on this license, see http://creativecommons.org/licenses/by-sa/3.0/. The author's present email is sderose at acm.org. For the most recent version, see http://www.derose.net/steve/utilities/. =cut "; }