Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: RFC: newscript.pl , my very first script!

by 1nickt (Canon)
on Jul 24, 2015 at 20:59 UTC ( [id://1136230]=note: print w/replies, xml ) Need Help??


in reply to RFC: newscript.pl , my very first script!

UPDATE: this was an error in the OP code, not a Mac thing

Doesn't work for me on Mac OSX. It creates the file properly (determined in post-mortem) but hangs when trying to open the file with emacs.

[13:52][nick:~/monks]$ perl 1136223.pl Name of the new script : foo Language of foo script: perl Making a Perl script: foo.pl.test Add modules? ex: File::Basename; (use strict and use warning are turned on by default). [yes/no]: yes This script does NOT add a ';' for you! Say 'done' when you done.. Modules: Data::Dumper; done <-- hangs here <-- then after killing process: ----:---F1 *scratch* All L1 (Fundamental)------------------- +-------------------------------------------------------------- ": No such file or directory at 1136223.pl line 104, <STDIN> line 5. [13:54][nick:~/monks]$
The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: RFC: newscript.pl , my very first script!
by Darfoune (Novice) on Jul 25, 2015 at 14:16 UTC

    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.

Re^2: RFC: newscript.pl , my very first script!
by Darfoune (Novice) on Jul 24, 2015 at 21:36 UTC

    Hello sir

    I would really like to implement that feature but I'm not sure how to proceed in order to see why emacs is failing to it's job. Do I have to redirect the error output from the shell to stdout or is it some perl trick I can't think of right now? I have a few days off so I'll soon take time to run newscript.pl on my father's mac osx and see if I could figure how to make it work. Thanks for trying it !!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found