App-Templer
view release on metacpan or search on metacpan
lib/Templer/Global.pm view on Meta::CPAN
use strict;
use warnings;
package Templer::Global;
=head2 new
Constructor.
Any parameters specified in our single hash-argument are saved away,
the filename specified in the 'file' parameter will be opened and parsed.
=cut
sub new
{
my ( $proto, %supplied ) = (@_);
my $class = ref($proto) || $proto;
my $self = {};
#
# Allow user supplied values to override our defaults
#
foreach my $key ( keys %supplied )
{
$self->{ lc $key } = $supplied{ $key };
}
bless( $self, $class );
$self->_readGlobalCFG( $self->{ 'file' } ) if ( $self->{ 'file' } );
return $self;
}
=head2 _readGlobalCFG
Read the specified configuration file. Called by the constructor if
a filename was specified.
=cut
sub _readGlobalCFG
{
my ( $self, $filename ) = (@_);
#
# If the configuration file doesn't exist that's a shame.
#
return if ( !-e $filename );
#
# Open the file, making sure we're UTF-8 safe.
#
open( my $handle, "<:utf8", $filename ) or
die "Failed to read '$filename' - $!";
binmode( $handle, ":utf8" );
while ( my $line = <$handle> )
{
# strip trailing newline.
$line =~ s/[\r\n]*//g;
# skip comments.
next if ( $line =~ /^#/ );
# If the line is : key = value
if ( $line =~ /^([^=]+)=(.*)$/ )
{
my $key = $1;
my $val = $2;
$key = lc($key);
$key =~ s/^\s+|\s+$//g;
$val =~ s/^\s+|\s+$//g;
#
# command expansion?
#
if ( $val =~ /(.*)`([^`]+)`(.*)/ )
{
# store
my $pre = $1;
my $cmd = $2;
my $post = $3;
# get output
my $output = `$cmd`;
chomp($output);
# build up replacement.
$val = $pre . $output . $post;
}
#
# If the line is pre/post-build then save the values as an array
#
if ( $key =~ /^(pre|post)-build$/ )
{
push( @{ $self->{ $key } }, $val );
}
else
{
#
# The general case is store the value in the key.
#
$self->{ $key } = $val;
}
print "Templer::Global set: $key => $val\n"
if ( $self->{ 'debug' } );
}
}
close($handle);
}
( run in 0.864 second using v1.01-cache-2.11-cpan-140bd7fdf52 )