Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Subroutine Question

by GrandFather (Saint)
on Jun 27, 2012 at 23:56 UTC ( [id://978799]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Subroutine Question
in thread Subroutine Question

There are two elements to my reply. One is to provide examples of a few techniques you may not have encountered yet (like using HEREDOCs in the print statements) and using if as a statement modifier (last if ...;).

The second element is to think about appropriate places to use a subroutine. Subs are generally used either to wrap up common code so it is only implemented once, or to wrap up some complex code so that it is easier to manage and understand. In either case using a sub makes code much easier to test because the sub can be tested in isolation.

Part of the Perl philosophy is TIMTOWTDI (there is more than one way to do it). However some ways are better than others and, especially while you are learning, it is good to see a variety of alternative solutions. In this case the original sample duplicated code by having two essentially identical subs, but didn't take advantage of a sub to wrap up the complicated task of getting and validating data from the user. Figuring out good places to use subs is quite an art and requires a fair degree of experience. Don't be afraid to change your mind about where and how to use subs in your code. Very often you can make code much easier to understand and maintain through refactoring (moving the code around) where subs play a very large part in how code is managed.

True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2024-04-18 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found