Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Create and edit new scripts by johngg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found