[Catalyst] Avoid using each() in Catalyst

Bernhard Graf catalyst at augensalat.de
Mon May 22 00:44:14 CEST 2006


Hello,

today I encountered a really annoying behaviour (= potential bug) in 
Catalyst:

I was writing a plugin that occasionally modifies 
$c->request->body_parameters and $c->request->query_parameters .

This is done by defining a method prepare_body_parameters() in the 
plugin, expecting prepare_parameters() to run later to merge body and 
query parameters into $c->request->parameters().

But prepare_parameters() fails, because it uses each(). each() uses an 
internal iterator. Previous modifications to the query & body params
might leave iterators of these hashes at a random position, probably
at the end - as a consequence the while ( ... each ...) loops aren't 
executed at all.

Of course the solution is to reset the modified hashes in the plugin by 
using keys() in void context, but I had to study the Catalyst code to 
understand this.

To be bullet proof I'd suggest to replace all occurences of

	while ( my ( $name, $param ) = each ... )

by

	forach $name ( keys ... )

because with keys() hashes are always read from the beginning.

Of course this applies to all methods that might be overloaded by a 
plugin.
-- 
Bernhard Graf



More information about the Catalyst mailing list