Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How do I assign a variable to the file content?

( #33866=categorized question: print w/ replies, xml ) Need Help??
Contributed by NotProud on Sep 25, 2000 at 00:26 UTC
Q&A  > files


I am trying to get a number from a text file and add one to it. Here is my code so far. Where does one go from here?
#!/usr/local/bin/perl -w use strict; print "Content-type: text/html\n\n"; $counttext = 'd:/'; $thenumber; open(COUNTTHIS, "< $counttext") or die "I can't open counttext: $!\n"; print (<COUNTTHIS>); $thenumber = <COUNTTHIS>; #I thought something like this, but no print "$thenumber"; print "<br>"; $thenumber = ++$thenumber; print "$thenumber";
Thank you!

Answer: How do I assign a variable to the file content?
contributed by Corion

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 :

  • Concurrency: If two users simultaneously access your counter CGI, they will see the same counter value. Hits may be lost on an overloaded machine, if one counter program runs much slower than several others.
  • Security: Your program is writing to a file on your system. This always means that you must take special care not to open up any security holes, like overwriting the wrong files.

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;
Answer: How do I assign a variable to the file content?
contributed by meonkeys

Keeping in mind all of Corion's expert advice, here's a working version of your code, assuming counttext.txt exists and is accessible:

#!/usr/local/bin/perl -w use strict print "Content-type: text/html\n\n"; my $counttext = 'd:/'; my $thenumber; open(COUNTTHIS, "< $counttext") or die "I can't open counttext: $!\n"; $thenumber = <COUNTTHIS>; close(COUNTTHIS); print "$thenumber<br>"; ++$thenumber; print "$thenumber"; open(OUT, "> $counttext") or die "I can't open counttext: $!\n"; print OUT $thenumber; close(OUT);

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2014-09-21 13:20 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (171 votes), past polls