Skip to content

Instantly share code, notes, and snippets.

@ian-kent
Created July 15, 2013 21:23
Show Gist options
  • Save ian-kent/6003638 to your computer and use it in GitHub Desktop.
Save ian-kent/6003638 to your computer and use it in GitHub Desktop.
Some examples - working, erroring and segfaulting
package obj2;
sub new { my ($package, %args) = @_; bless { %args }, 'obj2' };
sub render { shift->{obj}->() };
package obj;
sub new { bless {}, 'obj' };
sub capture { return obj2->new(obj => pop) }
package main;
use Data::Dumper;
use Text::Xslate;
my $count = 0;
my $macro = undef;
my $xslate = Text::Xslate->new(
path => {
'test.tx' => qq{
: \$obj.capture(-> {
Rendering <: count() :>
: }).render
}
},
cache => 1,
function => {
count => sub {
++$count;
},
capture => sub {
$macro = bless { macro => shift }, 'test';
undef;
},
render => sub {
$macro->{macro}->();
}
}
);
# This fails
print $xslate->render('test.tx', { obj => obj->new } );
print $xslate->render('test.tx', { obj => obj->new } );
print $xslate->render('test.tx', { obj => obj->new } );
39,43c39,42
< # This works
< my $param = { obj => obj->new };
< print $xslate->render('test.tx', $param );
< print $xslate->render('test.tx', $param );
< print $xslate->render('test.tx', $param );
---
> # This fails
> print $xslate->render('test.tx', { obj => obj->new } );
> print $xslate->render('test.tx', { obj => obj->new } );
> print $xslate->render('test.tx', { obj => obj->new } );
package obj2;
sub new { bless { obj => pop }, 'obj2' };
sub render { shift->{obj}->() };
package obj;
sub new { bless {}, 'obj' };
sub capture { return obj2->new(pop) }
package main;
use Data::Dumper;
use Text::Xslate;
my $count = 0;
my $macro = undef;
my $xslate = Text::Xslate->new(
path => {
'test.tx' => qq{
: \$obj.capture(-> {
Rendering <: count() :>
: }).render
}
},
cache => 1,
function => {
count => sub {
++$count;
},
capture => sub {
$macro = bless { macro => shift }, 'test';
undef;
},
render => sub {
$macro->{macro}->();
}
}
);
# This fails
print $xslate->render('test.tx', { obj => obj->new } );
print $xslate->render('test.tx', { obj => obj->new } );
print $xslate->render('test.tx', { obj => obj->new } );
package obj2;
sub new { my ($package, %args) = @_; bless { %args }, 'obj2' };
sub render { shift->{obj}->() };
package obj;
sub new { bless {}, 'obj' };
sub capture { return obj2->new(obj => pop) }
package main;
use Data::Dumper;
use Text::Xslate;
my $count = 0;
my $macro = undef;
my $xslate = Text::Xslate->new(
path => {
'test.tx' => qq{
: \$obj.capture(-> {
Rendering <: count() :>
: }).render
}
},
cache => 1,
function => {
count => sub {
++$count;
},
capture => sub {
$macro = bless { macro => shift }, 'test';
undef;
},
render => sub {
$macro->{macro}->();
}
}
);
# This segfaults
my $param = { obj => obj->new };
print $xslate->render('test.tx', $param );
print $xslate->render('test.tx', $param );
print $xslate->render('test.tx', $param );
package obj2;
sub new { bless { obj => pop }, 'obj2' };
sub render { shift->{obj}->() };
package obj;
sub new { bless {}, 'obj' };
sub capture { return obj2->new(pop) }
package main;
use Data::Dumper;
use Text::Xslate;
my $count = 0;
my $macro = undef;
my $xslate = Text::Xslate->new(
path => {
'test.tx' => qq{
: \$obj.capture(-> {
Rendering <: count() :>
: }).render
}
},
cache => 1,
function => {
count => sub {
++$count;
},
capture => sub {
$macro = bless { macro => shift }, 'test';
undef;
},
render => sub {
$macro->{macro}->();
}
}
);
# This works
my $param = { obj => obj->new };
print $xslate->render('test.tx', $param );
print $xslate->render('test.tx', $param );
print $xslate->render('test.tx', $param );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment