sub ContextString { my $context = shift(); if($context eq "foo") { return "String 1"; } elsif($context eq "bar") { return "String 2"; } } my $t_vars = { context_sub => \&ContextString, other => "mmm donuts", }; # config opts are whatever you need them to be my $template = Template->new($config_opts); $template->process("template_file.tt",$t_vars) || die $template->error();