file

i have a filr test.txt

cat test.txt
3

it only contains a integer value....i want to increment the value

is it possible to do it whithout opening the file, then reading the current value, then open in write mode.

is it possible to do in just 1 step

Rahul
LVL 1
shahrahulbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
use File::CounterFile;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shahrahulbAuthor Commented:
hahaha that is the problem i discussed in my previous question
#       my $d = CounterFile->new("$CONFIG::DATA/$file");
#       $d->inc;

when multiplt users hit the url at same time, i get the error
Software error:
Can't flock: No record locks available at /v/test.cgi line 40      and line 40 contains my $d = CounterFile->new("$CONFIG::DATA/$file");

so i m creating my own counter logic


if u have any alternative solution i m open
ozoCommented:
Maybe you can try appending a line to the file to increment, and counting the number of lines to read the counter.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

ZiaTioNCommented:
You could use the filename itself as the integer value and increment that.  However if this were the case the filename would change everytime and therefore you would need to perform a readdir every time and look for your text file and then increment and recreate the new file by opeing it anyway and then closing it and then remove the old file.

__________________________________
#!/usr/bin/perl -w

use strict;

opendir (DIR, '.') ||
   die "Unable to read current directory: ($!)\n";
my @files = grep {/\d+\.txt/} readdir (DIR);
closedir(DIR);

die "Multiple files found!\n" unless (scalar @files < 2);

my $newfile = $files[0] || 0;
$newfile =~ s/\.txt//;
$newfile++;
$newfile .= '.txt';
open(NEW, ">", $newfile) ||
   die "Unable to create $newfile: ($!)\n";
close(NEW);
unlink($files[0]) if ($files[0]);
_____________________________
ozoCommented:
Is there a database server accessible from all the machines that want to use the common counter?
brado1234Commented:
You do not need perl to increment the number in the file.  You can simply do it through the command line.. or a simple shell script....

cat test.txt | awk '{print $1+1}' > test.txt

This will take the number inside of the file "test.txt" and will increment it, and then write it back to the file "test.txt".

Is this what your looking for???

brado1234
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.