<br clear="all">On Wed, Oct 5, 2011 at 5:42 PM, Toby Corkindale <span dir="ltr"><<a href="mailto:toby.corkindale@strategicdata.com.au">toby.corkindale@strategicdata.com.au</a>></span> wrote:<br><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On 05/10/11 17:34, Alfie John wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey Toby,<br>
<br>
I think there are two problems here:<br>
<br>
   - Andrew was right. Put a + in front of the block<br>
   - can($_) in the second block is being called on the qw{} and not on<br>
an object<br>
</blockquote>
<br></div>
In case people want working test code, here is some below.<br>
By the way, I was trying to put the + on line 11 below, to no success, but perhaps you mean a different {?<br>
(I'm picking the, umm, third that occurs in the whole program, or second within the map..)<br>
<br>
<br>
#!/usr/bin/env perl<br>
use 5.14.1;<br>
use warnings;<br>
use Data::Dumper;<br>
<br>
my @fields = map { Foo->new($_) } qw(bing bang bong);<br>
<br>
my $foo = [<div class="im"><br>
    map {<br>
       my $f = $_;<br>
       {<br>
           map {<br>
               $f->can($_) ? ($_ => $f->$_) : ()<br>
           } qw(label name html);<br>
       }<br>
   } @fields<br></div>
];<br>
<br>
say Dumper($foo);<br>
<br>
package Foo;<br>
<br>
sub label { shift->{label} }<br>
<br>
sub new { bless { label => $_[1] } }<br>
<br>
1;<div><div></div><div class="h5"><br></div></div></blockquote><div><br></div><div>On 5.10.1 that didn't work for me. I tried it with adding a local variable for the hashref inside the first map, which seemed to do what I think you're trying to do</div>

<div><br></div><div><div>my $array_ref = [map {</div><div>    my $f = $_;</div><div>    my $inner = {</div><div>        map {</div><div>             $f->can($_) ? ( $_ => $f->$_ ) : ();</div><div>        } qw/label name html/</div>

<div>    };</div><div>    $inner;</div><div>} @fields];</div></div><div> </div><div>I would be careful putting subroutine calls inside maps that are being used for hashes though, because the returns from $f->label, $f->name, $f->html can only ever be a single value. If someone decides to change what gets returned, you could end up with "odd number of elements" errors. I do it pretty commonly, but only if I'm confident the testing around it is decent.</div>

<div><br></div><div><br></div></div>