I've got a plugin that returns an array ref. Not sure what was in my
coffee this morning, but when I then try to unshift another element
onto that array ref nothing happens.
But if I copy the value return from the plugin and then unshift than
then that works fine.
$ cat arrayref.pl
use strict;
use warnings;
use Template;
Template->new->process( \*DATA ) or die;
__END__
In template
[%
USE Dumper;
USE first = Arrayref;
Dumper.dump( first );
first.unshift( [ { foo => 'bar' } ] );
"note no change\n";
Dumper.dump( first );
"no copy and change\n";
x = first;
x.unshift( [ { foo => 'bar' } ] );
first = x;
Dumper.dump( first );
%]
$ perl arrayref.pl
In template
$VAR1 = [ <<<< array ref returned from plugin
[
{
'one' => 1,
'two' => 2
},
{
'three' => 2,
'four' => 4
}
],
[
{
'xone' => 1,
'xtwo' => 2
},
{
'xthree' => 2,
'xfour' => 4
}
]
];
note no change
$VAR1 = [ <<<< first.unshift
[
{
'one' => 1,
'two' => 2
},
{
'three' => 2,
'four' => 4
}
],
[
{
'xone' => 1,
'xtwo' => 2
},
{
'xthree' => 2,
'xfour' => 4
}
]
];
no copy and change <<<< copy and push
$VAR1 = [
[
{
'foo' => 'bar' <<< works!
}
],
[
{
'one' => 1,
'two' => 2
},
{
'three' => 2,
'four' => 4
}
],
[
{
'xone' => 1,
'xtwo' => 2
},
{
'xthree' => 2,
'xfour' => 4
}
]
];
$ cat Template/Plugin/Arrayref.pm
package Template::Plugin::Arrayref;
use strict;
use warnings;
use Data::Dumper;
use base 'Template::Plugin';
sub new {
return sub {
return [
[
{ one => 1, two => 2, },
{ three => 2, four => 4, },
],
[
{ xone => 1, xtwo => 2, },
{ xthree => 2, xfour => 4, },
],
];
}
}
1;