Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Convert perl over to php

    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.

    PHP Code:
    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
    Last edited by sonny; 03-26-2010 at 08:46 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    It pretty much all has to be changed I'm afraid. Perl is kinda similar to PHP, but they are completely different languages.
    PHP Code:
    $n 0;
    $fh = @fopen('count.txt''r+') or die('Error in opening file for read & write');
    $n fread($fh);
    if (
    $n 3)
    {
        
    ftruncate($fh0);
        @
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    It pretty much all has to be changed I'm afraid. Perl is kinda similar to PHP, but they are completely different languages.
    PHP Code:
    $n 0;
    $fh = @fopen('count.txt''r+') or die('Error in opening file for read & write');
    $n fread($fh);
    if (
    $n 3)
    {
        
    ftruncate($fh0);
        @
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    fseek($fh0); 
    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:
    PHP Code:
    $n = (int)fread($fhfilesize('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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    just change
    PHP Code:
    $n fread($fh); 
    to
    PHP Code:
    $n fread($fhfilesize("count.txt")); 
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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:
    PHP Code:
    fseek($fh0); 
    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:
    PHP Code:
    $n = (int)fread($fhfilesize('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

  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php

    $n 
    0;
    $fh = @fopen('count.txt''r+') or die('Error in opening file for read & write');
    $n fread($fhfilesize("count.txt"));
    if (
    $n 3)
    {
        
    ftruncate($fh0);
        @
    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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Replace the ftruncate call with fseek($fh, 0);
    Post back.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can you post you're entire code please?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Can you post you're entire code please?
    PHP Code:
    <?php

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

    print 
    "OK do it\n"

    ?>

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    <?php 

    $n 
    0
    $fh = @fopen('count.txt''r+') or die('Error in opening file for read & write'); 
    printf("\$n = %d, location: %d\n"$nftell($fh));
    $n fread($fhfilesize("count.txt")); 
    printf("\$n = %d, location: %d\n"$nftell($fh));
    if (
    $n 3

        
    fseek($fh0); 
        
    printf("\$n = %d, location: %d\n"$nftell($fh));
        @
    fwrite($fh, ++$n) or die ('Unable to write to file!'); 
        
    printf("\$n = %d, location: %d\n"$nftell($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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    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
    Last edited by sonny; 03-26-2010 at 10:34 PM.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #15
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •