Pregunta

I would expect the following code

my @array;
for my $rapport ( qw( value1 value2 value3 ) ) {
    push @array, { key => $rapport };
}

to produce:

$VAR1 = [
      {
        'key' => 'value1'
      },
      {
        'key' => 'value2'
      },
      {
        'key' => 'value3'
      }
    ];

However, running this code segment under Catalyst MVC I get:

$VAR1 = [
          {
            'key' => [ 'value', 'value2', 'value3' ]
          },
        ];

Can someone please explain to me why?

EDIT: could anyone with the same issue please add an example? I cannot reproduce after some code changes, but as it has been upvoted 5 times I assume some other users have also experienced this issue?

¿Fue útil?

Solución

This code example...

#!/usr/bin/perl

use Data::Dumper;
my @input = ( "var1", "var2", "var3" );
my @array;
for my $rapport ( @input ) {
    push @array, { key => $rapport };
}

print Dumper( \@array );

exit;

produces ...

$VAR1 = [
          {
            'key' => 'var1'
          },
          {
            'key' => 'var2'
          },
          {
            'key' => 'var3'
          }
        ];

But the following...

#!/usr/bin/perl

use Data::Dumper;
my @input = [ "var1", "var2", "var3" ]; # sometimes people forget to dereference their variables
my @array;
for my $rapport ( @input ) {
    push @array, { key => $rapport };
}

print Dumper( \@array );

exit;

shows...

$VAR1 = [
          {
            'key' => [
                       'var1',
                       'var2',
                       'var3'
                     ]
          }
        ];

As you can see both examples loop through an array but the second one is an array, that was initialized with a reference value. Since in Catalyst you normally ship various values through your application via stash or similar constructs, you could check weather your array really contains scalar values : )

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top