Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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 );



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:

sub mult( $$ ) { my ($a,$b) = @_; my $prod = $a * $b; print <<EOF <center> <h1> $a x $b = $prod </h1> </center> EOF }

Again, it takes two numbers, multiplies them, and returns a formatted result.

The generation of the multiplication table is also similar the PHP version:

print qq(<center>); print qq(<table border="1">); print qq(<tr>); print qq(<td>X</td>); for (my $r=0; $r<10; ++$r) { print qq(<td>$r</td>); } for (my $r=0; $r<10; ++$r) { print qq(<tr>); print qq(<td>$r</td>); for (my $c=0; $c<10; ++$c) { my $link = alLink( "main::mult", $r, $c ); print qq(<td> <a href="$link">$r x $c</a> </td>); } } print qq(</tr>); print qq(</table>); print qq(</center>);

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 );



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap