Update: This post contains some inaccuracies, which Tye points out in a reply below. Read both posts.

To me, this really sounds like you want to accomplish several steps :

  1. Read $thenumber from a file.
  2. Print $thenumber
  3. Increment $thenumber
  4. Write the new value of $thenumber back to the file
And it seems like you want to do this for a web counter CGI program.

The solution is available in the form of the File::CounterFile module. This maintains a counter file which does the counting for you.

If you think rolling your own version is the better solution, keep the following problems in mind :

Update: On rereading your question, I haven't answered your actual questions. Your code

open(COUNTTHIS, "< $counttext") or die "I can't open counttext: $!\n"; print(<COUNTTHIS>);
opens the file with the name contained in $counttext and prints the first line of it. The next line in your code,
$thenumber = <COUNTTHIS>; #I thought something like this, but no
reads the second line from the file COUNTTHIS, and stores it in $thenumber. What you propably wanted to do was to read a single line from COUNTTHIS, print it, and store it in $thenumber, like this :
print (<COUNTTHIS>); $thenumber = $_; # or, alternatively, like this : $thenumber = <COUNTTHIS>; print $thenumber;

In reply to Re: How do I assign a variable to the file content? by Corion
in thread How do I assign a variable to the file content? by NotProud

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.