Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VMethod 'defined' useless with STRICT = 1 #75

Open
LouisStrous opened this issue Feb 22, 2018 · 2 comments
Open

VMethod 'defined' useless with STRICT = 1 #75

LouisStrous opened this issue Feb 22, 2018 · 2 comments
Labels

Comments

@LouisStrous
Copy link

Calling the 'defined' VMethod on an undefined variable raises the "undefined variable" error if template runtime option STRICT is on, so that VMethod is not useful then. It would be more useful if the raising of the "undefined variable" error were suppressed when the 'defined' VMethod is being processed.

Reproduction code:

use warnings;
use strict;

use Template;

my $text = '[% IF foo.defined; "Yes!"; ELSE; "No."; END %]';
my $tt = Template->new({ STRICT => 1 });
$tt->process(\$text) or die $tt->error();

This yields

var.undef error - undefined variable: foo.defined

with Template Toolkit version 2.27. I expected to get No. instead.

A workaround is to use the DEFAULT directive to assign a value to each variable that has none.

@toddr toddr added the Bug label Oct 5, 2018
@dboehmer
Copy link

dboehmer commented Jan 18, 2020

I just tried STRICT mode and I think it's pretty useless this way:-(

I'd love to have this design:

  • printing a variable that doesn't exist→exception
  • printing a variable that is undef→print empty string, issue a warning (updated)
  • checking a variable that's undef with IF or WHILE→condition is false
  • checking a variable that doesn't exist→exception
  • .defined on a variable that doesn't exist→exception
  • .defined on a variable that's undef→false
  • .exists on a variable that doesn't exist→false

Is this possible as a fix/setting/plugin?

@Smylers
Copy link
Contributor

Smylers commented Dec 31, 2021

I'd love to have this design:

That'd be wonderful. We've had to avoid turning on STRICT because of no way to safely check for a NULL value being returned from a database field, which now means we aren't catching typos in variable names (or even in directives: [% ESLE %] was a fun one).

It would have to have a different name to STRICT, but that isn't a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants