Search code examples
perltemplatesgetopt

Do you have a good Perl template script?


I do a lot of programming in Perl and was wondering if people had a "default" template Perl script that they use and willing to share.

I started copying one of my older scripts which has Getopt functions. I am thinking people would have done something similar?


Solution

  • As people say before I have my methods templates in a module: use PMG::PMGBase; and for the initial script escafolding, as an emacs user, I have my perl-insert-start and perl-add-getoption templates, but writing things like:

    (defun perl-insert-start ()
      "Places #!..perl at the start of the script"
      (interactive)
      (goto-char (point-min))
      (insert "#!/usr/bin/env perl\n\n")
      (insert "=head1 [progam_name]\n\n")
      (insert " description:\n\n")
      (insert "=cut\n\n")
      (insert "use feature ':5.10';\n")
      (insert "use strict;\n")
      (insert "#use warnings;\n")
      (insert "#use Data::Dumper;\n")
    )
    

    is a bit tiresome. So at the end is easier for me to have a Perl template script (see below), and call it with run-command-on-region: C-u M-| :~/scripts/perl-start-template.pl from Emacs after selecting one space in a blank buffer:

    #!/usr/bin/env perl
    
    =head1 [progam_name]
    
     description:
    
    =cut
    
    use feature ':5.10';
    use strict;
    use Getopt::Long;
    
    my $prog = $0;
    my $usage = <<EOQ;
    Usage for $0:
    
      >$prog [-test -help -verbose]
    
    EOQ
    
    my $help;
    my $test;
    my $debug;
    my $verbose =1;
    
    
    my $ok = GetOptions(
                        'test'      => \$test,
                        'debug:i'   => \$debug,
                        'verbose:i' => \$verbose,
                        'help'      => \$help,
                       );
    
    if ($help || !$ok ) {
        print $usage;
        exit;
    }
    
    
    print template();
    
    
    sub template {
        ##
        ### Here start the template code
        ##
        return <<'EOT';
    #!/usr/bin/env perl
    
    =head1 [progam_name]
    
     description: This script prints a template for new perl scripts
    
    =cut
    
    use feature ':5.10';
    use strict;
    #use warnings;
    #use Data::Dumper;
    use Getopt::Long;
    # use Template;
    # use PMG::PMGBase;  
    # use File::Temp qw/ tempfile tempdir /;
    # use File::Slurp;
    # use File::Copy;
    # use File::Path;
    # use File::Spec;
    # use File::Basename qw(basename dirname);
    # use List::Util qw(reduce max min);
    # use List::MoreUtils qw(uniq indexes each_arrayref natatime);
    
    # my $PMGbase = PMG::PMGBase->new();
    my $prog = $0;
    my $usage = <<EOQ;
    Usage for $0:
    
      >$prog [-test -help -verbose]
    
    EOQ
    
    my $date = get_date();
    
    my $help;
    my $test;
    my $debug;
    my $verbose =1;
    
    my $bsub;
    my $log;
    my $stdout;
    my $stdin;
    my $run;
    my $dry_run;
    
    my $ok = GetOptions(
                        'test'      => \$test,
                        'debug:i'   => \$debug,
                        'verbose:i' => \$verbose,
                        'help'      => \$help,
                        'log'       => \$log,
                        'bsub'      => \$bsub,
                        'stdout'    => \$stdout,
                        'stdin'     => \$stdin,
    
                        'run'       => \$run,
                        'dry_run'   => \$dry_run,
    
                       );
    
    if ($help || !$ok ) {
        print $usage;
        exit;
    }
    
    sub get_date {
    
        my ($day, $mon, $year) = (localtime)[3..5] ;
    
        return my $date= sprintf "%04d-%02d-%02d", $year+1900, $mon+1, $day;
    }
    
    sub parse_csv_args {
    
        my $csv_str =shift;
        return [split ',', $csv_str];
    }
    
    EOT
    
    
    }