Browse DevX
Sign up for e-mail newsletters from DevX


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.

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:

function alLink()
  global $alFile;

  $info = func_get_args();

  $enc = urlencode( serialize( $info ) );
  return "$alFile?_alinfo=$enc";
When the user clicks on the link, the frozen function call must be thawed:

$info = unserialize( $einfo );
alApply( $info );
Here's the call to alLink():

my $link = alLink( "main::mult", $r, $c );
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():

sub alLink
  my @info = @_;

  local $Data::Dumper::Indent = 0;
  local $Data::Dumper::Purity = 1;

  my $enc = uri_escape( Dumper( \@info ) );

  return "$alFile?_alinfo=$enc";
To thaw the function call, call eval() on the frozen string, which then provides a list:

my $info = eval( $src );
alApply( $info );
As before, alApply() takes the first element in the list and applies to the rest of the elements in the list:

sub alApply( $ )
  my $fun = shift @$info;

  $fun->( @$info );

Thanks for your registration, follow us on our social networks to keep up-to-date