[Melbourne-pm] Nested maps returning hash references

Alfie John alfiejohn at gmail.com
Wed Oct 5 01:24:15 PDT 2011


Sorry... read my email during dinner.

Here is what I did as a test:

  #!/usr/bin/perl;

  use warnings;
  use strict;

  package Foo;

  use base 'Class::Accessor';

  my @fields = qw{ label name html };
  Foo->mk_accessors( @fields );

  sub new {
    my $self = bless {}, $_[0];

    foreach my $field ( @fields ) {
            $self->$field( $field );
    }

    return $self;
  }

  my @foos = map { Foo->new() } 1..3;

  use Data::Dumper; warn Dumper([
          map {
                  my $f = $_;
                  {
                          map {
                                  $f->can($_) ? ( $_ => $f->$_() ) : ()
                          } @fields
                  }
          } @foos
  ]);

It looks essentially the same as what you were running. This should output:

$VAR1 = [
          {
            'html' => 'html',
            'name' => 'name',
            'label' => 'label'
          },
          {
            'html' => 'html',
            'name' => 'name',
            'label' => 'label'
          },
          {
            'html' => 'html',
            'name' => 'name',
            'label' => 'label'
          }
        ];

Take out the + and you get:

$VAR1 = [
          'label',
          'label',
          'name',
          'name',
          'html',
          'html',
          'label',
          'label',
          'name',
          'name',
          'html',
          'html',
          'label',
          'label',
          'name',
          'name',
          'html',
          'html'
        ];

After looking at yours, it looks like the problem was the trailing
semi colon after the qw[} :)

Alfie

On Wed, Oct 5, 2011 at 5:44 PM, Toby Corkindale
<toby.corkindale at strategicdata.com.au> wrote:
>
> On 05/10/11 17:38, Alfie John wrote:
>>
>> Whoops. That qw{} was in my test code.
>>
>> I added the + on the block and it worked for me.
>
> That is weird; I tried prefixing a + symbol to both the left-hand-curly-braces in the maps (one at a time), to no avail!
>
> This is Perl 5.14.1..


More information about the Melbourne-pm mailing list