Some variables in SQL-Ledger code

by Armaghan Saqib, Thursday, December 15, 2022, 00:25 (495 days ago)

There are two variables which you will see every where in SQL-Ledger code. These are:

%myconfig
$form

This section is explanation of both these variables.

%myconfig

by Armaghan Saqib, Thursday, December 15, 2022, 00:27 (495 days ago) @ Armaghan Saqib
edited by Armaghan Saqib, Thursday, December 15, 2022, 00:46

When you add new users in sql-ledger using admin.pl (or through HR--Employees--Add Employee), it is added to the 'users/members' text file. This 'members' file is a text file which stores information in key/value pairs with username as the section name.

Here is one such section:

[admin@demo]
company=UK DEMO
dateformat=mm-dd-yy
dbconnect=dbi:Pg:dbname=demo;host=localhost
dbdriver=Pg
dbhost=localhost
dbname=demo
dboptions=set DateStyle to 'POSTGRES, US'
dbuser=postgres
[email protected]
name=admin
numberformat=1,000.00
password=adZILgCC7Lk7U
stylesheet=sql-ledger.css
templates=demo
vclimit=1000

When this user logs in into sql-ledger, a conf file named after this user is created in the users folder in Perl syntax (actually, a hash variable named %myconfig) and then included in the perl code. For example in this example '[email protected]' will be created and executed whenever you will login or access any sql-ledger function.

Within the sql-ledger Perl code, you can access user configuration from this user hash variable using standard syntax. So $myconfig{dbname} will return the current database name and $myconfig{company} will return the company name.

This Perl hash is also passed from executable scripts to modules (in SL folder) as a reference as part of parameters list. So in modules you access this using hash reference syntax. This means that now $myconfig->{dbname} will return the current database name.

Exercises:

1. Open users/members and see what is in it.
2. Open any conf file in users/ folder and see how user information is stored there.

$form

by Armaghan Saqib, Thursday, December 15, 2022, 00:45 (495 days ago) @ Armaghan Saqib
edited by Armaghan Saqib, Friday, December 23, 2022, 11:45

$form is a hash reference which contains the data sent by user through GET or POST http requests (whenever Save, Delete, Post buttons on forms are pressed). So, for example, if you had the following field in your form:

<input type=text name=firstname size=10 value="Armaghan Saqib">

You can access it with $form->{firstname} in your Perl code once that form is submitted.

It is also used as a global variable to store data temporarily read from database as needed. This makes it convenient to pass data around different 'subs' or modules.

$form is created in SL/Form.pm's 'sub new' and also converted into an object with 'bless' statement in the 'sub new'. This means that the functions and procedures in the SL/Form.pm can be used as properties and methods on the $form hash.

For example, you can format a number into the user specified number format with following code:

my $formatted_number = $form->($myconfig, 19290, 2);

And $formatted_number will contain "19,290.00".

Exercise:

Please open SL/Form.pm and go through all the subs to see what it has to offer.

RSS Feed of thread