HSP Documentation

Introduction

HSP allows executing Perl code that is embedded in plain text files that otherwise would not contain perl code. Similar to ASP, JSP or PHP you can define code blocks within plain text.
Example:

<~
 if($form{go} ne ''){
	htsendheader('location' => $form{go});
	htflush();
 }else{
	print "<form>URL: <input name=go><input type=submit></form>";
 }
~>

Syntax

Define code blocks
In order to embed perl code into otherwise plain text you must define areas that shall be interpreted. To do so you can use the following syntax:
<~
  # Perl code goes here
   ~>

The perl code is embedded between <~ and ~>. You can also use the following syntax:

  • <% ... %>
  • <? ... ?>
  • <script language="HSP"> ... </script>

But you cannot mix different them! If you decide to use one type of code block delimiters then you have to use it until the end of the file. That is if you have a "<% %>" code block you cannot have a "<? ?>" code block unless you put it into another file and embed it with the htembed command.
A special kind of code blocks was introduced to make variable interpolation easier:

<~..$foo~> or <~=$foo~>
These code blocks will be eval'ed and then printed. It is the same as <~ print $foo ~> only shorter.
You may also write things like <~.. $foo*100/$bar ~>
Content-Type and other HTTP Headers
Normally you don't have to care about the content-type header because HSP will automatically send it out. By default content-type is set to "text/html". If you need to change the content-type or if you want to add more headers you can use the htsendheader command:
htsendheader(NAME => CONTENT);
htsendheader(NAME => CONTENT [, NAME => CONTENT ]);

NAME is the type of the header. CONTENT is the value of the header. For example this will redirect the browser to another page:

htsendheader("location" => "/foo/bar.html");
Output Buffering
HSP buffers output before it is sent out. That is done so you can send HTTP Headers from everywhere in your script. The buffer is autoatically flushed at the end of the script. You can manually flush the buffer with the htflush command:
htflush;
Split Code into several Code Blocks
Everywhere where whitespace is allowed you can split the code and start a new code block unless you are in a regular expression. Any plain text between the two code blocks is interpreted as a print statement. For example you can write:
<~ if($foo){ ~>
 Hello World
<~ } ~>

That is the same as

<~
 if($foo){
  print "Hello World";
 }
~>

Don't do things like this:

<~ $foo = " ~> Hello World <~ " ~>

or

Hello World <~ =~s/\W+//g ~>
Form Values
HSP automatically reads and parses form values for you. POST and GET method and file uploads are supported. Form values are stored in the global hash %form.
Outsourcing HSP
You can embed external HSP code with the htembed command:
htembed("myscript.hsp"); # embed a .hsp script
You can even pass arguments to the embeded code:
htembed("myfile.hsp", {foo => 'bar'}); # pass arguments to .hsp script
The embeded script can access the arguments through %form. If you don't provide arguments then %form will contain the same values as in the calling script.
Print Environment
You can print the content of %ENV if you browse http://yourserver.com/myscript.hsp?hspinfo_env. You can use any HSP script to view %ENV.

Installation

Installation without root access
If you can use .htaccess files on your server to make .hsp files executed by HSP. Create or modify the .htaccess file in the root directory or in the directory where you want to run HSP files. Add the following lines to it:
AddHandler hsp-perl .hsp
Action hsp-perl /cgi-bin/hsp/hsp.pl

The second argument of the Action directive must match the relative URL of hsp.pl. Place hsp.pl in the cgi-bin of your server and edit the first line of hsp.pl to match the perl intepreter. Upload hsp.pl to the specified directory and make it executable by using CHMOD 755. The first line in hsp.pl must match the path to perl on your server.

Installation with root access
On Apache you can make all files with .hsp extension to be executed by HSP by editing the http.conf file. Add these lines to http.conf:
ScriptAlias /hsp-perl/ "PATH_TO_HSP.PL"
AddType application/hsp-perl .hsp
Action application/hsp-perl /hsp-perl/hsp.pl

PATH_TO_HSP.PL must be replaced with the path to hsp.pl.
Also open hsp.pl and edit the first line to match the path to the perl interpreter.

Author

Trung Nguyen, 2001.
trung.nguyen@2n-com.de