Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Text::Template question

My first time using Text::Template...  can anyone see why the vars in
my template are not being filled in?

$ cat foo.tmpl
foo: [@-- $foo --@]

$ cat foo.pl
use warnings;
use strict;
use diagnostics;

use Text::Template;

my $foo = 'FOO_BAR';

my $template = Text::Template->new(
        DELIMITERS => [ '[@--', '--@]' ],
        TYPE => 'FILE',
        SOURCE => 'foo.tmpl'
print $template->fill_in();

$ ./foo.pl

On May 25, 11:42 am, Phil Lawrence <prlawre@gmail.com> wrote:

Your template does not have access to lexical variables (ie, variables
declared with 'my').  This is stated in Text::Template's documentation
at http://search.cpan.org/~mjd/Text-Template-1.44/lib/Text/Template.pm#m...

You have a few options:
1) Declare the variable with 'our' instead of 'my'.  That will make it
a global variable in the current package.  Note the danger of this -
if your template changes the variable, it's changed in your perl code
as well.
2) Use a variable in a different package, and pass that package to
your Text::Template fill_in method.  For example, rather than
my $foo = 'FOOBAR';
you'd have:
and then pass PACKAGE => 'TEMPLATE' as an argument to fill_in
3) Pass a hash ref of values to the fill_in method.  So keep your
my $foo = 'FOOBAR';
and pass HASH => { foo => $foo } as an argument to fill_in

Hope this helps,
Paul Lalli

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc