Home: Perl Programming Help: Beginner:
Empty Output File generated while usig perl templates



Tejas
User

Jan 9, 2017, 6:11 PM


Views: 1969
Empty Output File generated while usig perl templates

Hi

I ve tried using perl templates and the requirement is to have an output file generated and the output file is empty

I ve tried in 2 different approached



Code
use lib '/Users/tejas/lib/perl/cpanm/lib/perl5'; 
use strict;
use warnings;
use Template;
use Data::Dumper ;
my $template = Template->new ( {
INCLUDE_PATH => '/Users/tejas/MyPerl/SDP/',
OUTPUT_PATH => '/Users/tejas/MyPerl/',
}) or die $Template::ERROR;

create_conf( { appid => '7777777' } );

sub create_conf
{

my ( $ref ) = @_;
print Dumper $ref;
foreach my $file ('Configfile.ini'){
$template->process( $file, $ref, $file ) or die $template->error;
}
return 1;
}


Quote
Configfile.ini is my template file

[%-
A='alphabet'
One='numeric'
appid='myapp'
-%]


Though, it creates an output file , its empty


Here is one more approach


Code
use lib '/Users/tejas/lib/perl/cpanm/lib/perl5'; 
use strict;
use warnings;
use Template;
use Data::Dumper ;
my $template = Template->new or die $Template::ERROR;

my $object =
{
conf =>
{
fp_input => '/Users/tejas/MyPerl/SDP/configfile.ini.template',
fp_output => '/Users/tejas/MyPerl/SDP/configfile.ini',
},
obj =>
{
template => $template,
},
};

create_conf( $object, { appid => '7777777' } );

sub create_conf
{
my ( $object, $ref ) = @_;

my $fp_input = $object->{conf}->{fp_input};
my $fp_output = $object->{conf}->{fp_output};
my $template = $object->{obj}->{template};

open my $fh_input, '<', $fp_input or die "cannot open '$fp_input': $!";
open my $fh_output, '>', $fp_output or die "cannot open '$fp_output': $!";

$template->process( $fh_input, $ref, $fh_output ) or die $template->error;

return 1;
}

Would you please advice what's missing here

Thanks
Tejas


BillKSmith
Veteran

Jan 10, 2017, 8:04 AM


Views: 1963
Re: [Tejas] Empty Output File generated while usig perl templates

Your template file does not look anything like the example in Template::Manual::Intro. I recommend that you implement that example. Make and test many small changes to make it meet your requirements.
Good Luck,
Bill