Wednesday, August 14, 2013

Tips on Converting PHP to Perl-CGI

The story today is very similar to yesterday's. Last month I created a new butterfly identification page for my personal website, using PHP and SQLite. By choosing some colors and patterns, newbies (in the butterfly field) can identify any butterflies they may have seen in the field. The page worked well on my desktop but when I uploaded it to Tripod, I realized that they support only Perl and not PHP. So I decided to convert the PHP page to Perl, without having to recode it from scratch. I started by making a copy of the ".php" file and renaming it to ".cgi". Then I made the below changes to convert the PHP code to Perl. Hope these tips would be useful to someone who reads this post.

* Added the Perl shebang line at the top of the page
* Added the standard line to print the content type
* Enclosed all other HTML code within multi-line prints
* Replaced all instances of the PHP "echo" with the Perl "print"
* Replaced the database connection PHP code with the corresponding Perl code
* Replaced the database query PHP code with the corresponding Perl code
* Added a subroutine for getting the CGI parameters, something that PHP automatically did
* Replaced the PHP built-in hash "%POST" to the hash "%form" created by my subroutine
* Replaced the square brackets used for PHP hashes with the curly brackets used in Perl
* Removed the single or double quotes that were used to reference the hash values in PHP
* Replaced the not equal to operator "<>" of PHP with the Perl equivalent "ne"

P.S.: After all that hard work, I found that Tripod does not support the use of DBI.pm. So finally I had to replace the dynamic butterfly identification page with an XML-XSLT page where the readers have to look at a table giving colors and patterns to try and figure out which butterfly they had seen. Well, such is life!

No comments:

Post a Comment