TemplateRex
view release on metacpan or search on metacpan
lib/TemplateRex.pm view on Meta::CPAN
$default_hsh{'func_prefix'} = '_';
$default_hsh{'func_package'} = "";
$default_hsh{'func_flg'} = 0;
# Class methods
sub set_defaults
{
my $class = shift @_;
my %set_hsh;
if ( ref($_[0]) eq "HASH" ) { %set_hsh = %{ $_[0] } }
else { %set_hsh = @_ }
# Merge with existing hash
%default_hsh = ( %default_hsh, %set_hsh);
return %default_hsh;
}
sub get_defaults
{
return %default_hsh;
}
}
#-----------------------------------------------------
# Object modify the inc lst
#-----------------------------------------------------
sub inc_lst
{
TemplateRex::set_defaults( {'inc_dir_lst'=>$_} );
}
# --------------------------
# Heavy lifting constructor
sub new
{
my $caller = shift @_;
# In case someone wants to sub-class
my $caller_is_obj = ref($caller);
my $class = $caller_is_obj || $caller;
# Passing reference or hash
my %arg_hsh;
if ( ref($_[0]) eq "HASH" ) { %arg_hsh = %{ shift @_ } }
else { %arg_hsh = @_ }
# verify input
unless ( ( $arg_hsh{'str'} ) || ( $arg_hsh{'file'} ) ) { _debug("Must specify a file OR str") }
# Override default hash with arguments
#my %conf_hsh = TemplateRex->get_defaults();
my %conf_hsh = __PACKAGE__->get_defaults();
%conf_hsh = (%conf_hsh, %arg_hsh);
# Set package for embedded function to caller unless otherwise specified.
unless ( $conf_hsh{'func_package'} ) { $conf_hsh{'func_package'} = (caller)[0] }
# The object data structure
my $self = bless {
'temp' => {},
'temp_tree' => {},
'func_tree' => {},
'data' => {},
'conf' => { %conf_hsh },
}, $class;
# Read in a template based on a search list
my $str = $arg_hsh{'str'} || $self->read_template( $arg_hsh{'file'} );
# Preload to get the ball rolling . . .
$self->{'temp'}->{'main'} = $str;
my @temp_lst = ('main');
foreach my $key ( @temp_lst )
{
$str = $self->{'temp'}->{"$key"};
my ($temp_hsh_ref, $func_hsh_ref) = $self->process_template( \$str, $key );
my @i_temp_lst = keys %{$temp_hsh_ref};
@{ $self->{temp} }{@i_temp_lst} = @{ $temp_hsh_ref }{@i_temp_lst}; # slices of hashes
@i_temp_lst = grep { $_ ne $key } @i_temp_lst;
$self->{'func_tree'}->{"$key"} = { %{$func_hsh_ref} };
$self->{'temp_tree'}->{"$key"} = [ @i_temp_lst ];
push @temp_lst, @i_temp_lst; # Twiddle with the looping list
}
# Whew that was hard work . . .
return $self;
}
#--------------------------------------------
# $str = $self->read_template($template_name)
#--------------------------------------------
# Finds a template based on a
# search list and returns a string.
#---------------------------------------------
sub read_template
{
my $self = shift @_;
my $template_file = shift @_;
# Dereference
my @inc_dir_lst = @{ $self->{'conf'}->{'inc_dir_lst'} };
my $sub_dir = $self->{'conf'}->{'sub_dir'};
# Tack on some a default
unless ( @inc_dir_lst) { @inc_dir_lst = ( "." ) };
my @sub_dirs;
if($sub_dir){
foreach (@inc_dir_lst){
push(@sub_dirs,"$_/$sub_dir");
( run in 3.666 seconds using v1.01-cache-2.11-cpan-2aafcb1aa8b )