The Artima Developer Community
Sponsored Link

Perl Buzz Forum
Template Toolkit's DEFAULT directive does not short-circuit

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Andy Lester

Posts: 518
Nickname: petdance
Registered: Jun, 2009

Andy Lester is an author and programmer.
Template Toolkit's DEFAULT directive does not short-circuit Posted: Sep 15, 2014 10:31 AM
Reply to this message Reply

This post originated from an RSS feed registered with Perl Buzz by Andy Lester.
Original Post: Template Toolkit's DEFAULT directive does not short-circuit
Feed Title: Perlbuzz
Feed URL: http://perlbuzz.com/atom.xml
Feed Description: What's happening in the world of Perl programming, including Perl 5, Perl 6, the CPAN and Parrot?
Latest Perl Buzz Posts
Latest Perl Buzz Posts by Andy Lester
Latest Posts From Perlbuzz

Advertisement

Template Toolkit's DEFAULT does not do short-circuit evaluation like you might think it would.

If you have

[% DEFAULT foo = user.calculate_foo; %]

Then the results of the method call user.calculate_foo are assigned to foo, unless foo already has a true value.

However, whether or not foo already has a true value, user.calculcate_foo is always invoked.

You might expect the code to look like this:

$foo = $user->calculate_foo() unless $foo;

But it's really like this;

$temp = $user->calculate_foo();
$foo = $temp unless $foo;

So, if user.calculate_foo is an expensive function that you don't want to invoke unless you have to, you'll have to resort to an explicit IF block:

IF !foo;
    foo = user.calculate_foo;
END

It's also worth noting that DEFAULT is not for checking if a variable is set, but for checking if it is true.

Read: Template Toolkit's DEFAULT directive does not short-circuit

Topic: Perlbuzz news roundup for 2014-09-15 Previous Topic   Next Topic Topic: Perlbuzz news roundup for 2014-08-11

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use