What's the most frequent type of bug you write when working in perl? These days, mine seems to be list context bugs. I try to do this:
But that really requires this:ShoppingCart->new( user => $user, map { $_->id => $_->quantity } @products, );
Contrived example, but hopefully you get the gist -- unexpected list context or lack of same is definitely a thorn in my side.ShoppingCart->new( user => $user, ( map { $_->id => $_->quantity } @products ), );
Anyone else have a class of errors that keeps biting them? Has it changed for you over your development as a Perl coder?
UPDATE
This was intended to be about the general question, not the example, but since people are complaining about the example, I'll offer one that works. CGI.pm will return undef or empty list for params with no value, depending on context. That means this code fails:
There are various ways to solve it, like forcing scalar context:use CGI; use Data::Dumper; my $q = CGI->new(foo => 1); print Dumper [ foo => $q->param('foo'), bar => 1, ];
BTW, the thing I was thinking of with map was not context but the way that a function which takes a list can grab more than you meant it to of what follows:foo => scalar $q->param('foo'),
Putting parentheses around the map solves the problem.ShoppingCart->new( user => $user, map { $_->id => $_->quantity } @products, bank_account => 99, );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: your Perl bug Achilles heel
by TGI (Parson) on Dec 05, 2008 at 04:10 UTC | |
Re: your Perl bug Achilles heel
by GrandFather (Saint) on Dec 05, 2008 at 01:06 UTC | |
by perrin (Chancellor) on Dec 05, 2008 at 02:39 UTC | |
Re: your Perl bug Achilles heel
by mirod (Canon) on Dec 05, 2008 at 10:58 UTC | |
by Anonymous Monk on Dec 05, 2008 at 11:10 UTC | |
by mirod (Canon) on Dec 05, 2008 at 14:33 UTC | |
Re: your Perl bug Achilles heel
by wfsp (Abbot) on Dec 05, 2008 at 07:57 UTC | |
Re: your Perl bug Achilles heel
by rhesa (Vicar) on Dec 06, 2008 at 03:06 UTC | |
Re: your Perl bug Achilles heel
by Lawliet (Curate) on Dec 05, 2008 at 02:32 UTC | |
Re: your Perl bug Achilles heel (parens)
by tye (Sage) on Dec 05, 2008 at 03:48 UTC | |
Re: your Perl bug Achilles heel
by Narveson (Chaplain) on Dec 05, 2008 at 23:56 UTC |
Back to
Meditations