Last active
February 20, 2025 13:16
-
-
Save MagnusEnger/3897873 to your computer and use it in GitHub Desktop.
Perl script template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Copyright 2019 Magnus Enger Libriotech | |
=head1 NAME | |
my_script.pl - Short description. | |
=head1 SYNOPSIS | |
perl my_script.pl -v | |
=cut | |
use Getopt::Long; | |
use Data::Dumper; | |
$Data::Dumper::Sortkeys = 1; | |
use Template; | |
use DateTime; | |
use YAML::Syck; | |
use Pod::Usage; | |
use Term::ANSIColor qw(:constants); | |
use Modern::Perl; | |
binmode STDOUT, ":utf8"; | |
$|=1; # Flush output | |
# Use a Util.pm located in a "lib" subdir of the dir the script is in | |
use FindBin qw( $Bin ); | |
use lib "$Bin/lib"; | |
use Util; | |
say BOLD, BLUE, "This text is in bold blue", RESET; | |
my $dt = DateTime->now; | |
my $date = $dt->ymd; | |
# Get options | |
my ( $configfile, $input_file, $limit, $verbose, $debug ) = get_options(); | |
# Get the config from file and add verbose and debug to it | |
if ( !-e $configfile ) { die "The file $configfile does not exist..."; } | |
my $config = LoadFile( $configfile ); | |
$config->{'verbose'} = $verbose; | |
$config->{'debug'} = $debug; | |
# Log to a file | |
my $log_file = 'myscript.log'; | |
open( LOG, '>>', $log_file ) or die "Could not open file '$log_file' $!"; | |
say LOG "My first report generated by perl"; | |
close LOG; | |
# Check that the file exists | |
if ( !-e $input_file ) { | |
print "The file $input_file does not exist...\n"; | |
exit; | |
} | |
# Configure Template Toolkit | |
my $outfile = 'data.txt'; | |
my $ttconfig = { | |
INCLUDE_PATH => '', | |
ENCODING => 'utf8' # ensure correct encoding | |
}; | |
# create Template object | |
my $tt2 = Template->new( $ttconfig ) || die Template->error(), "\n"; | |
$tt2->process( $template, $vars, $outfile, { binmode => ":utf8" } ) || die $tt2->error(); | |
=head1 OPTIONS | |
=over 4 | |
=item B<-c, --configfile> | |
Path to config file in YAML format. | |
=item B<-i, --infile> | |
Name of input file. | |
=item B<-l, --limit> | |
Only process the n first somethings. | |
=item B<-v --verbose> | |
More verbose output. | |
=item B<-d --debug> | |
Even more verbose output. | |
=item B<-h, -?, --help> | |
Prints this help message and exits. | |
=back | |
=cut | |
sub get_options { | |
# Options | |
my $configfile = ''; | |
my $input_file = ''; | |
my $limit = ''; | |
my $verbose = ''; | |
my $debug = ''; | |
my $help = ''; | |
GetOptions ( | |
'c|configfile=s' => \$configfile, | |
'i|infile=s' => \$input_file, | |
'l|limit=i' => \$limit, | |
'v|verbose' => \$verbose, | |
'd|debug' => \$debug, | |
'h|?|help' => \$help | |
); | |
pod2usage( -exitval => 0 ) if $help; | |
pod2usage( -msg => "\nMissing Argument: -c, --configfile required\n", -exitval => 1 ) if !$configfile; | |
pod2usage( -msg => "\nMissing Argument: -i, --infile required\n", -exitval => 1 ) if !$input_file; | |
return ( $configfile, $input_file, $limit, $verbose, $debug ); | |
} | |
=head1 AUTHOR | |
Magnus Enger, <magnus [at] libriotech.no> | |
=head1 LICENSE | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment