Solved

Port Perl Script to PHP

Posted on 2006-11-04
8
287 Views
Last Modified: 2012-06-21
  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
Comment
Question by:Randall-B
  • 4
  • 4
8 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 17924745
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
 

Author Comment

by:Randall-B
ID: 17925061
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
 

Author Comment

by:Randall-B
ID: 17925081
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17925492
> 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Randall-B
ID: 17925621
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 17926453
> .. 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
 

Author Comment

by:Randall-B
ID: 17926573
  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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 17928459
> 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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now