TYL PROGRAMMING LANGUAGE
▶ WORKING WITH MODULES
Modularizing Tyl Programs
User created modules in Tyl, are type of libraries that hold variables, statements and functionalities, that are to be used by another program. But there are some differences between libraries use in other languages and Tyl. Therefore it is called a module, because Tyl system treats it as any module in the sense that it runs it as well as the target module.

In this chapter we will work with a little CRM implementation, that has crmdb database, and client table, that has name, fname, age and phone columns. This is the stand alone version of an insert program, that can be named 'crm.insert.tyl':
≡ connect to MS SQL database
dbname 'crmdb'
connstr 'data source=(LocalDb)\MSSQLLocalDB;initial catalog=' + dbname + ';integrated security=true;connection timeout=60;'
mssqldb connstr
mssqldb.connect
mssqldb.connected ?
    print 'Connected to mssqldb `' + dbname + '` successfully.'
    print
    print 'type client information (name family-name age phone):'
    input clientinfostr
    clientinfo { name '' fname '' age 0 phone '' }
    clientinfo getclientinfo clientinfostr
    cmd 'insert into client (name, fname, age, phone) values(`'
cmd ++ clientinfo.name + '`, `'
cmd ++ clientinfo.fname + '`, '
cmd ++ clientinfo.age + ', `'
cmd ++ clientinfo.phone + '`)'
mssqldb.execute cmd ?
  print 'Client `' + clientinfo.name + ' ' + clientinfo.fname + '` was inserted successfully.'
  \
  print mssqldb.sqlerror
^
    \
    print 'Connection failed!' + newline
    print mssqldb.sqlerror
^
getclientinfo str »
    words string.words str
    result { name '' fname '' age 0 phone '' }
    ind -1
    result k v ~ result k words ( ind ++ )
    result
The program does its job efficiently, and sure to achieve its goals. But if we need to write all the code that involves other database management functionalities: update, delete and select, it will be tedious to repeat all this code. The solution is to modularize the code, meaning to put pieces of the code in other modules, so that they will all work as an application.

First, let's separate user interactivity from database functionalities by creating a program 'crm.dal.tyl', that will be the crm.dal module:
!
!     CRM.DAL MODULE
!
!     this module acts as the data access layer for
!     crmdb database
!
!==== module statements section =================
! variables
mssqldb 'data source=(LocalDb)\MSSQLLocalDB;initial catalog=crmdb;integrated security=true;connection timeout=60;'
clientinfo { name '' fname '' age 0 phone '' }
! init
isconnected connect
!==== module functions section =================
connect:
mssqldb.connect
mssqldb.connected ? %% \t
print 'Connection failed!' + newline
print mssqldb.sqlerror
 \f
insertclientbystr infostr: insertclient getclientinfo infostr
insertclient info:
    clientinfo info
    cmd 'insert into client (name, fname, age, phone) values(`'
cmd ++ clientinfo.name + '`, `'
cmd ++ clientinfo.fname + '`, '
cmd ++ clientinfo.age + ', `'
cmd ++ clientinfo.phone + '`)'
mssqldb.execute cmd ?
  print 'Client `' + clientinfo.name + ' ' + clientinfo.fname + '` was inserted successfully.'
  \
  print mssqldb.sqlerror
^
getclientinfo str:
    words string.words str
    result { name '' fname '' age 0 phone '' }
    ind -1
    result k v ~ result k words ( ind ++ )
    result
Then, create program 'crm.app.tyl', that will be the crm.app application running program:
!
!     CRM.APP MODULE
!
!     this module acts as the application running
!     program for handling client resources management
!
!==== module statements section =================
#mod crm.dal
isconnected ?
    print 'Connected to database successfully.'
    print
    print 'type client information (name family-name age phone):'
    input clientinfostr
print
    insertclientbystr clientinfostr
^
crm.dal is used as a library that holds variables and functionalities, that are used by crm.app. Upon running crm.app, in line #mod crm.dal, crm.dal is being included or called. Upon calling crm.dal, all its statements in its module statements section are executed.

crm.dal has also an init statements that act as an initialization of the module. So in line isconnected connect, an isconnected global variable is declared and assigned the result of connect function, so when the application is running, connection to the database is done, and if successful, isconnected will be TRUE. Note that all of the variables and functions of crm.dal, are also variables and functions of crm.app, meaning isconnected can be used in crm.app program.

After calling crm.dal, all crm.app has to do is getting client information string from the user, and send it to crm.dal insertclientbystr function, that will parse it and feed it to a SQL command for execution.
PART 2