Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: File I/O

by davidrw (Prior)
on Jan 17, 2007 at 20:44 UTC ( [id://595128]=note: print w/replies, xml ) Need Help??


in reply to File I/O

your attempt only opens the file for read, not write.
open RECORD_NUMBER, '<', "counter_data.dat" or die; $count = <RECORD_NUMBER>; close RECORD_NUMBER; $count++ open FILE, '>', "counter_data.dat" or die; print FILE $count; close FILE;
Have to ask, WHY do you need to do this? (there may be a far better/more robust way to accomplish your specifc task)

alternative cmd-line way (see perlrun for the switches):
perl -i -pe '++$_' counter.dat

Replies are listed 'Best First'.
Re^2: File I/O
by Melly (Chaplain) on Jan 17, 2007 at 22:46 UTC

    Maybe, maybe not. For simple jobs run from a cron or similiar, where locking may not be an issue, a simple counter file can often be the most suitable solution (esp. where ease of distrib. is an issue).

    IMHO, needless to say, but KISS.

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk
Re^2: File I/O
by Anonymous Monk on Jan 18, 2007 at 03:59 UTC
    First: Thanks to everyone who responded--so quickly!! This is sort of the blind leading the blind...I wrote a short PERL CGI script to give a truly non-programming person the ability to use a simple form that accepts input and creates an html file (which builds a table of data). My first crack at the CGI has very limited error checking. As I look to improve it, I figured I could capture the date and also increment the record entry counter so Patty only has to worry about a few entry fields. She is the only person besides myself that will probably ever use this. I guess I managed to do a couple things right...my die statements actually have a descriptive string with them. The way I opened the file threw me because a couple variations "did" write, but was incorrect. thanks again, I'm confident this will work. Can't wait to try it out in the office.
Re^2: File I/O
by blazar (Canon) on Jan 18, 2007 at 10:24 UTC
    your attempt only opens the file for read, not write.
    open RECORD_NUMBER, '<', "counter_data.dat" or die; $count = <RECORD_NUMBER>; close RECORD_NUMBER; $count++ open FILE, '>', "counter_data.dat" or die; print FILE $count; close FILE;

    I see most people answered along these lines. It is worth mentioning open '+<' mode too, although in that case one would have to seek as well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://595128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found