in reply to Re^2: Undef value in Template Toolkit
in thread Undef value in Template Toolkit

It is undef. However you're displaying it is stringifying it most likely.

Update: Oops, I stand corrected. Assigning undef in a template does look like it gets an empty string. Interesting. Amended code sample setting baz directly in the template.

Another update: Did some poking and I'm most of the way to convincing myself to agreeing that this might be a bug. It's prossibly worth at least pinging the mailing list with a sample script like this and seeing what the maintainers say.

Update the third: ++ and agreement with those below that relying on results from logic in the display template for anything other than displaying things just raises the hackles.

#!/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.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: Undef value in Template Toolkit
by mikelieman (Friar) on Aug 20, 2019 at 19:03 UTC
    It's intentional, if now regretted.

    From Template-Toolkit/source/lib/Template/Stash.pm

    #--------------------------------------------------------------------- +--- # undefined($ident, $args) # # Method called when a get() returns an undefined value. Can be redef +ined # in a subclass to implement alternate handling. #--------------------------------------------------------------------- +--- sub undefined { my ($self, $ident, $args) = @_; if ($self->{ _STRICT }) { # Sorry, but we can't provide a sensible source file and line +without # re-designing the whole architecture of TT (see TT3) die Template::Exception->new( $UNDEF_TYPE, sprintf( $UNDEF_INFO, $self->_reconstruct_ident($ident) ) ) if $self->{ _STRICT }; } else { # There was a time when I thought this was a good idea. But it +'s not. return ''; } }