http://qs1969.pair.com?node_id=652604


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