Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Undef value in Template Toolkit

by DreamT (Pilgrim)
on Aug 20, 2019 at 14:13 UTC ( #11104730=perlquestion: print w/replies, xml ) Need Help??

DreamT has asked for the wisdom of the Perl Monks concerning the following question:

I want to set a value to undef in a TT template:
[% hashref.value1 = undef %]
, but this only produces an empty string.
$VAR1 = { ... 'value1' => '' ... }

Is there a way around this? From what I've read it seems to be a "feature" in Template Toolkit that undefs are treated as empty strings.

Replies are listed 'Best First'.
Re: Undef value in Template Toolkit
by Fletch (Chancellor) on Aug 20, 2019 at 14:32 UTC

    What exactly "way around this" do you want? Perl itself treats undef in a string context as an empty string. What do you expect TT to do instead?

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

      The goal is to set the mentioned variable to undef, rather than an empty string.

        This sounds like an XY problem(?). I would argue that manipulating data within templates is generally a mistake. Caring about what form unviewable data is within a view also seems like a mismatch to the context.

        This seems to be some intermediate or side effect goal to me. TT's output is a string, so for that purpose undef and the empty string are equivalent. Maybe you have something like [% IF hashref.defined('value1') %]...[% END %] later on, or you want to pass a value back to the Perl code which called TT's process? For these situations you can [% hashref.delete('value1') %] which should do the trick unless you also need to distinguish between keys which exist from keys with an undefined value.

        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104730]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2020-10-25 23:29 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (249 votes). Check out past polls.