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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|