The basic point is that it is poor programming style (and ineffecient to boot) to have two subroutines that essentially do the exact same thing.

From my standpoint as someone who has learnt to code that painful way (is there any other), I've developped the standard methodology that if I have duplicated code (or code that closely duplicates functionalities I've used elsewhere) then I need to go back and look at condensing the duplicates into a single subroutine.

In this case, then, you might start by changing the subroutine as follows:

sub file_processing { my ($array_ref, $script, $arg, $file) = @_; open(FILE, $script . ' ' . $arg . ' ' . $file . ' |') or die ("can +'t do it: $!\n"); while (<FILE>) { chomp; next if /^\#/; next if /none/i; next if /unkno/i; push @{$array_ref}, split ( /\n/, $_); } close FILE; }

Notice, however, that there are some areas that could use some more work:

  1. The regexp that matches none/unknown/# could be improved and probably condensed into a single one looking something like this: /(?:#|unknown|none)/
  2. The assignment back to $array_ref may not do exactly what you expect/need -- a little testing would be in order

And finally, you'd call this script by doing the following:

&file_processing(\@array1, $script1, $arg1, $file1); &file_processing(\@array1, $script2, $arg2, $file2);

Which, of course, using the heuristic of duplicated code = BAD, could be condensed into a single array of arrays that are looped over and passed in in turn.

Does that help?


In reply to RE: Re: Subroutine question by jreades
in thread Subroutine question by Limo

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.