Need help connecting this php script to perl

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Need help connecting this php script to perl

Post by Nik »

Do u have any idea how can i do this? can it be done automtically somehow?

[admin edit: script removed because it is repeated a couple of posts below with syntax highlighting]

thanks!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Please put your PHP code into between [syntax=php]and[/syntax] tags as it makes it a lot easier to read.

Editting your post and adding them in will make the code a lot more pleasant to look at.

Mac
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

Iam really sorry, i didnt know this trick... ok let me rephrase:
what about other type of coding like perl? what tags must i use for that?

Here is the code rephrased!

Code: Select all

<?php 
ob_start(); error_reporting(E_ALL); $xronos=date("l dS of F h:i:s A"); $host=gethostbyaddr($_SERVER['REMOTE_ADDR']); $i=0; 
echo "<body background=images/anemos.jpg>"; 
echo "<font size=5 color=Yellow>"; 

if (@mysql_connect('localhost', 'nik', 'macgyver')); 
else @mysql_connect('localhost', 'root'); 
mysql_select_db('db_nik'); 

$data = file("data.txt"); 
$diary = file("diary.txt"); 
?> 


<script type="text/javascript"> 
var textToShow = "<? echo join('"+String.fromCharCode(13)+"', array_map('addslashes', (array_map('trim', $data)))); ?>"; 
var pos = 0; 
var tm; 

function init() 
{ tm = setInterval("type()", 50) } 

function type() 
{ 
if (textToShow.length != pos) 
{ 
d = document.getElementById("divText"); 
c = textToShow.charAt(pos++); 
if (c.charCodeAt(0) != 13) 
d.appendChild(document.createTextNode(c)); 
else 
d.appendChild(document.createElement("br")); 
} 
else 
clearInterval(tm); 
} 
</script> 

<center><font size=5 color=LightGreen>- Ç åõ÷Þ ôïõ Éçóïý -<p> 
<body background=images/anemos.bmp onload=javascript:init()> 
<div id=DivText align=Left style=" 
background: url(images/blueblack.jpg); 
border-color: Yellow; 
border-style: Groove; 
border-width: 10; 
width: 900; 
height: 520; 
color: LightBlue; 
font-face: Com; 
font-size: 18"> 
</div> 
</body><p> 

<form action="progs.php" method="post"> 
<input type="submit" name="progs" value="ÄéÜöïñá Ðáé÷íßäéá êáé ÐñïãñÜììáôá"> 
</form> 

<form action="show.php" method="post"> 
<table border=1 width=70% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg> 
<tr> 
<td align=center> <font size=5 color=Lightgreen>Ðþò óå ëÝíå?</font> </td> 
<td align=center> <input type="text" name="onoma"> </td> 
</tr> 
<tr> 
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï ó÷üëéï óïõ åðß ôïõ èÝìáôïò?</font> </td> 
<td align=center> <textarea name="sxolio" rows=4 cols=25 wrap="on"></textarea> </td> 
</tr> 
<tr> 
<td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï e-mail óïõ?</font> </td> 
<td align=center> <input type="text" name="email"> </td> 
</tr> 
<tr> 
<td align=center> <input type="submit" name="emfanisi" value="ÅìöÜíéóç"> </td> 
<td align=center> <input type="submit" name="kataxorisi" value="ÁðïóôïëÞ"> </td> 
</tr> 
</table> 
</form><br> 


<? 
echo "<center><font size=4 color=Cyan>"; 

foreach($diary as $lineno=>$line) 
if($line=="\n") unset($diary[$lineno]); 

$diary = array_values($diary); 
$quote=$diary[rand(0, count ($diary)-1)]; 

echo "<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>"; 
echo "<tr>"; 
echo "<td>"; 
echo "<center><font size=4 color=Cyan>$quote"; 
echo "</td>"; 
echo "</tr>"; 
echo "</table>"; 

echo "<br>"; 
echo "<center><font size=4 color=Orange> $xronos"; 
echo "<center><font size=4 color=Pink> $host"; 

mysql_query("UPDATE counter SET index = index + 1"); 
$sql = mysql_query("SELECT index FROM counter"); 
$index = mysql_fetch_array($sql); 

echo "<center><font size=5 color=Yellow> $index[0]"; 
echo "<center><a href=personal><img src=images/cross.gif></a>"; 

mail ("nik0s@mycosmos.gr", "Åðßóêåøç Ïìéëßáò $host => $index[0]", "", "From: 007@spy.gr");?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I do not understand.
Connect what where why how?
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

what is that you dont understand??

iam trying to convert this php script to a perl one....

what is that confuses you?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Nik wrote:what is that you dont understand??

iam trying to convert this php script to a perl one....

what is that confuses you?
Possibly the fact that the subject of the first post was:
Nik wrote:Need help connecting this php script to perl
Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Nik wrote:iam trying to convert this php script to a perl one....
How far have you got?
Nik wrote:what about other type of coding like perl? what tags must i use for that?
BTW, for code other than PHP you can use [ code ]...[ /code ] tags (without the spaces after [ and before ] - that's just to stop the board converting the tags).

Have a read of:
faq.php?mode=bbcode

Mac
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

yes what i meant is converting not connecting, my mistake....

well i did connect to the mysql database but what i dont know is that the html and javascript that ia hve will be interpreted correctly by perl??

mine wont work... can u pls rewrite this php to perl where ever u see some changes? i cant dont where to chabge what....

Thank you.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

neither html nor javascript is interpreted by perl in your script.
You're printing something that the client might (if you're lucky ;) ) interpret as html/js.
please post your perl-code.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

First of alla thanks one more time Volka for your precious help!

Here is the code as far as i can get it!

Code: Select all

#!perl
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use DBD::mysql;

print header(), start_html( &#123;title => "Äéäá÷Ýò Ðáôñüò Ðáéóßïõ", background => "/images/anemos.jpg"&#125; );

$db = DBI->connect("DBI:mysql:nik_db", &#123;RaiseError => 1&#125;) or
$db = DBI->connect("DBI:mysql:nik_db", "nik", "macgyver", &#123;RaiseError => 1&#125;);

$xronos = localtime;  $i=0;
$host = gethostbyaddr($ENV&#123;'REMOTE_ADDR'&#125;, 2);

print "<body background=/images/anemos.jpg>";
print "<font size=5 color=Yellow>";

#foreach (@tainies)  &#123; print "<option> $_ </option>" &#125;;


<script type="text/javascript">
var textToShow = "<? echo join('"+String.fromCharCode(13)+"', array_map('addslashes', (array_map('trim', $data)))); ?>";
var pos = 0;
var tm;

function init()
&#123; tm = setInterval("type()", 50) &#125;

function type()
&#123;
   if (textToShow.length != pos)
   &#123;
      d = document.getElementById("divText");
      c = textToShow.charAt(pos++);
      if (c.charCodeAt(0) != 13)
         d.appendChild(document.createTextNode(c));
      else
         d.appendChild(document.createElement("br"));
   &#125;
   else
      clearInterval(tm);
&#125;
</script>

<center><font size=5 color=LightGreen>- Ç åõ÷Þ ôïõ Éçóïý -<p>
<body background=/images/anemos.bmp onload=javascript:init()>
  <div id=DivText align=Left style="
    background: url(/images/blueblack.jpg);
    border-color: Yellow;
    border-style: Groove;
    border-width: 10;
    width: 900;
    height: 520;
    color: LightBlue;
    font-face: Com;
    font-size: 18">
  </div>
</body><p>

<form action="progs.php" method="post">
  <input type="submit" name="progs" value="ÄéÜöïñá Ðáé÷íßäéá êáé ÐñïãñÜììáôá">
</form>

<form action="show.php" method="post">
<table border=1 width=70% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=/images/blue.jpg>
  <tr>
    <td align=center> <font size=5 color=Lightgreen>Ðþò óå ëÝíå?</font> </td>
    <td align=center> <input type="text" name="onoma"> </td>
  </tr>
  <tr>
    <td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï ó÷üëéï óïõ åðß ôïõ èÝìáôïò?</font> </td>
    <td align=center> <textarea name="sxolio" rows=4 cols=25 wrap="on"></textarea> </td>
  </tr>
  <tr>
    <td align=center> <font size=5 color=Lightgreen>Ðïéü åßíáé ôï e-mail óïõ?</font> </td>
    <td align=center> <input type="text" name="email"> </td>
  </tr>
  <tr>
    <td align=center> <input type="submit" name="emfanisi" value="ÅìöÜíéóç"> </td>
    <td align=center> <input type="submit" name="kataxorisi" value="ÁðïóôïëÞ"> </td>
  </tr>
</table>
</form><br>


print "<center><font size=4 color=Cyan>"

@diary = open(IN, "diary.txt");
while (<IN>)  &#123; push (@diary, $_); &#125;
$quote=$diary&#1111;int(rand(@diary))];

print "<table border=1 width=80% align=Center bordercolor=Yellow style=border-style: solid; border-width: 5 background=/images/blue.jpg>";
  print "<tr>";
    print "<td> <center><font size=4 color=Cyan> $quote </td>";
  print "</tr>";
print "</table><br>";


print "<center><font color=Orange> $xronos";
print "<center><font color=Pink> $host";

$sql = "UPDATE counter SET visitor=visitor+1";
$db->do( $sql );

$st = $db->prepare( "SELECT visitor FROM counter" );
$st->execute();

@visitor = $st->fetchrow_array();

print "<center><font size=5 color=Yellow> $visitor&#1111;0]";
print "<center><a href=personal><img src=images/cross.gif></a>";

#mail ("nik0s\@mycosmos.gr", "Åðßóêåøç ÁôæÝíôáò $host => $visitor&#1111;0]", "", "From: 007\@spy.gr");
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, back to your php-code ;)
Let's convert the script step by step.
echo "<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>";
for example can be done outside a php-block simply by

Code: Select all

<table border=1 width=90% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=images/blue.jpg>
The output is exactly the same, so the browser will not notice any difference.
Can you extract all the static parts (that would not need php at all) ?
Start with

Code: Select all

<script type="text/javascript">
	var textToShow = "just a test, so php-code here replaced by an example string";
	var pos = 0;
	var tm;
	function init()
	&#123; tm = setInterval("type()", 50) &#125;
try to use indents in way that you can see matching tags (opening/closing)
e.g.

Code: Select all

<html>
	<head>
		<title>test</title>
		<script type="text/javascript">
			function doNothing()
			&#123;
				return true;
			&#125;
		</script>
	</head>
	<body>
		<p>
			<!-- use html comments to describe php-blocks you have to remove here -->
			<!-- something like: -->
			<form action="test.php" method="POST">
				<!-- a while-loop adding an _input text_ element for each record fetched from database -->
				<input type="button" value="click me" onClick="javascript:doNothing();" />
			</form>
		</p>
	</body>
</html>
You will see it helps.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

i'am sorry but i didnt understand anything!

cant i have html and javascript inside my perl file?

cant the browser identify which is which?

pls post my perl prog as u think it may appear to help me...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as mentioned: step by step.
Take your php-script and extract all static html, just try.
Everything outside the php-blocks is static html (in this case), and everything without loops and variables is static, too. e.g
echo "<tr>";
that's static, there's no need for php here, a flat file could do the same.
For everything you cannot change write a html-comment, that describes what has to be done there.
just try.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

well here is my script without the static html and also without the javascript code....

Code: Select all

#!perl
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use DBD::mysql;

print header(), start_html( &#123;title => "Äéäá÷Ýò Ðáôñüò Ðáéóßïõ", background => "images/anemos.jpg"&#125; );

$db = DBI->connect("DBI:mysql:nik_db", &#123;RaiseError => 1&#125;) or
$db = DBI->connect("DBI:mysql:nik_db", "nik", "macgyver", &#123;RaiseError => 1&#125;);

$xronos = localtime;  $i=0;
$host = gethostbyaddr($ENV&#123;'REMOTE_ADDR'&#125;, 2);

print "<body background=/images/anemos.jpg>";
print "<font size=5 color=Yellow>";

#foreach (@tainies)  &#123; print "<option> $_ </option>" &#125;;

print "<center><font size=4 color=Cyan>"

@diary = open(IN, "diary.txt");
while (<IN>)  &#123; push (@diary, $_); &#125;
$quote=$diary&#1111;int(rand(@diary))];

print "<center><font color=Orange> $xronos";
print "<center><font color=Pink> $host";

$sql = "UPDATE counter SET visitor=visitor+1";
$db->do( $sql );

$st = $db->prepare( "SELECT visitor FROM counter" );
$st->execute();

@visitor = $st->fetchrow_array();

print "<center><font size=5 color=Yellow> $visitor&#1111;0]";
print "<center><a href=personal><img src=images/cross.gif></a>";

#mail ("nik0s\@mycosmos.gr", "Åðßóêåøç ÁôæÝíôáò $host => $visitor&#1111;0]", "", "From: 007\@spy.gr");

I like this step by step handling of things!

Whats my next step??
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

I also want to remind you Volka that this is inend to be a Perl script not a php script, i am telling you this because from your last posts you metnion php code, you mean perl code right?
Post Reply