1.1. WebTools basics
Actually WebTools is a Perl module that concentrate
powerful tools for development of CGI applications. However in real
life you will never be asked to use webtools module directly, instead
you *must* use the only one cgi script: process.cgi ! That's not
mean that you have to write perl code in that file, even it is not
recommended to be modified. You can think about webtools and process.cgi
as your high level compiler or pre-processor.
Your scripts will be loaded and executed through process.cgi
and they will look like common php script. So no more dealing with
*shebang* and permission at all, once you have successfully instaled
your webtools system.
However your script may contain rather more than Perl code
- it can contain WebTools code! That's mean that you can have mixed
together Perl code, HTML, Templates. Take a look at diagram to see
how everything works:
Let's see what's happening when you run your script, for example
script.whtml, through you favourit browser:
1. You make query via browser:
http://www.yourserver.com/cgi-bin/webtools/process.cgi?file=script.whtml
2. Browser connect to your server through tcp protocol
using http or https interface.
3. Your server (Apache) find associated program to your '.cgi'
extension. That's should be your Perl compilator/translator.
(Under Unix/Linux/Windows
Apache use shebang to determinate appropriated program)
4. Server run Perl with respective user/permission
and Perl load and execute process.cgi
5. At this point process.cgi load webtools.pm
6. webtools.pm load globexport.pm ,strdouthandle.pm and some
additional modules and libraries that are need. It (globexport)
parse input data (using modified version of cgi-lib), parse cookies,
call additional security checks and makes global variables. From
another hand stdouthandle.pm "catch" STDOUT handle and
allows script to print-ing in buffered mode (default).
7. process.cgi run "pre-processor" routine that
parse your webtools code (script.whtml) and proceed available templates.
8. webtools.pm trigger "onStartUp" and "onActivate"
events, that are executed before your webtools script!
9. process.cgi "run" (eval) proceeded script and
it (script.whtml) return output data to browser through Apache server.
The most interesting moment is the time of webtools script(.whtml)
execution.
At this point script get everything: Input data is parsed (and allowed/trusted)
variables become as global (default ALL variables become global!),
cookies also become global, attachments are parsed and saved into
your default temp directory. Also all templates used into your script
(for example your header and footer html page) are parsed and ready
to be used/printed.
Here your script can lunch a lot of webtools available libraries
like: mail.pl (raw email-ing), xreader.pl (exteded template reader/parser),
htmlparse.pl (html form filler), deepwak.pl (recursive file operations),
dump.pl (on-line debugger), php.pl (useful sub set of Php functions),
Categories.pm (MySQL based categories managment), LinksMover.pm
(results page framing) and more...
With another words you can, for a couple of minutes, to create
complecated Web application that allows user login/signup; to use
sessions; to send mails and so on!
What's the deal?
At first time you have to do a lot of work (it may take up
to couple of hours, together with reading of respective documentation).
For example you have to install your script into your web (cgi-bin
based directory); to set permissions (in case of need!) and to configure
"config.pl" file (through install.cgi script).
Of course you probably will need database ineraction (Mysql,
Access, Flat SQL are supported), so you have to do a little bit
work to set right adjustments (via install.cgi that is a simple
game).
So when your WebTools is properly configured you will never
ever need to have deal with *real* CGI scripts. You will only need
to learn how to write powerful webtools scripts.
Ok, that's fine! But what's the deal?
All right, till now to write one complicated CGI script you
have to write in Perl (or other language) a lot of code, now you
become WebTools programmer because that's easiest way to write CGI
scripts. For example you can write: start_session(...) and new session
is done. You need to send mail... write mail(...) and it's done
too. So the big deal is that you short-cut your development time
using powerful webtools functions (Do you really need to know how
exactly mail is send?!)
[Top]
1.2. WebTools scripts
WebTools script is extended Perl script that contain
itself additional features and build-in functions like global
variables, sessions, templates and so on. Somebody can
tell you that webtools is template processor and somehow it
will be right, but that will not be the whole truth. WebTools
script often is recognized with ".whtml"
extension, but ".cgihtml",
".cgi" and ".html"
are also allowed.
These scripts have similar to Php style but with Perl
syntax. So that is good point for Perl and Php programmers
that intend to use WebTools as CGI programming layer.
Basically any webtools script can be split into three
main parts:
1. Pre-proceeding part.
2. Main code
3. Html code/ templates
*
Pre-proceeding part is actually perl code that is executed
before main script i.e. that are onStartUp
and onActivate parts.
onStartUp code is evaluated
just before input data to be parsed, here you can change
at run-time your configuration file, to change your temp
directory to set Get/Post restriction to restrict variables
to become global. That part lives into Globexport.pm namespace.
onActivate is evaluated into
webtools.pm namespace just before your real script to be
executed. This piece of code is designed to be used to re-write
your configuration, for example to change default database
user, password, db name and so on.
These two parts are placed into your script and looks like
templates.
*
Main code - To initiate begin of script you have to put
Php-like identifier "<?perl",
to set end of your code you must put "?>".
In one webtools script you can have as much as you need
pieces like that. See an example:
<?perl
print "Hello "; ?>
<?perl
print "world!";
?> |
Html
code/templates - on default you script is in html context,
so next "script" will be valid:
Hello
world!
However
you can mix together html and perl code (however I don't
like that style, so use external templates).
<?perl
my $message = 'My name is Julian!';
?>
Hello World!<BR>
<?perl
print $message;
?> |
Note:
All html parts through compilation time will be converted
in valid Perl code, ready to be printed as common string.
Next example shows you complete example that combine these
three parts together:
| <!--#onStartUp>
# Input stream is
not parsed! Variables are not global yet!
# If you need to print some messages don't
forget "Content-type: text/html\n\n"!
# Only available due StartUp process (parsing,loading
drivers and so on)
$webtools::my_cgi = 8388608; #
8MB (if you want load it from external file)
$webtools::WConfig{temp_path}='/tmp/your_folder/';
$webtools::WConfig{cgi_lib_maxdata}=$webtools::my_cgi;
# Set new POST limit size (8MB)
@ARGUMENTS = ('action','id','name'); #
Only these variables will become global!
%EXPECTED = ('action'=>'field','id'=>'field','name'=>'field');
# Only these variables
will be available to script. Any other fields/uploads
will be dismissed.
</#onStartUp-->
<!--#onActivate>
# You are in "webtools.pm"
-> All webtools functins are avaliable here!
# If you need to print some messages just
go ahead :)
# Available through script execution
# Currently '$webtools::WConfig{cgi_lib_maxdata}'
still has default value (not 8MB)!
$webtools::WConfig{cgi_lib_maxdata}=$webtools::my_cgi;
# Set new POST limit size again
for script!!!
$webtools::WConfig{sql_driver}='db_mysql';
# ...Change default db driver...
</#onActivate-->
<HTML>
<HEAD><TITLE>Example</TITLE></HEAD>
<BODY>
<?perl
print "Hello from my script!";
?>
</BODY>
</HTML>
|
To learn more about onStartUp and onActivate routines read
next article:
[Top]
|
|