I really like that first "point". Only thing I'd add is that if the single line of code is complex enough -- maybe it deserves a subroutine of its own.

As many have said, the purpose of "subs" is to make things more clear. But why does adding subs make things more clear? Obviously you can't (normally)just make every line a sub and have that make it clear.

I'd think of subroutines as ways of mentally helping you (and others if they read your code) to break your code up into small, functional chunks. Each subroutine can be a concept. Once you have the subroutine 'built', you should be able to use it anywhere (assuming its well designed) like a "custom addition" to perl for your specific program.

For instance, suppose you want to do something similar to the "index()" function, which can find a single character embedded in a string. However, instead of "single characters", suppose you have "symbolic names of characters" (ex.: "<Up>", "<Down>", "<Left>", "<Right>"). Now you want to find your "character"'s position in the array of characters. Suppose you are trying to find the character named "<Up>" (equivalent to the "up arrow" key that is not on the numeric pad). You need to search for it in an array with the symbolic names from above (<Up>, <Down>...).

Your normal index function looks like:
"index $mykey, $string"
So now you create a function that does the same for your symbolic names, called "my_string_index":

sub my_string_index($\@) { my ($symkey, $symkeys)=@_; my $index=-1; for ($index=0;$index<=$#$symkeys; ++$index) { if ($symkeys->[$index] eq $symkey) { return $index; } } return -1; }

Now you can use your new subroutine to perform the same function for your symbolic keys as the regular index does for single-letter keys:
"my_string_index $key_to_find, @symbolic_keynames;"

You have "added" your own "string_index" function that finds your symbolic-keys in an array of symbolic keynames. Ex:

@cursor_keys = qw(<Up> <Down> <Left> <Right>); $keyname_to_find='<Down>'; my_string_index $keyname_to_find, @cursor_keys;
You know longer have to think about the implementation -- it's just there, in your program. Later, if you don't like the performance of "my_string_index" using the 'for' loop, you can substitute other perl code to speed up the routine. If you've made your function 'stand-alone' (doesn't change anything outside the function), then you won't have to change the code in multiple places. You just have to change your 1 function, "my_string_index", and then everyplace that uses that function will gain in performance.

Another place to use functions -- as "place holders". If I want to write a file update program and I want to focus on the update code, first, I can start with a dummy skeleton:

# skeleton file read & write functions... sub readfile { my ($array_p)=@_; @$array_p=<>;} sub save_file { my ($array_p)=@_; foreach (@$array_p) { print $_; } } # (above dummy routines will be changed later) ### main program ### # first open file readfile(\@cur_file); #will put file into specified array #update code #delete blank comment lines and blank lines @mynewfile=grep ( !/^#?\s*$/, @cur_file); #save results back in file save_file(\@mynewfile); #will save array back to file
Using the above type of skeleton, you can start with your filter simply reading and writing to the terminal for testing. Later you can add "real" code to read and write the file you want (instead of standard input and output), but for the now, you can play & develop just the filter code.

Hope this gives you some ideas....

Linda


In reply to Re^2: when to use subroutine by perl-diddler
in thread when to use subroutine by convenientstore

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.