I am trying to learn perl, and am trying to implement something simple: reading, updating, then writing an index value to a file. This is what it would be in bash:
index = `cat /tmp/myfile.index` $index = $index + 1 echo $index > /tmp/myfile.index exit 0
This is what I think I need to do in perl (comments about specific lines of code follow the code):
if(! -e "/tmp/test.index") { system("/usr/bin/touch /tmp/test.index"); } #This first test is necessary because if I add the creation permission + to the open command, it clobbers the file, preventing me from readin +g from it open(FILE, "+</tmp/test.index") || die $!; $index = <FILE>; #printing out the value of $index leads me to believe that this part w +orks if( ! $index =~ /[0-9]+/ ) { $index = 1; } #This should handle any sort of corruption to the file $i++; truncate(FILE, 0); #I do this to be safe. But when I do, it prepends two null characters +. print FILE $index; #This command has no apparent effect. close(FILE); exit 0;
Help?
In reply to the manuals fail me by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |