#!/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 = ); print "Language of $name script: "; chomp ($language = ); # 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 warning are turned on by default).\n"; print "[yes/no]: "; # check if user wants modules chomp (my $addmodule = ); if ($addmodule =~ /yes/i) { my @modules; print "\nThis script does NOT add a ';' for you!\nSay 'done' when you're done..\nModules: "; while () { 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: \nYou may ommit the '<','>'\n[yes/no]: "; chomp (my $addlib = ); 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 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 template. 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 \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 }