Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks alot, I changed it.

I also added the possibility to add additional libraries when you choose a C program.

#!/usr/bin/perl use strict; use warnings; use File::Basename; ############################ # # Name : newscript # Usage: Makes ready to use script templates for # Bash and Perl only at the moment. It includes # the shebang line for both. Perl templates # includes some useful pragmas and the option to # include the required modules on the command line. # ############################ # declare some required vars my ($name, $language); my $fullname = $0; my $progname = basename($fullname); ## main program: print "Name of the new script : "; chomp ($name = <STDIN>); print "Language of $name script: "; chomp ($language = <STDIN>); # If laguage is Bash, make a Bash script if ($language =~ /bash/i) { print "\nMaking a Bash script: $name\n"; _makebash(); # If language is Perl, make a Perl script } elsif ($language =~ /perl/i) { print "\nMaking a Perl script: $name\n"; print "\nAdd modules? ex: File::Basename;\n(use strict and use war +ning are turned on by default).\n"; print "[yes/no]: "; # check if user wants modules chomp (my $addmodule = <STDIN>); if ($addmodule =~ /yes/i) { my @modules; print "\nThis script does NOT add a ';' for you!\nSay 'done' w +hen you're done..\nModules: "; while (<STDIN>) { last if ($_ =~ /done(;)?/i); push @modules, $_; } _makeperl(@modules); } elsif ($addmodule =~ /no/i) { _makeperl();} else { print "I assume no.\n"; _makeperl(); } # If language is C, make a C program } elsif ($language =~ /c/i) { print "\nMaking a C program: $name\n"; # Check if user wants to add more libraries print "Add other libraries? ex: <stdlib.h>\nYou may ommit the '<', +'>'\n[yes/no]: "; chomp (my $addlib = <STDIN>); if ($addlib =~ /yes/i) { my @libs; print "Say 'done' when you are finished.\nLibraries: "; while (<>) { last if ($_ =~ /done/i); push @libs, $_; } _makec(@libs); } elsif ($addlib =~ /no/i) { _makec(); } else { print "Fine. ONLY <stdio.h> for you !\n"; _makec(); } } else { print "This might help you:\n"; _usage(); } # Make a bash script sub _makebash { if ($language eq 'bash') { $name = "$name.test"; open (NEWSCRIPT, '>', $name); print NEWSCRIPT "#!/bin/bash\n\n"; close NEWSCRIPT; chmod 0700, "$name"; exec ("emacs -nw +3 $name"); } } # Make a perl script sub _makeperl { $name = "$name.pl.test"; my @mods = @_; open (NEWSCRIPT, '>', $name); print NEWSCRIPT "#!/usr/bin/perl\n\nuse warnings;\nuse strict;\n"; if (@mods){ # If module is defined, add it to the temp +late. for my $mod (@mods) { print NEWSCRIPT "use $mod"; } } print NEWSCRIPT "\n"; close NEWSCRIPT; print "\n"; chmod 0700, "$name"; exec ("emacs -nw +50 $name"); } # Make a C program sub _makec { $name = "$name.test.c"; my @library = @_; open (NEWPROG, '>', $name); print NEWPROG "#include <stdio.h>\n\n"; if (@library) { for my $lib (@library){ chomp $lib; if ($lib =~ /^<.*>$/) { print NEWPROG "#include $lib\n"; } else { print NEWPROG "#include <$lib>\n"; } } } print NEWPROG "\n"; close NEWPROG; print "\n"; chmod 0700, "$name"; exec ("emacs -nw +10 $name"); } # Sets the usage message. sub _usage { print<<EOF; Usage: $progname [no options yet] Creates ready to use script templates. The script will first ask you for the name of your program, then the language in which you want it written. If your chosen language is supported, it will make an empty script, with your name and 'test' appended to it. The script then makes an exec call to emacs -nw with your new file. (not very portable yet ..) note: If your chosen language is Perl, The script will ask you if you wish to import more modules. If you do want more input them then followed by a ';' and input 'done' when finish. bash: #/bin/bash perl: #/usr/bin/perl use warnings; use strict; use [yourmods]; C: #include <stdio.h> EOF }

I didn't have a chance to try it on OSX yet. I'll try to make it more portable.

I appreciate all your suggestions and ideas, thanks alot.


In reply to Re^2: RFC: newscript.pl , my very first script! by Darfoune
in thread RFC: newscript.pl , my very first script! by Darfoune

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 having an uproarious good time at the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found