• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Port Perl Script to PHP

  I need to port a Perl script to PHP.  The script emulates the Word "compare documents" feature when comparing different versions of an HTML document on the server.  I've seen the Perl-to-PHP translation guide at http://www.cs.wcupa.edu/~rkline/perl2php/ , but I need help from someone with expertise in both Perl and PHP.
    The Perl script's short source code is visible at: http://216.92.61.99/htmldiff2.htm .
   The expert who answers with a good PHP port can get points here and at the corresponding PHP Question at http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_22049380.html (by leaving the answer in both areas, or the answer in one area and a note about it in the other). Thanks.
0
Randall-B
Asked:
Randall-B
  • 4
  • 4
3 Solutions
 
ahoffmannCommented:
why would you downgrade something good?
Your code makes heavy use uf regex, which are badly supported in PHP (at least the correct usage there is cumbersome and error-prone).
So in short words: you can use most of the code as is in PHP, but you need to rewrite all parts with regex in PHP and call the proper functions. Doing that you have to evaluate the status code returned by the function, and depending on that evaluate the reults.
Then you also have to beat some strange behaviours with PHP's so-called PRE.
Nice job, good luck.

I'd simply do following:
<?php system("/path/to/your/working/perl-script"); ?>
0
 
Randall-BAuthor Commented:
ahoffman,
   I was hoping to port it to PHP, so the PHP application that I'm creating can be fully PHP (even though it may be inferior to Perl) and would also be easier to test on Wamp (since extensive testing has shown that this Perl script does not run the same on Windows Perl as it does on Unix, believe it or not).
   When I run this PHP script on Unix, the Perl script that it call does run pretty well: <?php system("/usr/www/users/myuser/cgi-bin/htmldiff2.cgi"); ?> , but it is printing the "Content-type: text/html" characters to the top of the screen. My Perl script has to begin with:
   #!/usr/local/bin/perl -w
    print "Content-type: text/html\n\n";
because it's on Apache on Unix. Without that line, the Perl script gives a 500 error.  If I'm going to call the Perl script from PHP, there would need to be some way to prevent the characters ("Content-type: text/html") from actually printing to the top of the screen.

    Another reason I wanted to port to PHP is that it would be easier for me to modify. For example, instead of comparing two HTML files whose paths are hard-coded into the Perl script, I eventually want to modify it to allow the user to specify two files (e.g. from two list-boxes); or better yet, to automatically compare two versions of HTML source stored in different rows of a MySQL database (where it would select the the newest (most recently-edited) version from the several versions that have been saved into new rows of the database), and each time another user opens the application, they will see the comparison of the most recently-edited version to the original version.

I started the translation but didn't get very far yet. I'd appreciate some help with it.  Thanks.
0
 
Randall-BAuthor Commented:
ahoffman,
   Correction: I should have tried commenting out the "print "Content-type: text/html\n\n"; .  When I comment out that line, it runs fine and does not print the "Content-type: text/html" to the top of the screen.
    But I would still like help translating the script to PHP for other reasons. Thanks.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ahoffmannCommented:
> But I would still like help translating the script to PHP ..
as I said: you need to write complex code fore ach and every regex used in the perl code
If you've done that, corrected all the errors you get, and fixedother PRE problems, then post the code.
Believe me, it's not simple to write regex which will work flawless using PHP ...
0
 
Randall-BAuthor Commented:
ahoffmann,
   Thanks. I don't doubt your word for a minute. I know it would be tough; that's why I was asking for expert help.
   If such PHP code would be inefficient and unreliable, as an alternative maybe you could help me customize the Perl script to interface with the PHP script better and use the original and most-recent versions of the html in database rows for the comparison, instead of using files in a directory.
    For example, if the PHP script has a GET variable for a document number (e.g. 10B-4), such as script.php?10B-4 , how would I pass that GET variable from the PHP script to the Perl script?

   And then, how would I accomplish this kind of database interface in Perl:

    $doc = $_GET['docnumber'];
         function connect() {
            $conn = mysql_connect("localhost", "root", "");
            mysql_select_db("database");
           return $conn;
        }
   $conn = connect();

  $OldFile = the results of a mysql_query such as "SELECT html FROM table WHERE docnumber = '$doc' AND revisiontime = the original (oldest) row with an entry for that docnumber (based on a datetime field).

  $NewFile = the results of a mysql_query such as "SELECT html FROM table WHERE docnumber = '$doc' AND revisiontime = the newest (most-recently-added) row with an entry for that docnumber (based on a datetime field).
   That way, any user (in a collaborative document-revision effort) would always see a comparison of the most-recent revision to the original.

   The script would also need to prevent other users from editing any rows containing that docnumber, so that changes would not be made by others while the current user has a document open.  Please advise how to modify the Perl script to accomplish this. Thanks.
0
 
ahoffmannCommented:
> .. PHP code would be inefficient and unreliable,
well, didn't say this (even it may be my mind:)

> ..  how would I pass that GET variable from the PHP script to the Perl script?
$p = $_GET['key']; system("perl-script $p");

>  And then, how would I accomplish this kind of database interface in Perl:
use DBI;
my $dbh=$dbh = DBI->connect("dbi:mysql", "root", "secret");
my $sth = $dbh->prepare("USE your-database");
$sth->execute();
0
 
Randall-BAuthor Commented:
  Before I use  $p = $_GET['key']; system("perl-script $p");   in the PHP script, I'll need to place a similar variable input near the top of the Perl script, right? What's the Perl syntax for " $p = $_GET['key']; "?
  Also, in the DBI connect code you provided, exactly where does the SQL select statement go? Thanks.
0
 
ahoffmannCommented:
> What's the Perl syntax for " $p = $_GET['key']; "?
use CGI;
my $q=new CGI;
my $p=$q->param('name-of-param');

> .. exactly where does the SQL select statement go? T
$dbh->prepare(----your SQL here ----);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now