#!/usr/bin/env perl use strict; use warnings; use Template (); my $t = Template->new( {} ); my $tmpl_source = <<"EOT"; This is my template. [% timestamp %] [%- IF foo.defined %] foo is defined and is "[% foo %]" [%- ELSE %] foo is undef [%- END %] [%- IF bar.defined %] bar is defined and is "[% bar %]" [%- ELSE %] bar is undef [%- END %] [%- baz = undef -%] [%- IF baz.defined %] baz is defined and is "[% baz %]" [%- ELSE %] baz is undef [%- END %] Done. EOT $t->process( \$tmpl_source, { foo => undef, bar => q{}, timestamp => scalar localtime() } ) or die $t->error, qq{\n}; exit 0; __END__ $ perl tt_test.plx This is my template. Tue Aug 20 11:07:30 2019 foo is undef bar is defined and is "" baz is defined and is "" Done.