#!/usr/bin/perl # use strict; use warnings; # Get modules, set autoflush on STDOUT. # use IO::File; use Term::ReadKey; STDOUT->autoflush(1); # Get name of script we want to create, die if it already # exists. # my $newfile = shift or die "Usage: newscript \n"; die "newscript: $newfile already exists\n" if -e $newfile or -l $newfile; # Make new IO::File handle to create script file with execute # permissions, die on failure. # my $scriptFH = IO::File->new($newfile, O_WRONLY|O_CREAT|O_TRUNC, 0755); die "newscript: open: $!\n" unless $scriptFH; # Set up hash-bang line and strictures for top of script, write # them to the new file and then close. # my $hashBang = "#!/usr/bin/perl\n#\n\nuse strict;\nuse warnings;\n\n"; $scriptFH->print($hashBang); $scriptFH->close(); # Set up editor command, prompts asking user if they want to # edit the new script, and a time-out value of five seconds. # my $editor = "/usr/local/bin/nedit"; our $prompt1 = "Start editing $newfile? ("; our $prompt2 = ") (y/n) : "; our $timeOut = 5; # Set up subroutine reference that will be used as the handler # for $SIG{ALRM}. # our $rcCountdown = sub { # If there is still time left, print prompt (decrementing # time-out value) and set alarm for one second. # if($timeOut) { print "\r", $prompt1, $timeOut --, $prompt2; alarm(1); } # Time is up, restore read mode and die. # else { ReadMode(0); die "Timed out\n" } }; # Install handler, call handler for the first time to prompt # and set alarm. # $SIG{ALRM} = $rcCountdown; $rcCountdown->(); # Set read mode to raw and await a key press. # ReadMode(4); my $resp = ReadKey(0); # If we got here we got a key press, reset read mode, unset # alarm and print a newline to move off prompt. # ReadMode(0); alarm(0); print "\n"; # If key pressed was 'y' then start the editor in the # background via exec, new editor window will appear and # shell will prompt for next command. # if($resp =~ /^y$/i) { exec "$editor $newfile &" or die "Couldn't invoke $editor: $!\n"; } # User doesn't want to edit so just exit. # exit;