To save myself some typing I have written a small script to make the creation of new Perl scripts simpler. I called it newscript (it does what it says on the tin) and you run it with an argument of the script name you want to create. It checks that the file does not already exist then creates a new file with execute permissions and places
#!/usr/bin/perl # use strict; use warnings;
at the top so I have no excuse for not using strict and warnings. It then asks the user if they would like to start editing the new file; the script sets up a timeout for the user to answer so that it doesn't sit there waiting forever. The editor I prefer to use is nedit but you could substitute your favourite in $editor. If your preference is something like vi then you could start an xterm with vi as it's command to run. This is rather *nix-centric but that's mostly what I use. Here's the script. I hope that it is of interest.
#!/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 <filename>\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;
Cheers,
JohnGG
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Create and edit new scripts
by graff (Chancellor) on Apr 25, 2006 at 01:11 UTC | |
by johngg (Canon) on Apr 25, 2006 at 09:03 UTC | |
by wazoox (Prior) on May 06, 2006 at 14:50 UTC | |
Re: Create and edit new scripts
by teamster_jr (Curate) on May 05, 2006 at 10:11 UTC | |
Re: Create and edit new scripts
by blazar (Canon) on Apr 26, 2006 at 10:17 UTC | |
by johngg (Canon) on May 06, 2006 at 14:25 UTC | |
by blazar (Canon) on May 06, 2006 at 20:21 UTC | |
Re: Create and edit new scripts
by jasonk (Parson) on Apr 25, 2006 at 04:26 UTC | |
by johngg (Canon) on Apr 25, 2006 at 15:36 UTC | |
Re: Create and edit new scripts
by arkturuz (Curate) on Apr 25, 2006 at 07:55 UTC | |
by wazoox (Prior) on Apr 25, 2006 at 19:07 UTC | |
by arkturuz (Curate) on Apr 26, 2006 at 07:47 UTC | |
by johngg (Canon) on Apr 25, 2006 at 15:20 UTC | |
by arkturuz (Curate) on Apr 26, 2006 at 07:42 UTC | |
Re: Create and edit new scripts
by zentara (Archbishop) on Apr 25, 2006 at 11:49 UTC | |
by johngg (Canon) on Apr 25, 2006 at 15:32 UTC |
Back to
Cool Uses for Perl