Using File Templates in Perl (FileTmplPatch)
Very Very usefull function for using the templates in perl.
This is like fill in the blanks in file with the values from hash.
Accepts a filepath of template file and referance to a hash having the keys simillar to string s used in file for filling the values.
sub fileTmplPatch {
my ($file,$hash_ref) = @_;
open FILE,"$file" || print "Failed to open tmpl file";
local $/;
my ($str,$val);
$str =;
if ( $str !~ /^\s*$/ ) {
my @keys = keys %{$hash_ref};
foreach $key ( keys %{$hash_ref}) {
$val = $hash_ref->{$key};
$str =~ s/#__($key)__#/$val/smg;
}
}
$str =~ s/#__(.*?)__#//gs;
undef $hash_ref; undef $val;
return $str;
}
This is like fill in the blanks in file with the values from hash.
Accepts a filepath of template file and referance to a hash having the keys simillar to string s used in file for filling the values.
sub fileTmplPatch {
my ($file,$hash_ref) = @_;
open FILE,"$file" || print "Failed to open tmpl file";
local $/;
my ($str,$val);
$str =
if ( $str !~ /^\s*$/ ) {
my @keys = keys %{$hash_ref};
foreach $key ( keys %{$hash_ref}) {
$val = $hash_ref->{$key};
$str =~ s/#__($key)__#/$val/smg;
}
}
$str =~ s/#__(.*?)__#//gs;
undef $hash_ref; undef $val;
return $str;
}
0 Comments:
Post a Comment
<< Home