Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Filehandles vs Uninitialized Values in pattern match

by HuckinFappy (Pilgrim)
on Nov 23, 2007 at 16:21 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2025-07-20 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.