CPAN

Template Toolkit’s DEFAULT directive does not short-circuit

September 15, 2014 CPAN No comments

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.

Perlbuzz news roundup for 2012-10-15

October 15, 2012 Community, CPAN, Perl 5, Perl 6, Rakudo No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-10-09

October 9, 2012 Code craft, Community, Conferences, CPAN, Perl 5 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-10-01

October 1, 2012 Code craft, CPAN, Perl 5, Perl 6, Rakudo No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-09-24

September 24, 2012 Conferences, CPAN, Perl 5, Perl 6 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-09-17

September 17, 2012 Community, CPAN, Perl 5 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-09-10

September 10, 2012 Code craft, CPAN, Perl 5, Perl 6 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-09-04

September 4, 2012 Conferences, CPAN, Perl 5, Perl 6 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-08-27

August 27, 2012 Community, CPAN, Perl 5, Perl 6 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.

Perlbuzz news roundup for 2012-08-20

August 20, 2012 CPAN, Perl 5 No comments

These links are collected from the
Perlbuzz Twitter feed.
If you have suggestions for news bits, please mail me at
andy@perlbuzz.com.