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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.