Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
Gordon Avatar asked Sep 24 '10 19:09

Gordon


2 Answers

In my .vimrc file I have

au BufNewFile *.pl s-^-#!/usr/bin/perl\r\ruse strict;\ruse warnings;\r\r-

which writes

#!/usr/bin/perl

use strict;
use warnings;

to any new Perl script. I also have

au BufNewFile *.pm s-^-package XXX;\r\ruse strict;\ruse warnings;\r\r1;-

for modules, but I tend to use Module::Starter for those anyway.

like image 77
Chas. Owens Avatar answered Oct 06 '22 01:10

Chas. Owens


When I need a basic template for many similar scripts, I just turn the similar parts into a module. The script then reduces to something like:

 use App::Foo;

 App::Foo->run( @ARGV );

The App::Foo would inherit from the template module and override whatever was different:

 package App::Foo;
 use parent qw(App::Template);

 ...

In the App::Template module, you put in whatever you need:

 package App::Template;

 sub run {
    my( $class, @args ) = @_;

    my $self = $class->new( ... );
    $self->init;
    $self->process_command_line( ... );

    ...
    }


 sub process_command_line { ... }

 ...

There are some frameworks on CPAN for this sort of thing, but I think it's just as easy to do it yourself and get exactly what you need without dealing with the parts you don't need.

like image 41
brian d foy Avatar answered Oct 06 '22 00:10

brian d foy