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.


In reply to When Is Dividing Code Into Different Subroutines/Packages Important? by Missing Words

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.