I am writing a program and am having a problem with a file I opened being prematurely eof'd... going out of scope somehow perhaps?? Not sure exactly what's going on. Am prepared to admit / accept that its something I don't know or understand properly and be educated.
Can anybody shed any light as to why this little program prematurely reaches end-of-file??? It gets through one iteration and returns the proper amount of data. The second go-around it says its at the end-of-file when there is clearly more data available (the actual dataset I'm using is almost 8MB so it definitely ain't lack of data!) I've stared at it for quite a while and reluctantly admit I just don't see it... ???
#! /bin/perl -w
## test to see when/how a file goes out of scope
use strict;
my $file = 'test.data';
my $frame_marker = 'data';
open(FILE_FD, $file) or die($!);
foreach my $loop (1..10) {
my $data = get_replay_data();
print "DATA READ:\n", map{ chomp; " VALUE='$_'\n" } @$data;
}
sub get_replay_data { # read from replay file and return a block of da
+ta
my $found = 0;
my @data = ();
die("ERROR -- File handle has closed (why??): $!") if (eof(FIL
+E_FD));
foreach my $line (<FILE_FD>) { # pull records from replay file
## wait for begin-marker to capture data
if ($line =~ /<$frame_marker>/i) { # wait for start-of
+-frame marker
$found++;
push @data, $line;
next;
}
if (!$found) {
next;
}
## wait for end-marker & return data
if ($line =~ /<\/$frame_marker>/i) {
push @data, $line;
return \@data; # return xml data if end-of-fra
+me marker found
}
push @data, $line; # otherwise grab frame data
}
return \@data;
}
__END__
--- Some sample data to work with:
<data>
<string>1 some stuff</string>
<string>1 some more stuff</string>
<string>1 yet more stuff</string>
<string>1 enough stuff</string>
</data>
<data>
<string>2 some stuff</string>
<string>2 some more stuff</string>
<string>2 yet more stuff</string>
<string>2 enough stuff</string>
</data>
<data>
<string>3 some stuff</string>
<string>3 some more stuff</string>
<string>3 yet more stuff</string>
<string>3 enough stuff</string>
</data>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.