[Dbix-class] DBIx::Class::HTMLWidget (checkboxes)

A. Pagaltzis pagaltzis at gmx.de
Fri Jul 14 18:30:00 CEST 2006


* Dennis Schön <ds at 1d10t.de> [2006-07-14 11:25]:
> I narrowed the problem down to the following code in
> HTMLWidget.pm line 43:
> 
> my %cb = map {$_->name => undef if 
> $_->isa('HTML::Widget::Element::Checkbox')} @{ $result->{_elements} };

Yes, which is because in a block that ends on an `if` statement,
if the conditional is false, then it becomes the value of the
block.

    $ perl -e'print "[$_]\n" for map { ( "foo" => 1 ) if !!$_ } qw( 1 0 0 0 1 0 0 1 )'
    [foo]
    [1]
    []
    []
    []
    [foo]
    [1]
    []
    []
    [foo]
    [1]

See all those false values? They screw up the hash.

Don’t use `if` (and for that matter, `for` and `while`) as the
last statement in a block that’s supposed to have a value.

Compare:

    $ perl -e'print "[$_]\n" for map { !!$_ ? ( "foo" => 1 ) : () } qw( 1 0 0 0 1 0 0 1 )'
    [foo]
    [1]
    [foo]
    [1]
    [foo]
    [1]

So the right way to write that code “fancily” is thus:

    my %cb = map {
        $_->isa( 'HTML::Widget::Element::Checkbox' )
            ? ( $_->name => undef )
            : ()
    } @{ $result->{_elements} };

Regards,
-- 
#Aristotle
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;



More information about the Dbix-class mailing list