Should I make a new level of subroutines to have less repeating code? (In other words, my subroutines would have subroutines.)
Sure, don't hesitate. Whether in Perl or in other languages, I regularly have four or five levels of subroutine calls (and sometimes even much more).

The main reason for making subs is obviously to remove code duplication. If you find yourself copying and pasting a dozen or more lines of code, then those lines should probably go into a sub. Often even for less than a dozen lines.

Sometimes, subroutines are good even if they are called only once, because they hide the gory details from the main stream of thought. One of the litmus tests to figure out if it makes sense to make a sub is whether it is easy to give them a name that makes that sense. For example, I am often writing programs that need to compare data from different files. I might write something like this (untested code):

my $old_cust_data_href = load_old_cust_data($old_cust_data_file); my $new_cust_data_href = load_new_cust_data($new_cust_data_file); my $result_hash_ref = data_compare($old_cust_data_href, $new_cust_data +_href); # ...
Each sub is used only once, but it removes the details about opening the file, reading the data, perhaps splitting it, loading data into the hash and perhaps closing the filehandler. But, of course, I am happier if I can use the same procedure for the two data loads:
my $old_cust_data_href = load_cust_data($old_cust_data_file); my $new_cust_data_href = load_cust_data($new_cust_data_file); my $result_hash_ref = data_compare($old_cust_data_href, $new_cust_data +_href); # ...
Or possibly even better:
my $cust_data_href{$_} = load_cust_data($cust_data_file{$_}) for qw /o +ld new/; my $result_hash_ref = data_compare($cust_data_href{old}, $cust_data_hr +ef{new});
Although there might be some exceptions, a good subroutine is often one that qualifies as a "pure function", i.e. a subroutine that, given the call parameters, return the same results (no sideboard effect, no global variable).

Then, of course, there is the danger of getting too far. I remember having worked on a huge mass of C++ code where just about every single method had no more than two lines of code, so that you ended up with a messy spaghetti of method calls. Going to that extreme makes thing very difficult to maintain, what I sometimes call write-only code.

Update: modified the last code sample which was not fully consistent.

Je suis Charlie.

In reply to Re: efficient use of subroutines by Laurent_R
in thread efficient use of subroutines by jeri_rl

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.