Solved

Newbie - Convert working php code to perl

Posted on 2004-08-11
20
318 Views
Last Modified: 2011-09-20
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>
0
Comment
Question by:wjdashwood
  • 7
  • 6
  • 5
20 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11782277
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
0
 

Author Comment

by:wjdashwood
ID: 11782937
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11786178
>  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 ...
0
 
LVL 6

Expert Comment

by:holli
ID: 11791499
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.
0
 
LVL 6

Expert Comment

by:holli
ID: 11791524
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>
0
 

Author Comment

by:wjdashwood
ID: 11791533
thanks for that. i will have a go at converting it and let you know how it goes.
0
 
LVL 6

Expert Comment

by:holli
ID: 11791566
are you aware, that perl`s string comparison operator is "eq", not "=="?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11791610
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)
0
 
LVL 6

Expert Comment

by:holli
ID: 11791683
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? ;-))
0
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.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 11791952
argh, hol[y|li] true: need to check my spelling :-)
0
 
LVL 6

Expert Comment

by:holli
ID: 11832051
wjdashwood,

how is it? did you reach an solution?
0
 

Author Comment

by:wjdashwood
ID: 11841371
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
0
 

Author Comment

by:wjdashwood
ID: 11841615
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
0
 
LVL 6

Expert Comment

by:holli
ID: 11842281
1)
'!=' for strings is 'ne',

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

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

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11842636
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)
0
 

Accepted Solution

by:
wjdashwood earned 0 total points
ID: 11851771
thanks for all of your help, but this really is a bit beyond me and it seems silly to spend all this time converting some code that works perfectly well. I am changing my hosting to one that has php installed.
Thanks again for your help.
I will award point equally to those who have helped me, but I will have to award a grade C because the question is not completed, but I can see how it could help others. Please let me know if you would like the points. Those who have replied within the next couple of days will get the points shared between them.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11855119
agreed with PAQ and refund
0
 
LVL 6

Expert Comment

by:holli
ID: 11867678
yep
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

12 Experts available now in Live!

Get 1:1 Help Now