Link to home
Start Free TrialLog in
Avatar of wjdashwood
wjdashwood

asked on

Newbie - Convert working php code to perl

I have programmed before using very basic perl but it was a long time ago and I didn't exactly get on with it. I chose to learn PHP instead which is great, but I have a web page hosted on a server without php capabitites. It does, however have perl.

I have a html/php script which the user enters details (including an email address) sumbits in and the php checks that the required fields are filled in and that the e-mail address is of the correct format. It then either outputs a well done message and sends the thing, or it outputs the errors and the form again.

I was hoping I would be able to get some help converting my php code into perl. This is the code I have so far:

<body>
      <?php
//validation code for email send
   if ($support=="yes"){
      if($email==""AND $name=="" AND $tel=="" AND $detail==""){
      $noinput="<br/>Please make sure you complete the form before clicking the submit button<br/>";
      $show="y";
      }
      else{
             if($email==""){
         $noemail="<br/>You must enter an e-mail address<br/>";
         $show="y";
      }
      else{
         function EmailCheck($email)
         {
                $result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email );
         if ($result)
           return TRUE;
         else
          return FALSE;
                }

             $valid = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email );
      if (!$valid){
      $notvalid="<br/>The e-mail address you entered was not valid, please check it and try again<br/>";
      $show="y";
      }
      
      }//end e-mail else
      
      if($name==""){
      $noname="<br/>You must enter a name<br/>";
      $show="y";
      }
      
      if($detail==""){
        $nodetail="<br/>You must enter a message<br/>";
        $show="y";
      }
      
      }//end validation

      if ($show==""){
    $to = "Support@anaddress.co.uk";
    $subject = "Support";
    $message = "Name: $name \n      Telephone Number:$tel \n Email address: $email\n Message: $detail";             
      
    $headers  = "From: $email\n";
    $success = mail($to, $subject, $message, $headers);
    if ($success) {
        $sent="<br/><font class=\"maintext\">Your e-mail has been sent.</font><br/><br/>";
            }
    else {
          $sent="<br/><font class=\"maintext\">An error occurred when sending your e-mail. <br/><br/>Please try again<br/> <br/>If the problem persists please e-mail us at: errors@anaddress.co.uk </font><br/>";
            $show="y";
            }
            }
            
            }//end e-mail sending
?>

 <?php      echo "<form action=\"support.php?support=yes\" method=\"post\">";?>
<table width="100%" align="center" bgcolor="#EEEEEE" cellpadding="5" class="mainborder">
           <tr>
                <td colspan="2">
                  <?php //error message output
                   echo"<font color=\"#FF0000\">";
                   echo"$noemail$notvalid$noinput$noname$nodetail";
                   echo"</font>";
                   echo"$sent";
                   if ($support=="" OR $show=="y"):
                  ?>
                </td>
              </tr>
                  
      <tr class="maintextgrey">
      <td align="right" valign="center"> &nbsp;&nbsp;<strong>Name</strong><font size="-3\"> *</font> </td>
      <td ><input type="text" name="name"size="30" maxsize="50"
            <?php
            if ($name!=""){
            echo"value=\"$name\"";}
            ?>
      </td>
                </tr>
             <tr class="maintextgrey">
                  <td align="right" valign="center"> &nbsp;&nbsp;<strong>E-mail Address</strong><font size="-3"> *</font> </td>
                  <td><input type="text" name="email"size="30" maxsize="50"
            <?php
            if ($email!=""){
            echo"value=\"$email\"";}
            ?>
      </td>
                </tr>
                <tr class="maintextgrey">
                  <td align="right" valign="center"> &nbsp;&nbsp;<strong>Telephone Number </strong></td>
                  <td><input type="text" name="tel"size="30" maxsize="50"
            <?php
            if ($tel!=""){
            echo"value=\"$tel\"";}
            ?>
      </td>
              </tr>
                <tr class="maintextgrey">
                       <td align="right" valign="top"><strong>Enquiry</strong><font size="-3"> *</font></td>
                   <td>      
                         <textarea cols=25 name="detail" rows=6><?php if ($detail!=""){echo"$detail";}?></textarea> <br/><font size="-3">* Fields Required</font><br/> <br/>
      <input type="submit" value="Submit">
                  </td>  
      </tr>
      </form>
      <?php
      endif;
      ?>
            </table>
Avatar of ahoffmann
ahoffmann
Flag of Germany image

use perl with CGI.pm,
then start converting your .php page as follows (steps to be improved):
  1. for each plain HTML code use perls print command
  2. import GET and POST parameters (see man CGI)
  3. anything inside <?php ?> can be used in perl verbatim
  4. replace php's echo by perl's print
test it by running from command line, should give roughly the same html page on STDOUT
Avatar of wjdashwood
wjdashwood

ASKER

does this mean in a perl script each peice of html must be coded as perl? If so what happens with the quotes? I know in PHP you must put a \ before each " is this the same with perl?

Also will the following function still work or does it have a different name in perl:

mail($to, $subject, $message, $headers);

are the $ still corect for variables and do the if statements remain the same?

Thanks,
H
>  each peice of html must be coded as perl?
yes.
perl is a programming language, while php is a web language (manly, not talking about its othee features)

> If so what happens with the quotes?
nothing, remain as is.

> .. PHP you must put a \ before each " is this the same with perl?
in the way PHP uses quotes, you can do the same in perl, don't need to change anything.

> Also will the following function still work or does it have a different name in perl:
well, that's something to change:
  all PHP functions need to be replaced by corresponding perl ones.
Luckily perl has very few functions, but huge amount of modules :-)
You need to find a module yourself, i.e. for mail there are multiple ones

> are the $ still corect for variables and do the if statements remain the same?
yes

i.g. php is pure (would not say dump) man's perl, it borrowed the syntax, but forgot to implement the glory things, unfortunatelly ...
for easier quoting, you can use this:

print <<"HTML";
<html>
<body>
</body>
HTML

Note: The Token "HTML" must start at the beginning of the line.
You might also use the following module (written by me).

package HTML::Perlfilter;

my $filtered;

use Filter::Simple sub
{
      return if $filtered++>0;
      
      my $html = 0;
      my $inhtml = 1;
      
      @_ = split (/\r\n/, $_);
      
      $_ = "print <<\"HTML$html\";\n";
      

      for my $line ( @_ )
      {
            if ( $line =~ /^\s*@(.*$)/ )
            {
                  $_ .= "print <<\"HTML$html\";\n" unless $inhtml;
                  $_ .= "$1\n";
                  $inhtml = 1;
            }
            else
            {
                  $_ .= "HTML$html\n", $html++ if $inhtml;
                  
                  $inhtml = 0;
                  
                  $_ .= "$line\n";
            }
            
      }
      
      $_ .= "HTML$html\n" if $inhtml;
};

1;

--------------------------------------------------------------------------------------


Example of usage (hello.cgi):
Call this like http://www.yourserver.com/scriptlocation/hello.cgi?user=myName

#!perl -w

use strict;
use HTML::Perlfilter;
use CGI;

my $q = new CGI;
my $user = $q->param("user");

@Content-Type: text/html
@
@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
@<html>
@      <head>
@            <meta name="author" content="Markus Holzer" />
@            <link href="/css/kv.css" rel="stylesheet" type="text/css" />
@            <title>Hello World sample</title>
@      </head>
@ <body>
@ <h1>Hello $user</h1>
@ </body>
@ </html>
thanks for that. i will have a go at converting it and let you know how it goes.
are you aware, that perl`s string comparison operator is "eq", not "=="?
dooh, "eq" vs. "==" in perl: missed that, thanks for pointing it out holli!

also, IMHO it's not a good idea to use sophisticated perl and modules in a first step of convertion, if someone is not used to perl (for example using $_ and @_ is dangereous, sometimes, somehow, you need to know before)
the module is not very sophisticated:-), anyway he can still fall back to the Token-Quoting.
for php, didn´t you mean poor/dumb man´s perl? ;-))
argh, hol[y|li] true: need to check my spelling :-)
wjdashwood,

how is it? did you reach an solution?
sorry, i am still working on it. I'm trying to overcome as many problems as I can on my own and I will let you know how far I get
So far I have this:
It was mentioned that eq replaces ==, what about != will that work?
I know that from function to end email else will not work. I'm going to come back to that in a bit. Other than that does it look ok to you guys? I have also forgotten how to test it. Can someone tell me how to do that. Thanks.


#!/usr/bin/perl

print "Content-type: text/html\n\n";

if ($support=="yes"){
      if($email eq ""AND $name eq "" AND $tel eq "" AND $detail eq ""){
     $noinput="<br/>Please make sure you complete the form before clicking the submit button<br/>";
     $show="y";
      }
      else{
             if($email eq ""){
        $noemail="<br/>You must enter an e-mail address<br/>";
        $show="y";
     }
     else{
        function EmailCheck($email)
        {
                $result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email );
        if ($result)
          return TRUE;
        else
         return FALSE;
                }

             $valid = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email );
     if (!$valid){
     $notvalid="<br/>The e-mail address you entered was not valid, please check it and try again<br/>";
     $show="y";
     }
     
     }#end e-mail else
     
     if($name eq ""){
     $noname="<br/>You must enter a name<br/>";
     $show="y";
     }
     
     if($detail eq ""){
       $nodetail="<br/>You must enter a message<br/>";
       $show="y";
     }
     
     }#end validation

     if ($show eq ""){
    $to = "Support@anaddress.co.uk";
    $subject = "Support";
    $message = "Name: $name \n     Telephone Number:$tel \n Email address: $email\n Message: $detail";          
     
    $headers  = "From: $email\n";
    $success = mail($to, $subject, $message, $headers);
    if ($success) {
        $sent="<br/><font class=\"maintext\">Your e-mail has been sent.</font><br/><br/>";
          }
    else {
         $sent="<br/><font class=\"maintext\">An error occurred when sending your e-mail. <br/><br/>Please try again<br/> <br/>If the problem persists please e-mail us at: errors@anaddress.co.uk </font><br/>";
          $show="y";
          }
          }
         
          }#end e-mail sending
print "     <form action=\"support.cgi?support=yes\" method=\"post\">";
print="<table width=\"100%\" align=\"center\" bgcolor=\"#EEEEEE\" cellpadding=\"5\" class=\"mainborder\">";
print="           <tr>";
print="                <td colspan=\"2\"> ";
print="                <font color=\"#FF0000\">";
print="                $noemail$notvalid$noinput$noname$nodetail";
print="                </font>";
print="                $sent";
               if ($support eq "" OR $show eq "y"){
print="                </td>";
print="              </tr>";
               
print="     <tr class=\"maintextgrey\"> ";
print="     <td align=\"right\" valign=\"center\"> &nbsp;&nbsp;<strong>Name</strong><font size=\"-3\"> *</font> </td>";
print="     <td ><input type=\"text\" name=\"name\"size=\"30\" maxsize=\"50\"";
 
          if ($name!=""){
print="          value=\"$name\"";
}

print="     </td>";
print="               </tr> ";
print="             <tr class=\"maintextgrey\"> ";
print="                 <td align=\"right\" valign=\"center\"> &nbsp;&nbsp;<strong>E-mail Address</strong><font size=\"-3\"> *</font> </td>";
print="                 <td><input type=\"text\" name=\"email\"size=\"30\" maxsize=\"50\" ";
          if ($email!=""){
print="          value=\"$email\"";
}
print="     </td>";
print="               </tr>";
print="               <tr class=\"maintextgrey\"> ";
print="                 <td align=\"right\" valign=\"center\"> &nbsp;&nbsp;<strong>Telephone Number </strong></td>";
print="                 <td><input type=\"text\ name=\"tel\"size=\"30\" maxsize=\"50\"";
                              if ($tel!=""){
print="          value=\"$tel\"";
}

print="     </td>";
print="             </tr>";
print="               <tr class=\"maintextgrey\"> ";
print="                     <td align=\"right\" valign=\"top\"><strong>Enquiry</strong><font size=\"-3\"> *</font></td>";
print="                  <td>     ";
print="                         <textarea cols=25 name=\"detail\" rows=6>if ($detail!=""){print"$detail";}print"</textarea> <br/><font size=\"-3\">* Fields Required</font><br/> <br/>";
print="     <input type=\"submit\" value=\"Submit\"> ";
print="                 </td>   ";
print="     </tr>";
print="     </form>";
     }
print="           </table> ";

thanks for the help
1)
'!=' for strings is 'ne',

2)
use 'and' instead of 'AND',

3)
use
$valid = $text =~ /regex/
instead of
$valid = ereg("regex", $text)

there are countless other changes too, i.g. most things in perl are build-in while you need a function in PHP (see ereg example)
ASKER CERTIFIED SOLUTION
Avatar of wjdashwood
wjdashwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
agreed with PAQ and refund
yep