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

Re: Filehandles vs Uninitialized Values in pattern match

by HuckinFappy (Pilgrim)
on Nov 23, 2007 at 16:21 UTC ( #652604=note: print w/replies, xml ) Need Help??

in reply to Filehandles vs Uninitialized Values in pattern match

When you use the line:
open (DAT, "+>>$file") or die "$!";
You aren't just opening the file read/write, you're opening it read/write in *append* mode. That means the pointer is at the end of the file when you open it. So when you read it, you don't get anything. There are a few things you can try here to learn a little more. Here's a little debugger session I played with that might help you get this working correctly. There's also some other things I do differently here that you may wish to adopt as standard practices, such as using a lexical filehandle and 3-argument form of open:
DB<1> open $fh, '+>>', 'test.dat' + + DB<2> if ( eof $fh ) { print "Don't try to read, you're at the end!\ +n" } + Don't try to read, you're at the end! DB<3> seek $fh, 0, 0 + + DB<4> if ( not eof $fh ) { print "Read on, McDuff!\n" } + + Read on, McDuff! DB<5> @lines = <$fh> + + DB<6> x @lines DB<7> chomp @lines + + 0 'First Line' 1 'Second Line' 2 'Last Line'
Hope this helps, ~Jeff

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2023-06-08 21:18 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (35 votes). Check out past polls.