Skip Menu |
 

This queue is for tickets about the Text-Template CPAN distribution.

Report information
The Basics
Id: 55696
Status: resolved
Priority: 0/
Queue: Text-Template

People
Owner: Nobody in particular
Requestors: cjm [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 1.48



Subject: [PATCH] add a STRICT option
Download (untitled) / with headers
text/plain 839b
I've started using Dist::Zilla. It uses Text::Template. I wanted to turn on strict in my templates for the same reason I do it in my scripts: to help catch typos. The problem is that PREPEND => 'use strict;' is not adequate, because it doesn't let you use any variables that come from the HASH parameter. I think what is needed is a STRICT => 1 option. This would be roughly equivalent to PREPEND => 'use strict; use vars (All_Vars_Defined_in_HASH);' but it would come before any PREPEND string, so you could use both STRICT and PREPEND in the same template. If you don't have a HASH, then it just prepends 'use strict;'. You could also have an always_strict class method, but that's not essential. The attached patch adds the STRICT option as described above. I've also included the patch with Dist-Zilla-PluginBundle-CJM-0.03.
Subject: Template_strict.patch
This patch for Text::Template 1.45 adds support for the STRICT option. This automatically prepends 'use strict' to your template code, but still allows you to use variables defined by the HASH option. --- lib/Text/Template.pm 2008-04-16 18:38:08.000000000 -0500 +++ lib/Text/Template.pm 2010-03-12 12:21:31.416024029 -0600 @@ -255,6 +255,7 @@ my $fi_eval_package; my $fi_scrub_package = 0; my $fi_filename = _param('filename') || $fi_self->{FILENAME} || 'template'; + my $fi_strict = _param('strict', %fi_a); my $fi_prepend = _param('prepend', %fi_a); unless (defined $fi_prepend) { @@ -272,6 +273,7 @@ $fi_eval_package = caller; } + my @fi_varlist; my $fi_install_package; if (defined $fi_varhash) { if (defined $fi_package) { @@ -281,7 +283,12 @@ } else { $fi_install_package = $fi_eval_package; # The gensymmed one } - _install_hash($fi_varhash => $fi_install_package); + @fi_varlist = _install_hash($fi_varhash => $fi_install_package); + } + + if ($fi_strict) { + $fi_prepend = "use vars qw(@fi_varlist);$fi_prepend" if @fi_varlist; + $fi_prepend = "use strict;$fi_prepend"; } if (defined $fi_package && defined $fi_safe) { @@ -441,6 +448,7 @@ $hashlist = [$hashlist]; } my $hash; + my @varlist; foreach $hash (@$hashlist) { my $name; foreach $name (keys %$hash) { @@ -449,13 +457,23 @@ local *SYM = *{"$ {dest}::$name"}; if (! defined $val) { delete ${"$ {dest}::"}{$name}; + my $match = qr/^.\Q$name\E$/; + @varlist = grep { $_ !~ $match } @varlist; } elsif (ref $val) { *SYM = $val; + push @varlist, do { + if (UNIVERSAL::isa($val, 'ARRAY')) { '@' } + elsif (UNIVERSAL::isa($val, 'HASH')) { '%' } + else { '$' } + } . $name; } else { *SYM = \$val; + push @varlist, '$' . $name; } } } + + @varlist; } sub TTerror { $ERROR }
This has been committed and will be in v1.48 Thanks! -- Regards, Michael Schout


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.