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

Suggested templates for Perl

by tirwin (Initiate)
on Apr 28, 2008 at 04:57 UTC ( [id://683236]=perlquestion: print w/replies, xml ) Need Help??

tirwin has asked for the wisdom of the Perl Monks concerning the following question:

O Ye Gatherers of Knowledge -

I have a simple enough question, although the replies may not be so. :)

I have been searching for 'best practice' recommendations on what should be included in a minimum template for new perl scripts and cannot find anything on the subject (most of my searches using the keywords 'perl' and 'template' take me to links on template processing using perl or HTML::Template).

I would like to create a template for my favorite editor so that I can start new projects easily with all of the boring-yet-useful bits already filled out. For example: path to the perl interpreter, date created, author, program name, description, changelog, license, etc.

What bits of information do best practices dictate should be included? Anyone have a template they're willing to share?

Thanks in advance.

Replies are listed 'Best First'.
Re: Suggested templates for Perl
by GrandFather (Saint) on Apr 28, 2008 at 05:08 UTC
    use strict; use warnings;

    'nuff said. :-D


    Perl is environmentally friendly - it saves trees
Re: Suggested templates for Perl (old)
by tye (Sage) on Apr 28, 2008 at 05:48 UTC

    See (tye)Re: Stupid question. Glancing at it, it still is pretty much the template I use after all of these years. Some justifications are still collecting on my scratchpad I have moved into that node.

    - tye        

      Nice. Very nice! I'll steal that...
Re: Suggested templates for Perl
by andreas1234567 (Vicar) on Apr 28, 2008 at 05:42 UTC
    I often start out like this:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Getopt::Long; use Pod::Usage; use Log::Log4perl; use Log::Dispatch::FileRotate; my $conf = undef; my $log = undef; my $help = undef; my $test = undef; my $file = undef; $conf = q( log4perl.category.Script = INFO, ScreenAppender, ScriptFile +RotateAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.stderr = 1 log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %F +:%L - %m%n log4perl.appender.ScreenAppender.Threshold = DEBUG log4perl.appender.ScriptFileRotateAppender = Log::Dispatch +::FileRotate log4perl.appender.ScriptFileRotateAppender.filename = script.log log4perl.appender.ScriptFileRotateAppender.mode = append log4perl.appender.ScriptFileRotateAppender.size = 100000 log4perl.appender.ScriptFileRotateAppender.max = 5 log4perl.appender.ScriptFileRotateAppender.layout = PatternLayout log4perl.appender.ScriptFileRotateAppender.layout.ConversionPattern= +[%p] %d %F:%L:- %m%n ); Log::Log4perl::init( \$conf ); $log = Log::Log4perl::->get_logger(q(Script)); $log->debug("start") if $log->is_debug(); # GetOptions GetOptions("help|?" => \$help, "test" => \$test, "file=s" => \$file) or pod2usage(2); pod2usage(1) if $help; # ------ main ------ $log->debug("end") if $log->is_debug(); exit(0); __END__ =pod =head1 NAME =head1 SYNOPSIS =head1 OPTIONS =head1 DESCRIPTION =head1 TODO =head1 SEE ALSO =head1 AUTHOR =cut
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Suggested templates for Perl
by stiller (Friar) on Apr 28, 2008 at 07:35 UTC
    "boilerplate" might be a better search term for this.

    You should check out Perl Best Practices, it's an increadibly good book. Boilerplates on pp 133-139.

Re: Suggested templates for Perl
by oko1 (Deacon) on Apr 28, 2008 at 16:02 UTC

    I have a very flexible three-part system that turns Vim (my favorite editor) into the perfect Perl IDE for me. Part 1 is a shell script that creates the file, adds a shebang, a dated comment with the author's name, and a 'use strict;' -

    #!/bin/bash # Created by Ben Okopnik on Mon Apr 28 11:41:01 EDT 2008 # Enter your name here; otherwise, your login name will be used name="" [ -z "$1" ] && { printf "Usage: $0 <file_to_create>\n"; exit; } [ -f "$1" ] && { printf "File already exists!\n"; exit; } printf "#!`which perl` -w\n" > $1 printf "# Created by ${name:-$USER} on `date`\nuse strict;\n\n\n" >> $ +1 chmod +x $1 vi + $1

    The next two parts are Vim keymaps which live in my "~/.vimrc":

    map <silent> <F2> :set filetype=perl<CR>:set kp=perldoc\ -f<CR>:0<CR>: +-r!which perl<CR>I#!<ESC>$a -w<ESC>o# Created by Ben Okopnik on^[:r!d +ate<CR>-J^<CR>iuse strict;<CR><CR> map <F5> :w<CR>:![ -x "%:p" ]\|\|chmod +x "%:p"<CR>:!"%:p"

    The 'F2' key inserts the same content as the script would plus sets up a few useful things in Vim - e.g., hitting the 'K' key now runs 'perldoc -f ' instead of 'man ' on the word under the cursor. Perl syntax highlighting is also enabled. The 'F5' key saves the file, makes it executable if it's not already, then presents you with the command line dialog where hitting the 'Enter' key will execute the current file. You can also enter any arguments your script needs at that time. When it's done, you're returned to your Vi session.

    I realize that this is more than just a template, but for me, that bit is pretty much inextricably tied up with all the rest of it. I'm too Lazy to use just templates. :)

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
Re: Suggested templates for Perl
by Your Mother (Archbishop) on Apr 28, 2008 at 17:26 UTC

    Mine is simple and includes a handful of use modules which I often don't. To 'Control+cut' 100 lines of stuff is all but instant so just jam in anything you tend to use and take out what didn't end up being necessary once you have your script pretty well drafted.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found