Permalink
Browse files

Fixed bug (unbugged previous fix) in Template::Plugin::Filter that wa…

…s causing the $self reference in a dynamic filter closure to be garbage collected too soon

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@1244 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent 5a89101 commit af22f8c1d95b1ef691872a808d777196c1aac142 @abw committed Jul 4, 2009
Showing with 33 additions and 8 deletions.
  1. +13 −5 lib/Template/Plugin/Filter.pm
  2. +17 −3 t/plugins.t
  3. +1 −0 t/proc.t
  4. +2 −0 t/test/lib/simple2
@@ -11,7 +11,7 @@
# Andy Wardley <abw@wardley.org>
#
# COPYRIGHT
-# Copyright (C) 2001-2007 Andy Wardley. All Rights Reserved.
+# Copyright (C) 2001-2009 Andy Wardley. All Rights Reserved.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
@@ -26,7 +26,7 @@ use base 'Template::Plugin';
use Scalar::Util 'weaken';
-our $VERSION = 1.37;
+our $VERSION = 1.38;
our $DYNAMIC = 0 unless defined $DYNAMIC;
@@ -63,13 +63,21 @@ sub init {
sub factory {
my $self = shift;
my $this = $self;
- weaken($this);
+
+ # This causes problems: https://rt.cpan.org/Ticket/Display.html?id=46691
+ # If the plugin is loaded twice in different templates (one INCLUDEd into
+ # another) then the filter gets garbage collected when the inner template
+ # ends (at least, I think that's what's happening). So I'm going to take
+ # the "suck it and see" approach, comment it out, and wait for someone to
+ # complain that this module is leaking memory.
+
+ # weaken($this);
if ($self->{ _DYNAMIC }) {
return $self->{ _DYNAMIC_FILTER } ||= [ sub {
my ($context, @args) = @_;
my $config = ref $args[-1] eq 'HASH' ? pop(@args) : { };
-
+
return sub {
$this->filter(shift, \@args, $config);
};
@@ -106,7 +114,7 @@ sub merge_args {
sub install_filter {
my ($self, $name) = @_;
- $self->{ _CONTEXT }->define_filter( $name => $self->factory() );
+ $self->{ _CONTEXT }->define_filter( $name => $self->factory );
return $self;
}
View
@@ -17,7 +17,7 @@
#========================================================================
use strict;
-use lib qw( ./lib ../lib ../blib/arch );
+use lib qw( t/lib ./lib ../lib ../blib/arch );
use Template::Test;
use Template::Plugins;
use Template::Constants qw( :debug );
@@ -30,11 +30,13 @@ my $DEBUG = grep(/^--?d(debug)?$/, @ARGV);
#$Template::Plugins::DEBUG = 0;
my $dir = abs_path( -d 't' ? 't/test/plugin' : 'test/plugin' );
+my $src = abs_path( -d 't' ? 't/test/lib' : 'test/lib' );
unshift(@INC, $dir);
my $tt1 = Template->new({
- PLUGIN_BASE => 'MyPlugs',
- DEBUG => $DEBUG ? DEBUG_PLUGINS : 0,
+ PLUGIN_BASE => ['MyPlugs','Template::Plugin'],
+ INCLUDE_PATH => $src,
+ DEBUG => $DEBUG ? DEBUG_PLUGINS : 0,
}) || die Template->error();
require "MyPlugs/Bar.pm";
@@ -249,4 +251,16 @@ ERROR: Date: plugin not found
-- expect --
/cgi-bin/bar.pl?debug=1
+-- test --
+-- use tt1 --
+-- name Simple plugin filter --
+[% USE Simple -%]
+test 1: [% 'hello' | simple %]
+[% INCLUDE simple2 %]
+test 3: [% 'world' | simple %]
+-- expect --
+test 1: **hello**
+test 2: **badger**
+test 3: **world**
+
View
@@ -41,3 +41,4 @@ This is procfoobar
This is procfoofoo
This is procbarbar
This is procbarbaz
+
View
@@ -0,0 +1,2 @@
+[% USE Simple -%]
+test 2: [% 'badger' | simple -%]

0 comments on commit af22f8c

Please sign in to comment.