SillyMonk has asked for the wisdom of the Perl Monks concerning the following question:

OK here is the script Im working on:
#!/usr/bin/perl print "Content-type: text/html\n\n"; open(flashcount, "/var/www/html/log/flashcount.txt"); $count=<flashcount>; $outcome = $count+1; print $outcome; close(flashcount); open(flashcount,">/var/www/html/log/flashcount.txt"); print flashcount $outcome ; print "</BODY>\n"; print "</HTML>";
The problem is at the moment when you goto the text file it just has the number value stored such as 5,7,29 whatever. I need it to say count=5,7,29 for instance I have tried several different ways of doing this but they all result in an error page, I tried
print flashcount "count="$outcome ; (error) print flashcount "count" $outcome ; (error)
I've played about with lots of different ways, just doesnt seem to be working, Any help or advice would be greatly appreciated. Thanks from SillyMonk, James

Replies are listed 'Best First'.
Re: Writing to a New File
by toolic (Bishop) on Jan 28, 2008 at 20:03 UTC
    If the 1st line of your flashcount.txt file contains "5", then the following code:
    #!/usr/bin/env perl use warnings; use strict; my $file = './flashcount.txt'; open my $fh, '<', $file or die "Can not open file $file: !$\n"; my $count = <$fh>; close $fh or die "Can not close file $file: !$\n"; chomp $count; my $outcome = $count + 1; print "outcome = $outcome\n";

    prints (Update):

    outcome = 6

    Another update: added chomp.

      Your way does look impressive toolic and i've saved that code, it'll probably come in handy very shortly, But I've seemed to have fixed it in a strange way indeed.
      print flashcount $outcome, "count=".$outcome;
      this code seems to allow the value to continue incrementing. the the result i get in the txt file is 5count=5 I know thats not so pretty but flash will be able to pick up the value from the txt file Strange I know, Thanks again TheZip, Bradcathey and Toolic!
Re: Writing to a New File
by almut (Canon) on Jan 28, 2008 at 20:34 UTC

    In order to have it properly interpreted as a number, you need to remove the leading "count=" part after having read the line from the file... — something like this

    my $flashcount_file = "/var/www/html/log/flashcount.txt"; open my $flashcount, "<", $flashcount_file; my $count = <$flashcount>; $count =~ s/^count=//; # <-- remove "count=" $count++; close $flashcount; open my $flashcount, ">", $flashcount_file; print $flashcount "count=$count"; close $flashcount;

      Oh how many times have I seen this code crap out!

      You have a race condition. flock the file while you're working on it. In order to maintain the lock, you won't be able to close and reopen the file, so you'll have to open the file using in read-write mode and use seek.

      use Fcntl qw( LOCK_EX SEEK_SET ); my $flashcount_file = '/var/www/html/log/flashcount.txt'; open(my $flashcount, '+<', $flashcount_file) or die; flock($flashcount, LOCK_EX) or die; my $count = <$flashcount>; $count =~ s/^count=//; $count++; seek($flashcount, 0, SEEK_SET) or die; # Not needed in this case. truncate($flastcount, 0) or die; print $flashcount "count=$count\n";

      Untested.

      Note: The code assumes the file already exists.

      Update: Added code.

Re: Writing to a New File
by GrandFather (Saint) on Jan 28, 2008 at 21:52 UTC

    The following code updates the count 'in place' using a regex. If the regex fails an initial count is generated.

    use strict; use warnings; open FLASHCOUNT, '<', '/var/www/html/log/FLASHCOUNT.txt'; my $line = <FLASHCOUNT> || ''; close(FLASHCOUNT); $line = 'count = 1' unless $line =~ s/(\d+)/$1 + 1/e; open FLASHCOUNT, '>', '/var/www/html/log/FLASHCOUNT.txt'; print FLASHCOUNT $line; close FLASHCOUNT; print "Content-type: text/html\n\n"; print $line; print "</BODY>\n"; print "</HTML>";

    Update: note ikegami's comment re race conditions.


    Perl is environmentally friendly - it saves trees
Re: Writing to a New File
by bradcathey (Prior) on Jan 28, 2008 at 19:59 UTC

    try:

    print flashcount "count=".$outcome; print flashcount "count=$outcome";
    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      print flashcount "count=".$outcome; print flashcount "count=$outcome";
      Both these codes added count= to the text file perfectly, except the value isn't increasing any ?