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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With