Rediscover (and Structure) CGI with a Simple Link Technique : Page 4

CGI is the most widely used protocol for Web programming, but it lacks basic programming constructs such as function calls. You can work around these weaknesses by encoding function calls directly into Web links easily creating active linksand you can do it in PHP, Perl, JavaScript, and Java.

by Greg Travis

Jun 10, 2004

Page 4 of 6

Freezing and Thawing in PHP
It's time to take a closer look at how the function call is frozen and thawed. The function call is defined by an array containing the name of the function and the arguments to that function. This array is turned into a string using the serialize method. This string is no good for use in a link, so you must first encode it with the urlencode() function. Here's the function alLink() which takes care of this:

When the user clicks on the link, the frozen function call must be thawed:

$info = unserialize( $einfo );
alApply( $info );

function alApply( $info )
{
$fun = array_shift( $info );
call_user_func_array( $fun, $info );
}
Mult in Perl
Now, consider the same system written in Perl. Other than the obvious syntax changes, the two implementations are more or less the same. Here's the multiplication routine:

This is more or less the same as the PHP, except for the syntax change. Note that you have to specify the package that the mult() routine lives in-in this case, 'main'. This is because the ActiveLink code lives in its own package, and needs to know what package the target routine is defined in.

Freezing and Thawing in Perl
To freeze the function name and arguments, use the Data::Dumper package to turn them into a string, and the uri_escape() function to encode it for inclusion in a link. This is done in alLink():