Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Welcome to the Monastery
 
PerlMonks  

Re^2: Subroutine Question

by Socrates440 (Acolyte)
on Jun 27, 2012 at 21:57 UTC ( #978777=note: print w/ replies, xml ) Need Help??


in reply to Re: Subroutine Question
in thread Subroutine Question

I wrote it with a subroutine because I am in the chapter regarding subroutines in the textbook that I am walking myself through. I know that this program could have been accomplished in other ways.


Comment on Re^2: Subroutine Question
Re^3: Subroutine Question
by GrandFather (Cardinal) on Jun 27, 2012 at 23:56 UTC

    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
Node Status?
node history
Node Type: note [id://978777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2013-05-24 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (495 votes), past polls