PSP::Engine - $psp object class and primary engine of the Perl Script Pages
use PSP::Engine; my $psp = new PSP::Engine( conf => new PSP::Config(), cgi => new PSP::CGI(), no_cgi => 1, file => 'file.psp', execute => \$text_string, args => $args ); my $success = $psp->execute(\$text_string,'filename',$args); my $success = $psp->file('file.psp',$args); $psp->print($text); $psp->printError($text,$filename); my $cgi=$psp->cgi(); $value = $psp->setvar('variable',$value); my $value = $psp->var('variable'); $psp->var->{variable} = $value; my $value = $psp->var->{variable}; my $var = $psp->var(); $var->{variable} = $value; my $value = $var->{variable}; my @vars = $psp->vars(); my $text = $psp->flush(); my $errors = $psp->flushErrors();
This module provides the engine for the Perl Script Pages (PSP). PSP code is executed within a PSP::Engine object and the object exports itself to the PSP code being executed.
Creates a new object of the PSP::Engine class.
use PSP::Engine; my $psp = new PSP::Engine( conf => new PSP::Config(), cgi => new PSP::CGI(), no_cgi => 1, file => 'file.psp', execute => \$text_string, args => $args );
None.
The following parameters are hash key/value parameters.
Executes PSP code from a string reference.
my $success = $psp->execute(\$text_string,$filename,$args);
execute(\$data,[$a,$b])
will execute the PSP code
stored in $data. The code can access the first argument ($a) via $args->[0] and
the second one ($b) via $args->[1].
Loads PSP code directly from a file, changes directory to the file's directory and executes the code.
my $success = $psp->file('file.psp',$args);
Please note that paths/filenames with leading slash ('/') are not absolute to the filesystem but relative to the BaseDirectory, configured through PSP::Config! You may set BaseDirectory to '' if you wish to have ``real'' absolute paths.
file('file.psp',[$a,$b])
will execute file.psp,
the code within file.psp can access the first argument ($a) via $args->[0] and
the second one ($b) via $args->[1].
Appends data to the PSP output.
$psp->print($text); $psp->print(@text);
None.
None.
Appends an error message to the PSP error output.
$psp->printError($text,$filename);
At the moment, errors printed through this method are also appended to the PSP output. This may change (i.e. become optional) in future versions.
None.
Returns a reference to the PSP::CGI object associated to the object, if present.
my $cgi = psp$->cgi();
None.
None.
Stores a value for a given variable name in the object. This is used for exchanging data between PSP code instances.
my $varvalue = psp$->setvar($varname,$varvalue);
None.
Either returns the value stored for a named variable inside the object, or a reference to the variables hash if variable name is omitted.
my $varvalue = psp$->var($varname);
my $var = psp$->var(); $var->{$varname} = $varvalue; my $varvalue = $var->{$varname};
None.
Returns an array of variable names in use.
my @vars = $psp->vars();
None.
None.
Returns the object's output data and resets the data storage.
my $text = $psp->flush();
None.
None.
Returns the object's error messages and resets the error message storage.
my $errros = $psp->flushErrors();
None.
None.
When constructing a PSP::Engine object, it expects a PSP::Config and a PSP::CGI object as hash parameters:
my $psp = new PSP::Engine( conf => new PSP::Config(), cgi => new PSP::CGI() );
If one or both objects are omitted, the ``missing'' object(s)
will be created.
See also the PSP::Config manpage and the PSP::CGI manpage.
The no_cgi => 1 option prevents the PSP::Engine constructor from creating a new PSP::CGI object:
my $psp = new PSP::Engine( no_cgi => 1 );
This is useful if PSP is not used for CGI but for i.e. templating tasks.
Both the file method and the execute method may be used directly from the object constructor for comfort:
my $psp = new PSP::Engine( file => 'file.psp', args => $args );
my $psp = new PSP::Engine( execute => \$text_string, args => $args );
PSP code may either be loaded from a file using the file method or directly from a text string reference using the execute method:
my $success = $psp->execute(\$text_string,'filename',$args); my $success = $psp->file('file.psp',$args);
Both methods return either 1 or undef to declare success or failure of code execution.
The $args parameter is optional. If set, it will be available to the PSP code
executed via the $args variable. It is intended to supply arguments to the
code, i.e. via an array or hash reference.
For example $psp->file('file.psp',[$a,$b])
will execute file.psp, the code
within file.psp can access the first argument ($a) via $args->[0] and the
second one ($b) via $args->[1].
Please note that paths/filenames with leading slash ('/') are not absolute to the filesystem but relative to the BaseDirectory, configured through PSP::Config! You may set BaseDirectory to '' if you wish to have ``real'' absolute paths.
The second parameter for the execute method, 'filename', is optional and may be undef. It is only used for constructing an error message if execution fails.
You can use the execute method and file method more than once, resulting output is appended.
Using the print method, you may append data to the PSP output to be flushed later:
$psp->print($text);
For error messages, there is a printError method, also accepting a text string as parameter:
$psp->printError($text,$filename);
The $filename parameter is optional and may be omitted. If given, the error message will be prepended with this filename.
At the moment, errors printed through the printError method are also appended to the PSP output. This may change (i.e. become optional) in future versions.
After PSP code execution, the resulting output is to be flushed from the object to a string using the flush method:
my $text = $psp->flush();
The same applies to code execution error messages via the flushErrors method:
my $errors = $psp->flushErrors();
The object can be re-used after flushing.
Veit Wahlich
E-Mail: cru |at| zodia |dot| de
WWW: http://home.ircnet.de/cru/
v0.7 Wednesday, 18 January 2006
Copyright 2004-2006 Veit Wahlich
This software is distributed as free (libre) software under the terms of the GNU General Public License, version 2 <http://www.gnu.org/copyleft/gpl.html>. The author disclaims responsibility of any damage or harm caused directly or indirectly by usage of this software. Use only at your own risk.