Re: the manuals fail me
by jwkrahn (Abbot) on Mar 28, 2012 at 21:17 UTC
|
$index = `cat /tmp/myfile.index`;
$index = $index + 1;
system "echo $index > /tmp/myfile.index";
exit 0;
| [reply] [d/l] [select] |
Re: the manuals fail me
by ikegami (Patriarch) on Mar 29, 2012 at 01:11 UTC
|
use Fcntl qw( LOCK_EX O_RDWR O_CREAT SEEK_SET );
my $qfn = '/tmp/myfile.index';
sysopen(my $fh, $qfn, O_RDWR|O_CREAT, 0666)
or die("Can't create or open \"$qfn\": $!\n");
flock($fh, LOCK_EX);
my $index = <$fh>;
++$index;
seek($fh, 0, SEEK_SET);
print($fh "$index\n");
#truncate($fh, tell($fh));
You could also do it without locking:
my $qfn = '/tmp/myfile.index';
open(my $fh, '<', $qfn)
or die("Can't open \"$qfn\": $!\n");
my $index = <$fh>;
++$index;
open($fh, '>', $qfn)
or die("Can't create \"$qfn\": $!\n");
print($fh "$index\n");
| [reply] [d/l] [select] |
|
The locking method appeals to me. Could you tell me what LOCK_EX should do, and which version of fcntl you use with it? My manpage does not mention it, and google is not helpful.
| [reply] |
|
...and Perl only provides one flock, so no idea what "version of fcntl" means.
| [reply] [d/l] |
|
LOCK_EX is an exlusive lock. See man flock(2)
| [reply] |
Re: the manuals fail me
by jwkrahn (Abbot) on Mar 28, 2012 at 21:51 UTC
|
use Tie::File;
my $filename = '/tmp/myfile.index';
tie my @index, 'Tie::File', $filename or die "Cannot open '$filename'
+because: $!";
++$index[ 0 ];
untie @index;
| [reply] [d/l] |
Re: the manuals fail me
by JavaFan (Canon) on Mar 28, 2012 at 21:21 UTC
|
use autodie;
use Fcntl qw[:DEFAULT :seek];
sysopen my $fh, "/tmp/text.index", O_RDWR | O_CREATE, 0666;
my $count = <$fh> || 0;
seek $fh, 0, SEEK_SET;
truncate $fh;
print $fh $count + 1;
close $fh;
Or use a precanned CPAN solution. | [reply] [d/l] [select] |
Re: the manuals fail me
by JavaFan (Canon) on Mar 28, 2012 at 21:26 UTC
|
echo $((`cat /tmp/myfile.index 2> /dev/null` + 1)) > /tmp/myfile.index
| [reply] [d/l] |
|
That's a nifty but subtle and tricky solution. Normally, when a shell command ends with redirection to a file like that, and the same file name is also used as input at a previous step in the same command line, the file gets truncated first (the redirect/truncation is processed before the preceding steps are launched), so the the earlier input step ends up reading an empty file.
My mind boggles at how much of the bash man page must be studied to understand why your command line works the way it does, executing the double-paren'd chunk before truncating the output file. And I feel sorry for anyone who looks at that line, concludes that it's a simple and normal thing to use the same file as input and redirection output in a single command, and forgets about the parens.
| [reply] |
Re: the manuals fail me
by rovf (Priest) on Mar 29, 2012 at 10:31 UTC
|
Since you seem to be on Unix/Linux or compatible:
perl -pi -e '++$_' /tmp/myfile.index
--
Ronald Fischer <ynnor@mm.st>
| [reply] [d/l] |
Re: the manuals fail me
by Util (Priest) on Mar 28, 2012 at 21:59 UTC
|
I think it is clearer and less error-prone to open the file twice (input, then clobber and output) instead of opening once in read-and-write mode.
Working, tested code:
#!perl
use strict;
use warnings;
my $filename = '/tmp/test.index';
my $index = 0;
if ( -e $filename ) {
open my $input_fh, '<', $filename
or die "Failed to open '$filename' for input: $!";
my $line = <$input_fh>;
if ( not eof $input_fh ) {
die "File '$filename' has more than one line! Dying to avoid c
+lobbering unexpected contents.";
}
close $input_fh or warn;
chomp $line;
$index = $line;
}
$index++;
open my $output_fh, '>', $filename
or die "Failed to open (and clobber) '$filename' for output: $!";
print {$output_fh} $index, "\n";
close $output_fh or warn;
| [reply] [d/l] |
Re: the manuals fail me
by MidLifeXis (Monsignor) on Mar 29, 2012 at 12:41 UTC
|
All responses (at this point in time) except for ikegami's (++, BTW) do not mention locking. If you will be accessing this file simultaneously by multiple processes, you will want to use a locking solution, or you face a race condition.
| [reply] |
Re: the manuals fail me
by Anonymous Monk on Mar 29, 2012 at 14:01 UTC
|
Thanks, all. That gives me many ideas. | [reply] |