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.


In reply to Re: Beginner's question about elegent foreach by bellaire
in thread Beginner's question about elegent foreach by tart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.