home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP13.TXT < prev    next >
Text File  |  1987-03-25  |  24KB  |  520 lines

  1.                    Chapter 13 - Modules, Local and Global
  2.  
  3.  
  4.  
  5.                       PREREQUISITES FOR THIS MATERIAL
  6.  
  7.              Before  attempting  to understand  this  material,  you
  8.         should  have a good grasp of the principles taught in Part I
  9.         of  this  tutorial.   None of the material from Part  II  is
  10.         required to do a meaningful study of modules in Modula-2.
  11.  
  12.                            WHAT GOOD ARE MODULES?
  13.  
  14.              Modules are the most important feature of Modula-2 over
  15.         its  predecessor Pascal making it very important for you  to
  16.         understand what they are and how they work.  Fortunately for
  17.         you,  there  are not too many things to learn about them and
  18.         after  you master them you will find many uses for  them  as
  19.         you develop programs, and especially large programs.
  20.  
  21.              Load and display the program named LOCMOD1.MOD for your
  22.         first   example  of  a  program  with  an  embedded  module.
  23.         Modules  are  nothing new to you because every  program  you
  24.         have examined has been a module.   At this time, however, we
  25.         will introduce a local module.
  26.  
  27.                           WHAT IS A LOCAL MODULE?
  28.  
  29.              A local module is simply a module nested within another
  30.         module,  just like the example on your monitor at this time.
  31.         The  module  named  "LocalStuff" is nested within  the  main
  32.         module and is heavily indented for clarity.   Since  nothing
  33.         is  imported into the local module,  nothing that belongs to
  34.         the  main module can be seen from within the nested  module.
  35.         In  addition,  since the procedure "GetNumber" is  the  only
  36.         thing  exported  from  the local  module,  nothing  else  is
  37.         available to the main module.   In effect,  the local module
  38.         is  an  impenetrable  wall through which  nothing  can  pass
  39.         without the benefit of the IMPORT and EXPORT list.   In this
  40.         case,  the  variable "Counter" cannot be modified in any way
  41.         by the main module, either intentionally or accidentally and
  42.         the  procedure  "GetNumber" will very stubbornly  refuse  to
  43.         allow  any flexibility in its output,  adding three  to  its
  44.         internally  stored variable each time it is called.   It may
  45.         seem  to you that this result can be accomplished easily  by
  46.         using  another procedure without the module but we will  see
  47.         shortly that it will not be the same.
  48.  
  49.                         THE BODY OF THE LOCAL MODULE
  50.  
  51.              The  body  of  the  local  module  has  one   statement
  52.         contained within it,  "Counter := 4;", that is executed only
  53.         when  the module is loaded,  and at no other time.   This is
  54.         therefore  an  initialization section for the  module.   Any
  55.  
  56.  
  57.                                  Page 81
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                    Chapter 13 - Modules, Local and Global
  68.  
  69.  
  70.         valid  statements can be put here and they will be  executed
  71.         when  the  program  is loaded,  or you  can  omit  the  body
  72.         altogether   by  omitting  the  BEGIN  and  any  statements.
  73.         Actually,  this  body is no different than the body  of  the
  74.         main  program  since  it too is executed one time  when  the
  75.         program is loaded, except for the fact that the main program
  76.         is required to have a body or you will have no program.
  77.  
  78.                       THE MODULE VERSUS THE PROCEDURE
  79.  
  80.              We  must digress a bit to see the difference  in  these
  81.         two  important  topics  in  Modula-2.   A  procedure  is  an
  82.         executable section of code whereas a module is a grouping of
  83.         variables,  constants,  types,  and procedures.  A module is
  84.         never executed since it is simply a grouping identifier.
  85.  
  86.              The  variables in a procedure do not exist when  it  is
  87.         not  being executed,  but instead are generated  dynamically
  88.         when  the procedure is called.  A variable therefore,  has a
  89.         lifetime associated with it in addition to a  "type".   This
  90.         may  seem  strange  to  you but if you think  about  it  for
  91.         awhile,  it will help explain how recursive procedure  calls
  92.         work.   The  module,  on the other hand,  exists anytime its
  93.         surrounding  code exists,  in this case,  the main  program.
  94.         Since the module always exists,  the variable "Counter" also
  95.         always exists because it is defined as a part of the module.
  96.         If this variable were defined within a procedure,  it  would
  97.         be  automatically regenerated every time the procedure  were
  98.         called  and  would  therefore  not  remember  the  value  it
  99.         contained the prior time the procedure was called.  We could
  100.         choose  to  define  the  variable as  global  and  it  would
  101.         therefore always be available and never regenerated,  but we
  102.         would  be  left  with the possibility  of  anything  in  the
  103.         program modifying it either accidentally or on purpose.   In
  104.         a  program as small as this one,  it would not be a problem,
  105.         but  it is intended to illustrate the solution to a  problem
  106.         embedded in a much larger program.
  107.  
  108.              Suppose,  for  example,  that  you wished  to  generate
  109.         random  numbers  for some use within a program.   You  could
  110.         include  all  of the code within a module using  the  module
  111.         body  for  the  seed  initialization,  and  a  procedure  to
  112.         generate  one random number each time it  was  called.   The
  113.         structure  would be essentially the same as that given here,
  114.         but the actual code would be different.  Nothing in the main
  115.         program  or any of its procedures could in any  way  corrupt
  116.         the job given to the random number generator.
  117.  
  118.                     BACK TO THE PROGRAM ON YOUR MONITOR
  119.  
  120.              In  this  case we have one local module defined  within
  121.  
  122.  
  123.                                  Page 82
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                    Chapter 13 - Modules, Local and Global
  134.  
  135.  
  136.         the main module but as many as desired could be used, and we
  137.         have one procedure in the local module whereas we could have
  138.         as many as desired.   In fact,  we could have local  modules
  139.         embedded in a procedure,  or in other local modules.   There
  140.         is no real limit as to how you can structure your program to
  141.         achieve the desired results.   One thing must be remembered.
  142.         If you embed a local module within a procedure,  all of  its
  143.         variables are defined dynamically each time the procedure in
  144.         which  it  is  embedded  is called,  and its  body  is  also
  145.         executed each time.   This can be used to advantage in  some
  146.         situations,  but it would be best to leave this construct to
  147.         the future when you have more experience with Modula-2.
  148.  
  149.              In  the body of the main module you will  find  nothing
  150.         new   except   for  the  call  to  the  function   procedure
  151.         "GetNumber()"  which is actually nothing new except that  it
  152.         is embedded in the local module "LocalStuff".   Compile  and
  153.         run the program to see if it does what you expect it to do.
  154.  
  155.                              TWO LOCAL MODULES
  156.  
  157.              It would be well to point out at this time that if  you
  158.         define two local modules at the same level, one could EXPORT
  159.         a variable, procedure, constant, or type and the other could
  160.         IMPORT  it and use it in any legal fashion.   You  therefore
  161.         have  the ability to very carefully define the mechanism  by
  162.         which the two modules interact.
  163.  
  164.                             ANOTHER LOCAL MODULE
  165.  
  166.              The  program we have been inspecting had the  procedure
  167.         exported without qualification, so it could only be referred
  168.         to  by  its simple name.   This could have led to  a  naming
  169.         conflict  which can be solved by using a qualified export as
  170.         is done in the next program.   Load and display the  program
  171.         named LOCMOD2.MOD.  This program is very similar to the last
  172.         except for moving the output statements to the procedure.
  173.  
  174.              First,  you  should notice that the procedure  name  is
  175.         exported  using  "EXPORT QUALIFIED" which allows the use  of
  176.         the  qualified  call  to the procedure in  line  number  25.
  177.         There  can  never  be  a conflict  of  names  in  calling  a
  178.         procedure  this  way because it is illegal to use  the  same
  179.         name  for a module more than once at any level.   In a local
  180.         module,  you  have  a choice of using  either  qualified  or
  181.         unqualified export of items, but the exported items must all
  182.         be  of the same export type because only one export list  is
  183.         allowed per module.
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                  Page 83
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                    Chapter 13 - Modules, Local and Global
  200.  
  201.  
  202.                        IMPORTING INTO A LOCAL MODULE
  203.  
  204.              The  three  output  procedures are used  in  the  local
  205.         module  "MyStuff",  but  because it is only  permissible  to
  206.         import  items  from a module's immediate  surroundings,  the
  207.         procedures must first be imported into the main module.
  208.  
  209.              The  procedure named "WriteStuff" is even more  tightly
  210.         controlled  than  that in the last program because this  one
  211.         doesn't  even  return a value to the  calling  program.   It
  212.         updates  its own internally stored value,  displays it,  and
  213.         returns control to the calling program.
  214.  
  215.              Compile  and run this program,  then we will go  on  to
  216.         global  modules.
  217.  
  218.                                GLOBAL MODULES
  219.  
  220.              As useful as local modules are,  they must take a  back
  221.         seat  to the global module with which you are already fairly
  222.         familiar  because you have been using them  throughout  this
  223.         tutorial.  The modules "InOut", "Terminal", and "FileSystem"
  224.         are  examples of global modules that you already know how to
  225.         use.   Now  you  will  learn how to write  your  own  global
  226.         modules  that can be called in exactly the same way as these
  227.         standard modules from any program.
  228.  
  229.                         YOUR FIRST DEFINITION MODULE
  230.  
  231.              In order to get started,  load and display the  program
  232.         named CIRCLES.DEF on your monitor.  The first thing you will
  233.         notice  is  that  we used a  different  extension  for  this
  234.         program  because  there is another part to the program  with
  235.         the same name but the usual extension "MOD".   What you have
  236.         displayed  on  your  screen is the definition  part  of  the
  237.         global  module  and it serves two very  important  purposes.
  238.         First,  it defines the interface you need to use the  module
  239.         in  one of your programs,  and it defines the details of the
  240.         interface  for the compiler so it can do type  checking  for
  241.         you  when you call this module.   The Modula-2 compiler uses
  242.         the  information  contained  here to  check  all  types  and
  243.         numbers  of  variables  just like it would do  in  a  singly
  244.         compiled program.
  245.  
  246.              The program on your monitor does very little.  In fact
  247.         its purpose is to do nothing because there are no executable
  248.         statements in it.  It is only to define the interface to the
  249.         actual program statements contained elsewhere.   Notice that
  250.         the  procedures  are  exported using the  qualified  option.
  251.         All  identifiers that are exported from a definition  module
  252.         must  be  qualified  so  that the user  has  the  option  of
  253.  
  254.  
  255.                                  Page 84
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                    Chapter 13 - Modules, Local and Global
  266.  
  267.  
  268.         importing   them  either  way.    It  is  legal  to   export
  269.         procedures, variables, constants, or types for use elsewhere
  270.         as  needed  for the programming problem  at  hand,  but  the
  271.         majority  of  exported items are procedures.   It should  be
  272.         obvious  that nothing within the module is available to  any
  273.         other part of the program unless it is exported.
  274.  
  275.                          THE IMPLEMENTATION MODULE
  276.  
  277.              We  are  not finished with the definition part  of  the
  278.         module yet but we will look at the implementation part of it
  279.         for  a few moments.   Load the program named CIRCLES.MOD and
  280.         display it on your monitor.   This is the part of the module
  281.         that  actually does the work.   Notice that there are  three
  282.         procedures here, two of which were defined in the definition
  283.         part of the module making them available to other  programs.
  284.         The procedure named "GetPi" is a hidden or private procedure
  285.         that  is  only available for use within  this  module.   The
  286.         other  two  procedures  are available to  any  program  that
  287.         wishes to use them simply by importing them.
  288.  
  289.              Anything  defined in the definition part of the  module
  290.         is also available here for use without redefining it, except
  291.         for  the procedure headers which must be completely  defined
  292.         in both places.   Anything imported into the definition part
  293.         of the module must also be imported here if it will be  used
  294.         in  this module,  imported identifiers are not automatically
  295.         transferred into this part of the module.
  296.  
  297.                       MORE ABOUT THE USE OF TWO PARTS
  298.  
  299.              The  definition part of the module defines  the  public
  300.         information  about the module and the implementation part of
  301.         the  module defines the private or hidden information  about
  302.         the module.   It may seem sort of silly to go to the trouble
  303.         of separating a module into two parts but there are at least
  304.         three good reasons to do so.
  305.  
  306.         1. You may not care how the module is implemented.
  307.  
  308.              In  all of the programs we have run up to  this  point,
  309.              you   probably   didn't  care  how  the   "WriteString"
  310.              procedure  did its job.   You only wanted it to do  the
  311.              job it was supposed to do to aid you in learning to use
  312.              Modula-2 efficiently.   It would have been senseless to
  313.              have  cluttered your monitor with the details of how it
  314.              worked every time you wanted to know how to use it.
  315.  
  316.         2. It hides details of implementation.
  317.  
  318.              If you were working on a large programming project  and
  319.  
  320.  
  321.                                  Page 85
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                    Chapter 13 - Modules, Local and Global
  332.  
  333.  
  334.              you  were  assigned to job of writing a  procedure  for
  335.              others  to  use that did some well  defined  task,  you
  336.              would  define the interface carefully and be  finished.
  337.              If,  however,  one  of the users studied your  detailed
  338.              code  and found a way to trick it into doing  something
  339.              special,  he  may  use  the trick in his  part  of  the
  340.              program.   If  you then wanted to improve your  routine
  341.              and  remove  the  code  that  allowed  the  trick,  the
  342.              interface would no longer work.   To prevent this,  you
  343.              give  others  only the interface to work with and  they
  344.              cannot  look for tricks.   This is called  "information
  345.              hiding" and is a very important technique which is used
  346.              on large projects.
  347.  
  348.         3. It allows for orderly development.
  349.  
  350.              It is possible to define all of the definition parts of
  351.              the  modules  and have all members of  the  development
  352.              team agree to the interface.   Long before the  details
  353.              of the individual procedures are worked out, the entire
  354.              team knows what each procedure will do and they can all
  355.              begin  work  on their respective parts of  the  overall
  356.              system.   This  is very effective when used on a  large
  357.              team effort.
  358.  
  359.                        COMPILATION ORDER IS IMPORTANT
  360.  
  361.              In  order for the above principles to work effectively,
  362.         a very definite order of compilation must be adhered to.  If
  363.         the   identifiers  declared  in  the  definition  part   are
  364.         automatically  available  in the implementation part of  the
  365.         module,  then it is obvious that the definition part must be
  366.         compiled before the implementation part of the module can be
  367.         compiled.   Also,  if  the definition part is  modified  and
  368.         recompiled,  then  the implementation part may also  require
  369.         modifications to comply with the changes and it must also be
  370.         recompiled.
  371.  
  372.              The  next  rule is not nearly so obvious but  you  will
  373.         understand it when we explain it.   When a calling module is
  374.         compiled,  it checks each of the imported identifiers to see
  375.         that  the  types  and number of  variables  agree  with  the
  376.         calling sequences used in the program.   This is part of the
  377.         strong  "type  checking" done for you by Modula-2.   If  you
  378.         modify  and recompile one of the called  definition  modules
  379.         and  attempt  to relink the program together,  you may  have
  380.         introduced  a  type incompatibility.   In order  to  prevent
  381.         this,  Modula-2  requires you to recompile every module that
  382.         calls  a  modified  definition  module.   It  does  this  by
  383.         generating a "key" when you compile a definition module  and
  384.         storing  the "key" when you compile the calling module.   If
  385.  
  386.  
  387.                                  Page 86
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                    Chapter 13 - Modules, Local and Global
  398.  
  399.  
  400.         you  attempt to link a program with differing  "keys",  this
  401.         indicates that the definition module was changed,  resulting
  402.         in  a new "key" and hence a mismatch,  and the  linker  will
  403.         generate an error.
  404.  
  405.                           WHY ALL OF THIS TROUBLE?
  406.  
  407.              It  may  not seem to be worth all of the extra  trouble
  408.         that the Modula-2 compiler and linker go through to do  this
  409.         checking  but  it  is important for a  large  program.   The
  410.         information used in the definition part of the module is the
  411.         type  of  information  that should be well  defined  in  the
  412.         design  stages of a programming project,  and if well  done,
  413.         very few or no changes should be required during the  coding
  414.         phase  of  the  project.   Therefore  it  is  expected  that
  415.         recompiling  several  definition modules should  not  happen
  416.         very  often.   On  the other hand,  during  the  coding  and
  417.         debugging  phase  of the project,  it is expected that  many
  418.         changes will be required in the implementation parts of  the
  419.         modules.   Modula-2  allows  this and still  maintains  very
  420.         strong  type  checking  across module boundaries to  aid  in
  421.         detecting sometimes very subtle coding errors.
  422.  
  423.              The above paragraph should be interpreted as a  warning
  424.         to  you.   If  you find that you are constantly  recompiling
  425.         modules due to changes in the definition modules, you should
  426.         have spent more time in the software design.
  427.  
  428.                          NOW TO ACTUALLY USE IT ALL
  429.  
  430.              With all of that in mind,  it will be necessary for you
  431.         to  reload  the  program  named  CIRCLES.DEF  which  is  the
  432.         definition  part  of  the  module,  and  compile  it.   Your
  433.         compiler  will  generate several different files for use  in
  434.         cross checking.   After you get a good compile,  reload  the
  435.         program  named CIRCLES.MOD which is the implementation  part
  436.         of the module and compile it.   During this compile, some of
  437.         the files generated by CIRCLES.DEF will be referred to.   It
  438.         would  be an interesting exercise to modify a procedure call
  439.         in  one  of  the programs to see what kind of  an  error  is
  440.         displayed.   After a good compile on both of these  modules,
  441.         you  have a new module in your library that can be used just
  442.         like  any of the other global libraries that came with  your
  443.         compiler.
  444.  
  445.              Load and display the program GARDEN.MOD for an  example
  446.         of  a program that calls your new library or global  module.
  447.         This  program  is very simple and should pose no problem  in
  448.         understanding for you.   The two new procedures are imported
  449.         and  used just like any other procedure.   Compile  and  run
  450.         this  program.
  451.  
  452.  
  453.                                  Page 87
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                    Chapter 13 - Modules, Local and Global
  464.  
  465.  
  466.  
  467.                      A FINAL WORD ABOUT GLOBAL MODULES
  468.  
  469.              From  the above description of global modules,  it  may
  470.         not  be  very obvious to you that it is perfectly legal  for
  471.         one  global  module  to call another  which  in  turn  calls
  472.         another, etc.  Program structure is entirely up to you.  For
  473.         example,  we could have called "WriteString" and some of our
  474.         other  familiar  procedures from within  the  "AreaOfCircle"
  475.         procedure.  The order of compilation must be kept in mind or
  476.         you  will  not  get a good compilation and linking  of  your
  477.         completed program.
  478.  
  479.              Remember  that there is nothing magic about the  global
  480.         or library (the names are synonymous) modules supplied  with
  481.         your  compiler.   They  are simply global modules that  have
  482.         already been programmed and debugged for you by the compiler
  483.         writer.  This is probably a good time to mention to you that
  484.         you   may  have  only  received  the  source  code  for  the
  485.         definition  part of the library modules with your  compiler.
  486.         Many  compiler writers will supply the source code  for  the
  487.         implementation  part  of  the library modules  only  if  you
  488.         supply them with a little more money.   After all,  they are
  489.         in  business  for  the money and most people never  wish  to
  490.         modify  the supplied routines but are happy to use  them  as
  491.         is.    All   compiler  writers  will  supply  you  with  the
  492.         definition part of the library modules because they are your
  493.         only means of interfacing with them.
  494.  
  495.                     THE PROCEDURE TYPE, SOMETHING NEW
  496.  
  497.              Load and display the program named PROCTYPE.MOD on your
  498.         monitor  for an example of a procedure TYPE.   In line 6  we
  499.         define  a  variable "OutputStuff" to be a PROCEDURE type  of
  500.         variable  that requires an "ARRAY OF CHAR" as  an  argument.
  501.         This variable name can now be used to refer to any procedure
  502.         that uses a single "ARRAY OF CHAR" as an argument.
  503.  
  504.              In  the  definition part of the program two  procedures
  505.         are defined,  each of which uses a single "ARRAY OF CHAR" as
  506.         an  argument.    Then  in  the  main  program  the  variable
  507.         "OutputStuff"  is  assigned each of the new  procedures  and
  508.         used  to  call them.   In addition,  it is used to call  the
  509.         supplied   procedure   "WriteString"   to   illustrate   the
  510.         possibility of doing so.   Finally,  the procedures are  all
  511.         called  in  their normal manner to illustrate that there  is
  512.         nothing magic about them.  Any procedure type can be used to
  513.         call  any  procedures that use the same  type  of  parameter
  514.         calls as those defined when it is created as a variable.
  515.  
  516.  
  517.  
  518.  
  519.                                  Page 88
  520.