Alister West

home is where your code is ...

Simple script boilerplate.

Getopt::Long and Pod::Usage are encouraged to work together.

my-script.pl

#!/usr/bin/env perl

use strict;
use warnings;

use English -no_match_vars;

$OUTPUT_AUTOFLUSH=1; # $|++;


=head1 NAME

    my-script.pl

=head1 SYNOPSIS

    my-script.pl [OPTIONS] [file1, ...]

    OPTIONS: 
        --help,-h
        --config,-c
        --debug,-d
        --dryrun
        --limit,-l
        --noautoupdate

=cut

use Data::Dump   qw/dump/;
use Getopt::Long qw/:config auto_help/;
use Pod::Usage;
use Lazy::Lockfile;

my $lockfile = Lazy::Lockfile->new( $pid_file ) || die "Couldn't get lock";

#
# Get the options.
#
# defaults
my %opts = ( config => 'my.config', autoupdate => 1 );
GetOptions( \%opts,
    'config|c=s'  # string
    'debug|d+',   # incremental -d -d -d (debug=3)
    'dryrun',     # flag
    'limit|l=i',  # integer
    'autoupdate!',# boolean --(no)autoupdate
) or pod2usage(2);


# alternative globals - don't use with files called help :P
our $DEBUG  = grep { /^(?:--debug|--dryrun|--readonly|-v|-d)/i } @ARGV;
our $DRYRUN = grep { /--dryrun|--readonly/i } @ARGV;
pod2usage(2) if grep { /help/i } @ARGV;

$DEBUG=1 if -t STDOUT; # default to verbose if running from cmdline

# strip out command options.
@ARGV = grep { !/-/ } @ARGV;

#
# Setup some output handling.
#

# Takes array or scalar and prints it.
sub debug (@) { chomp(my @msg = @_); $opts{debug} and say "[debug " . (time - $^T) . "s] ". $_ for @msg; }
# debug \%opts;

sub debug { dump @_ }

# Alternative output handling
sub logger { my $msg = shift;
    say "[".DateTime->now( time_zone => 'America/Vancouver') ."] $msg";
    return $msg;
}
sub debug  { my $msg = shift; return logger $msg  if -t STDOUT; }


# ...
By Alister West