[Templates] Array ref returned from plugin can't be unshifted?
Jim Spath
jspath@pangeamedia.com
Thu, 28 Jun 2007 13:21:08 -0400
The value of first is not an array reference. It is a code reference,
which means unshift won't work on it.
Bill Moseley wrote:
> Hey, can anyone repeat this or explain what obvious mistake I made?
>
> Thanks,
>
> On Tue, Jun 26, 2007 at 11:51:07AM -0700, Bill Moseley wrote:
>> 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;