Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Beginner's question about elegent foreach

by bellaire (Hermit)
on Feb 04, 2011 at 14:21 UTC ( #886212=note: print w/replies, xml ) Need Help??

in reply to Beginner's question about elegent foreach

For what you've got now, it's just fine. There are a couple of small changes you could make. In the first loop, many common classes that have a param method will let you do this to reduce duplication:
$template->param($textField, $self->param($textField) || $userData->{$textField} || ''); $finalData{$textField} = $template->param($textField);
You could also move the third loop next to the first, since they are doing similar things, if the order isn't important. Another technique you could use, since you're curious, is a dispatch table. If you end up having a lot of kinds of these data handlers, or if they get quite large, this can be advantageous. Although it's probably overkill for only 3 different cases, here's what that would look like:
my %fieldHandler = ( textField => sub { my $field = shift; $template->param($field, $self->param($field) || $userData->{$ +field} || ''); $finalData->{$field} = $template->param($field); }, checkBox => sub { my $field = shift; if ($self->param($field) eq '1' || $userData->{$field} eq '1') + { $template->param($field,'checked'); $finalData{$field} = 1; } }, fixField => sub { my $field = shift; if ($which eq 'INTERNAL') { $template->param($field, $self->param($field) || $userData +->{$field} || '') } }, ); my %fieldMap = ( fullname => 'textField', username => 'textField', authname => 'textField', email => 'textField', chk1 => 'checkBox', chk2 => 'checkBox', chk3 => 'checkBox', emp_id => 'fixField', emp_name => 'fixField', emp_cat => 'fixField', ); foreach my $fieldName (keys %fieldMap) { my $handler = $fieldMap{$fieldName}; $fieldHandler{$handler}->($field); }
If the order in which you process the fields is important, you'd have to specify it separately:
my @fieldNames = qw(fullname username authname email chk1 chk2 chk3 em +p_id emp_name emp_cat); foreach my $fieldName (@fieldNames) ...

This has the advantage of putting all of your field names close together with their handler mappings. It also gives you the option of moving off very complex handlers into named subroutines instead of anonymous ones, but if you do that you'll have to pass along references to the data you're using. (This example only works because the anonymous subroutines are acting as closures, and have access to variables in the same lexical scope in which they were defined.)

As stated, for only three types of field this is probably not necessary, but there more you have and the more complex your data handling becomes, the more spread out and difficult to follow your existing structure will become. Eventually all of those foreach loops start to look the same, making it harder to maintain.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://886212]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2019-10-14 00:44 GMT
Find Nodes?
    Voting Booth?