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

Darfoune:

Overall, it looks fine. Here are some suggestions, though:

First, you have a _usage() function that'll give help on the program. You may want to use POD for your documentation, and have your usage message be brief and refer the user to run perldoc newscript.pl to see full documentation.

The second thing I'd suggest is to use 'heredocs' for your script templates. That way, they're easier to edit correctly and get the way you want them to be, something like:

print NEWSCRIPT <<EOPerlTpl; #!/usr/bin/perl # $name # use warnings; use strict; EOPerlTpl

My final suggestion is to have a couple tags you can use to add clusters of frequently-used modules. As an example, I was often asked to generate reports from our database, and they normally would want the report in a spreadsheet. So I'd code it so that if it recognized "RPT" as a module, that I'd automatically add:

use DBI; use Spreadsheet::WriteExcel; use Spreadsheet::WriteExcel::Styler; my ($DBName, $DBUID, $DBPWD) = ("MyDatabase", "reportAccount", "tehRpt +P@55werd"); . . . add a few lines here to define my favorite excel styles . . .

...roboticus

When your only tool is a hammer, all problems look like your thumb.

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

    Hi roboticus

    I will need to look into POD documentation and how to make one but it's a good idea. As with the heredoc style for the templates, how would I be able to add user chosen modules to the heredoc ? Correct me if I'm mistaken but heredocs are a fixed lenght. This is the thought that led me to use print statement at first. Thanks alot for you feedback !!!

      Darfoune:

      While a heredoc is fixed length (barring variable substitution), you can still do multiple writes to the file. So the heredoc could have the basic header for your script. Then as the user selects other modules, you can continue to add them to the script.

      open my $FH, '>', 'afile.pl'; print $FH <<EOHDR; Some header stuff EOHDR while (<>) { if (/^RPT/) { print $FH, "another line of your script" } elsif (/^FOO/) { print $FH, "something completely different" } elsif (/^Q/) { last; } }

      Alternatively, you can use a heredoc to load a variable, then continue to add data to the variable:

      my $SCRIPT = <<EOHDR Some header stuff EOHDR; $SCRIPT .= "More script stuff";

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        I can see now how more easy it would be with a heredoc having bigger headers ..!

        About the tags for cluster of frequently used modules, I will look for a way so that the user would input it's prefered module and cluster them. I use so very few modules myself right now that I wouldn't know which ones to group together. I will post it as soon as I have a working version