How should I structure a series of relatively short functions that each require a handful of parameters, without having to pass all the parameters around? Was I better off before refactoring? Is OO the way to go here?

It would take knowing more specifics to address these, so here is just some basic observation. If there is no requirement that keeps you from using a plain-old run-of-the-mill table to structure your data and your code, then that will suffice (90%+ of the time). Some of the most over-engineered code out there comes from people doing very exotic and fancy things when all they really needed was a simple table with some operations on rows. Tables are simple and everyone can understand it just by looking at it. Moreover, nearly any concievable data structure can be expressed with them, thats why relational databases have been around for so long. (note: this is not a suggestion to use a database, just a comment on structure).

Of course, not everything is amenable to this approach: nested and irregular data structures, a registry, other caveats of course apply. The point is, if all you have is a standardized series of name-value pairs that can be arranged into rows, why not keep it as simple as possible.

### begin_: init use strict; use warnings; use Data::Dumper; my $oDataTable = [ { 'type' => 'type1', 'text2' => 'text-a2', 'regex' => '[a-z\\d]?', 'url' => 'http://c', 'text1' => 'text-a1', 'dir' => 'a' }, { 'type' => 'type2', 'text2' => 'text-b2', 'regex' => '[a-z\\d]{0,2}', 'url' => 'http://b', 'text1' => 'text-b1', 'dir' => 'b' }, { 'type' => 'type3', 'text2' => 'text-c2', 'regex' => '[a-z\\d]{0,3}', 'url' => 'http://c', 'text1' => 'text-c1', 'dir' => 'c' }, ]; ### begin_: process the data MungeMyData($oDataTable); print Data::Dumper->Dump([$oDataTable], [qw(dataroot)]); ### begin_: the master subroutine sub MungeMyData { my $oData = shift || die"missing data"; for my $row (@{$oData}){ if ($row->{type} eq 'type1' ){ $row->{dir} = uc($row->{dir}); $row->{text1} = reverse($row->{text1}); ### munge munge munge ... }; if ($row->{type} eq 'type2' ){ $row->{dir} = lc($row->{dir}); $row->{text1} = substr($row->{text1},3); ### munge munge munge ... }; ### munge munge munge ... if ($row->{type} eq 'type9' ){ $row->{dir} = lc($row->{dir}); $row->{text1} = substr($row->{text1},3); ### munge munge munge ... }; }; @{$oData} = sort { $a->{type} cmp $b->{type}} @{$oData}; return $oData; }###end_sub ### begin_: the end 1; __END__

In reply to Re: Structuring code by dimar
in thread Structuring code by bobf

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.