Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

RE: Re: Subroutine question

by jreades (Friar)
on Sep 18, 2000 at 22:29 UTC ( [id://33004]=note: print w/replies, xml ) Need Help??


in reply to Re: Subroutine question
in thread Subroutine question

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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found