Another way is to refactor them into four separate classes, based on your $dcr_type variable. A fifth class, would be used as a sort of factory and would decide which subclass to return based on the value of the $dcr_type. You would still have the if...then logic, but it is hidden by a separate layer of abstraction. It would go something like this (this is just an example, so the code below is very skimpy)

package factory; #We don't need any logic in this constructor, so... sub new { my $class = shift; bless {}, $class; } sub getInstance { my ($class, $dcr_type, @row) = ( $_[0], uc $_[1], @{$_[2]} ); if ($dcr_type eq "CONTACT"){ return contact->new(@row); } elsif($dcr_type eq "LITERATURE"){ return literature->new(@row); } elsif($dcr_type eq "FAQ"){ return faq->new(@row); } elsif($dcr_type eq "ORGANIZATION"){ return organization->new(@row); } } package contact; sub new {...} sub doTheDeed{ return ('HQ' , undef); } package literature; sub new {...} # @_row is this classes local version of the @row parameter sub doTheDeed{ return (undef, $_row[4]) } package faq; sub new {...} # @_row is this classes local version of the @row parameter sub doTheDeed{ return (undef, $_row[2] eq 'Service' ? $_row[3] : $_row[4]); } package organization; sub new {...} # @_row is this classes local version of the @row parameter sub doTheDeed{ my @ret = ($_row[2], undef); $ret[0] = 'HQ' if ($_row[0] eq 'Headquarters'); return @ret; }

Now your main body code will look like:

my $fact = factory->new(); my $instance = $fact->getInstance($dcr_type, @row); return $instance->doTheDeed();

In reply to Re: Rethinking Your Program's If-Thens by Steve_p
in thread Rethinking Your Program's If-Thens by princepawn

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.