Text-TemplateFill
view release on metacpan or search on metacpan
TemplateFill.pm view on Meta::CPAN
# 2 Value - array
sub ArithVarSet {
my ($self, $name, @val) = @_;
my $varref = &Value($globalself, $globaltag, $name, 1);
${$varref} = $val[$#val];
return @val;
}
# Create a new template object.
# Initialise default options.
sub new {
my $class = shift;
my $PageNo = 0; # Not started yet. The first to print is page 1
my $PageLineNo = 0;
my $Now = time;
# Program variables
my %ProgVars = (
);
# Calculated & auto vars
my %CalcVars = (
'PageNo' => \$PageNo, # Current page number
'PageLineNo' => \$PageLineNo, # Current line number in page (0 if page not started)
'Now' => \$Now, # Current time - actually time of 'new'.
);
my $CalcHandle = new Math::Expression;
$CalcHandle->SetOpt( 'VarGetFun' => \&ArithVarGet,
'VarSetFun' => \&ArithVarSet,
'VarIsDefFun' => \&ArithVarIsDef,
'PrintErrFunc' => \&ArithError);
# References to all the paragraphs for this template
# This contains references to hashes keyed on the paragraph tag.
my %Paragraphs = (
);
my %template = (
'Errors' => 0, # Error count
'ErrorFunction' => \&PrintError, # What to call on error
'Initialised' => 0, # True when post-read file initialisation done
'Locale' => '', # May be something like 'fr_CA.ISO8859-1'
'OldLocale' => '', # On entry to GeneratePara
'BaseDir' => '.', # What to add if no '/' at start of file name
'LineTerminator'=> "\n", # What to put at the end of each line
'EndPageSeq' => '', # Probably \f, else use many empty lines
'PageLen' => 66, # Length of output page
'Variables' => \%ProgVars, # Hash of all program variables
'CalcVars' => \%CalcVars, # Hash of all calculated variables
'Paragraphs' => \%Paragraphs, # Hash of all paragraphs
'StartPageTag' => ' ', # Which tag to use to auto start a page, undef doesn't work - space is dodge
'EndPageTag' => ' ', # Which tag to use to auto end a page
'CalcHandle' => $CalcHandle, # For calculations
);
return bless \%template => $class;
}
# Set an option in the %template.
sub SetOpt {
my $self = shift @_;
while($#_ > 0) {
&Error($self, "Unknown option '$_[0]'") unless(defined($self->{$_[0]}));
&Error($self, "No value to option '$_[0]'") unless(defined($_[1]));
$self->{$_[0]} = $_[1];
shift;shift;
}
}
# Read a file in. The user arguments are:
# * a tag name that is used to identify/obtain this paragraph in the future
# * a file name that will be read, if this is not given, use tag.
# Return true on error.
sub ReadPara {
my ($self, $tag, $fname) = @_;
$globalself = $self;
$self->{Initialised} = 0; # Need to reassess what is what
$fname = $tag unless(defined($fname)); # no $fname ?
my @Lines = ();
my ($ParaOnPage, $ParaTotal) = (0, 0);
my @Calc = ();
my @CalcStr = ();
my @LineNoMap = ();
my %ParaDescript = (
'Lines' => \@Lines,
'LineNoMap' => \@LineNoMap, # For error messages - else removed comments cause bad reporting of errors
'Calc' => \@Calc, # Calculations - parsed trees
'CalcStr' => \@CalcStr, # Calculations - uncompiled
'EndPage' => 0, # True if paragaph ends a page
'StartPage' => 0, # True if paragaph starts a page
'BlanksAfter' => 0, # True blanks to page botton when EndPage come after paragraph
'ParaOnPage' => \$ParaOnPage, # Paragraph usage count this page
'ParaTotal' => \$ParaTotal, # Paragraph usage count total
);
# Get the file to open, prepend the base dir if not absolute:
my $fn = (($fname =~ /^\//) ? '' : $self->{BaseDir}) . '/' . $fname;
unless(open(TMPL, "<$fn")) {
&Error($self, "Cannot open '%s' as: $!", $fn);
return(1);
}
while(<TMPL>) {
chop; # Basic line tidy:
s/\s*\r?$//;
next if(/^\$\{#\}/);
# If it is a calculation, extract it & save
if(/^\$\{Calc\s+(.*)\}$/) {
( run in 0.691 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )