http://qs321.pair.com?node_id=265850

Missing Words has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

     Through a serious of events that I am sure no one here at the monestary wants to here, a program that I use often got deleted. This actually comes as a blessing in disguise since the code was fairly messy(It was my first perl program that actually did anything of use for me). I was laying out a basic template for the structure of the program and stumbled upon a question. When is it appropriate separate code into packages and subroutines? Should you only put something into a subroutine if it is code that is used multiple times?

Readmore to see the template I have created and more detailed description of my question.


Here is the template for the program I am creating:
#!perl #RunLogger2 v0.001 use warnings; use diagnostics; use strict; use PassMasker; #####User Interface##### sub login{} sub login_parser{} sub menu{} sub menu_parser{} ###Data Checking### sub check_date{} ###DataBase Interface### { package Runner; sub new{} sub ran{} sub edit_run{} sub edit_runner{} sub print_log{} }

     PassMasker is a module I created which contains a fairly simple subroutine which allows a password to be given by the user, but only stars(or any choosen mask) will show up on the screen. (See this node if you are interested in it)

     Now to the questions. I felt it was a good idea to separate the code dealing with the user interface from the code dealing with the database. Futhermore, I have given the interface to the a separate package. I did this becuase I think it will improve maintainability,since I would be able to seperate problems caused by my communication with the database I am using(probably a plain DBM file) from problems caused by my communcation with the user. However I am very new to perl and programming, and am not sure if this is the best way to proceed. Am I simply adding more complication becuase of the comminication between the different subroutines and if some of these subroutines are only used once is this just a waste of space? To give an example:

The subroutine Runner::new will probably only be used once during the program. Would it be better to simply put that code directly into Main::menu_parser, which will determine which subroutine to call based on the user input?

As I am typing this, I am realizing the exact meaning of what I am asking may be ambiguous without code to show what would be going on in each sub. Here is an attempt to pose a more general question:

Is there merit in sub-dividing code into packages/subroutines even if it is not syntactically needed?



I feel that if I am ever going to learn to program well I need to start making clean, managable code. Any suggestion on this topic would be very helpful, so I can at least be headed in the right direction before I start the more major coding.

Thanks in Advance.