...

View Full Version : Convert perl over to php



sonny
03-26-2010, 04:45 PM
Hi I am trying to covert my site scripts over to php
can someone show what lines need to be changed
in below perl code to work on php page? it gives
syntax errors.



my $n = 0;
open (FH, "<", 'count.txt') or die "$!";
$n = <FH>;
close FH;
if ($n < 3) {
open (FH, ">", 'count.txt') or die "$!";
print FH ++$n;
close FH;
}
else {
print "Limit reached\n";
exit(0);
}



Thanks
Sonny

Fou-Lu
03-26-2010, 05:21 PM
It pretty much all has to be changed I'm afraid. Perl is kinda similar to PHP, but they are completely different languages.


$n = 0;
$fh = @fopen('count.txt', 'r+') or die('Error in opening file for read & write');
$n = fread($fh);
if ($n < 3)
{
ftruncate($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
}
else
{
print "Limit Reached\n";
}
fclose($fh);


Thats the closest direct conversion from the looks of it.

PHP has 'short cut' ways of dealing with this though. file_get_contents() will return all the contents as a string (if you're just using numbers in the file, it will convert it just like perl of course), and file_put_contents() to write data to a file. So you can take this down quite a bit as well.

sonny
03-26-2010, 07:48 PM
It pretty much all has to be changed I'm afraid. Perl is kinda similar to PHP, but they are completely different languages.


$n = 0;
$fh = @fopen('count.txt', 'r+') or die('Error in opening file for read & write');
$n = fread($fh);
if ($n < 3)
{
ftruncate($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
}
else
{
print "Limit Reached\n";
}
fclose($fh);


Thats the closest direct conversion from the looks of it.

PHP has 'short cut' ways of dealing with this though. file_get_contents() will return all the contents as a string (if you're just using numbers in the file, it will convert it just like perl of course), and file_put_contents() to write data to a file. So you can take this down quite a bit as well.

That gives Warning: Wrong parameter count for fread()
and does not write number for some reason

Fou-Lu
03-26-2010, 07:59 PM
Right, sorry fread needs a size to read. Just do that lazy with $n = fread($fh, filesize('count.txt'));. That should fix that problem.

Lets take a looksee at the ftruncate to see how it works. Perhaps its not as I assumed....
Nope it doesn't. Remove the ftruncate and use this:


fseek($fh, 0);

PHP's fseek third parameter is from whence, which is seek set by default, so always from the start of the file. If you have problems with number joins at any point, after you're fwrite you can then call ftruncate and give it the length of the size indicated by ftell.
My original assumption was the ftruncate would truncate TO the 0 bytes in size. The only thing I'll suggest is to immediately cast this to an integer:


$n = (int)fread($fh, filesize('count.txt'));


Or to look at the pack method. Aside from this, you may use a secondary file resource for writing, or look at using the file_get_contents and file_put_contents to get around the seek.

I'm afraid I can't test ATM, so please give that a shot and post back the results.

DJCMBear
03-26-2010, 08:03 PM
just change


$n = fread($fh);

to


$n = fread($fh, filesize("count.txt"));

sonny
03-26-2010, 08:04 PM
Right, sorry fread needs a size to read. Just do that lazy with $n = fread($fh, filesize('count.txt'));. That should fix that problem.

Lets take a looksee at the ftruncate to see how it works. Perhaps its not as I assumed....
Nope it doesn't. Remove the ftruncate and use this:


fseek($fh, 0);

PHP's fseek third parameter is from whence, which is seek set by default, so always from the start of the file. If you have problems with number joins at any point, after you're fwrite you can then call ftruncate and give it the length of the size indicated by ftell.
My original assumption was the ftruncate would truncate TO the 0 bytes in size. The only thing I'll suggest is to immediately cast this to an integer:


$n = (int)fread($fh, filesize('count.txt'));


Or to look at the pack method. Aside from this, you may use a secondary file resource for writing, or look at using the file_get_contents and file_put_contents to get around the seek.

I'm afraid I can't test ATM, so please give that a shot and post back the results.

Yes I did add $n = fread($fh,filesize('count.txt')); before
it counts but puts a space in front of the number every time it updates

example looks like this in the count file
1
__2
___3 etc

That's odd

sonny
03-26-2010, 08:17 PM
<?php

$n = 0;
$fh = @fopen('count.txt', 'r+') or die('Error in opening file for read & write');
$n = fread($fh, filesize("count.txt"));
if ($n < 3)
{
ftruncate($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
}
else
{
print "Limit Reached\n";
}
fclose($fh);

print "OK do it\n";

?>


This is odd, check it out, every time the number is updated in the count.txt file a space is added before the number making it longer and longer

Fou-Lu
03-26-2010, 08:44 PM
Replace the ftruncate call with fseek($fh, 0);
Post back.

sonny
03-26-2010, 09:01 PM
Replace the ftruncate call with fseek($fh, 0);
Post back.

I replaced
ftruncate($fh, 0);

With
fseek($fh, 0);

no errors but the log does not write
at all

Fou-Lu
03-26-2010, 09:03 PM
Can you post you're entire code please?

sonny
03-26-2010, 09:06 PM
Can you post you're entire code please?



<?php

$n = 0;
$fh = @fopen('count.txt', 'r+') or die('Error in opening file for read & write');
$n = fread($fh, filesize("count.txt"));
if ($n < 3)
{
fseek($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
}
else
{
print "Limit Reached\n";
}
fclose($fh);

print "OK do it\n";

?>

Fou-Lu
03-26-2010, 09:15 PM
<?php

$n = 0;
$fh = @fopen('count.txt', 'r+') or die('Error in opening file for read & write');
printf("\$n = %d, location: %d\n", $n, ftell($fh));
$n = fread($fh, filesize("count.txt"));
printf("\$n = %d, location: %d\n", $n, ftell($fh));
if ($n < 3)
{
fseek($fh, 0);
printf("\$n = %d, location: %d\n", $n, ftell($fh));
@fwrite($fh, ++$n) or die ('Unable to write to file!');
printf("\$n = %d, location: %d\n", $n, ftell($fh));
}
else
{
print "Limit Reached\n";
}
fclose($fh);

print "OK do it\n";

?>


If you run that and get this as a result (view source code if necessary to see the line breaks):
$n = 0, location: 0
$n = ?, location: 4 // ? is whatever the number is in you're text file
$n = ?, location: 0 // ? is whatever the number is in you're text file
$n = ? + 1, location: 4 // ? is whatever the number is in you're text file

Then it should be working. If it shows:
$n = 0, location: 0
$n = ?, location: 4 // ? is whatever the number is in you're text file
$n = ?, location: 4 // ? is whatever the number is in you're text file
$n = ? + 1, location: 8 // ? is whatever the number is in you're text file

Then the problem is that fseek's API is lying and it uses a SEEK_CUR and not a SEEK_SET. If thats the case, add another argument to fseek that uses SEEK_SET as its value.

OR we can look at using file_get_contents and file_put_contents. Technically, file_get_contents is better on these small files for speed, but I'd not use it on any sort of large file.

sonny
03-26-2010, 09:31 PM
when I call the page, this is what I get

$n = 0, location: 0 $n = 0, location: 66560 $n = 0, location: 0 $n = 0, location: 66560 OK do it


The source code of the page looks like this
$n = 0, location: 0
$n = 0, location: 66560
$n = 0, location: 0
$n = 0, location: 66560
OK do it

Fou-Lu
03-26-2010, 09:41 PM
Thats not right, that says you're file is reading 66560 bytes into you're $n. Hense why it lists as 0 in this situation.
Windows system by chance? Change the open mode to binary for starters: $fh = @fopen('count.txt', 'r+b') or die('Error in opening file for read & write'); to see if that helps.
With those unusual locations on the file pointer (which also explains the problem with $n since its likely a string. A big one.), I would delete the file completely and let PHP make it for you.

Try those. First thing I'd do is delete the file first. If that still doesn't work try binary mode (though that shouldn't really matter either; normally it auto-detects the best choice).

sonny
03-26-2010, 09:52 PM
Thats not right, that says you're file is reading 66560 bytes into you're $n. Hense why it lists as 0 in this situation.
Windows system by chance? Change the open mode to binary for starters: $fh = @fopen('count.txt', 'r+b') or die('Error in opening file for read & write'); to see if that helps.
With those unusual locations on the file pointer (which also explains the problem with $n since its likely a string. A big one.), I would delete the file completely and let PHP make it for you.

Try those. First thing I'd do is delete the file first. If that still doesn't work try binary mode (though that shouldn't really matter either; normally it auto-detects the best choice).

Its all white space in the file I just noticed that, that other code did write but put a white space on top of a white space every time it updated the count.

Fou-Lu
03-26-2010, 10:10 PM
Its all white space in the file I just noticed that, that other code did write but put a white space on top of a white space every time it updated the count.

Yeah, thats what I figured from the ftell there. Just go ahead and delete the count.txt file; the + modes on the file open will always attempt to recreate the file.
Then just make sure you're read is cast to an int:


$n = (int)fread($fh, filesize("count.txt"));

And the increment will allow it to hit a 1 for the next write (pretending it was originally zero essentially), and it should all work fine from there.

sonny
03-26-2010, 10:32 PM
Let me start off by saying, You are one of the nicest and most caring people
I have yet to met online, you could have easily walked the other way, but
you did not, you stayed with me on this to the end.

This is working perfect




<?php

$n = 0;
$fh = @fopen('count.txt', 'r+b') or die('Error in opening file for read & write');

$n = fread($fh, filesize("count.txt"));
if ($n < 5)
{
fseek($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
print "OK do it\n";

}
else
{
print "Limit Reached\n";
}
fclose($fh);

?>

Fou-Lu
03-26-2010, 11:10 PM
Let me start off by saying, You are one of the nicest and most caring people
I have yet to met online, you could have easily walked the other way, but
you did not, you stayed with me on this to the end.

This is working perfect




<?php

$n = 0;
$fh = @fopen('count.txt', 'r+b') or die('Error in opening file for read & write');

$n = fread($fh, filesize("count.txt"));
if ($n < 5)
{
fseek($fh, 0);
@fwrite($fh, ++$n) or die ('Unable to write to file!');
print "OK do it\n";

}
else
{
print "Limit Reached\n";
}
fclose($fh);

?>


Great, glad that worked out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum