[sf-perl] Slinging two handles into __DATA__

yary not.com at gmail.com
Fri Oct 3 15:41:25 PDT 2008


I just solved my issue, but since I had this email half typed up &
still have questions, thought I'd share.

My goal was to have two chunks to compare in a __DATA__ section and a
file handle pointing to each. I want to read from DATA and OLD_DATA as
if they are separate files, interleaving reads from them, doing
calculations on a lines from each, then continuing.

There was a bug and it seems to be in perl (v5.10.0 for cygwin), but
it might be something amiss with my usage (I haven't duped an input
file handle before). The code is below. Why is "seek DATA" required-
as only OLD_DATA has been read, why is the position of DATA disturbed?

(I know I could use a file, or two external files, or a database, or
in-memory variables, etc as other ways to do this- just curious.)

#!/usr/bin/perl -w

use strict;

my $pos = tell DATA;

open (OLD_DATA,"<&DATA");
# Skip ahead to the blank line
while (<OLD_DATA>) {last if /^\s*$/}

# Uncomment the next line to make it work better at first
# but even with this seek, the handles get out of sync later
#seek DATA, $pos, 0;

# Print first line after the first blank line- works
print scalar <OLD_DATA>,"\n";

# I want it to print the first line of the data, but
# instead it prints some info from partway through
print scalar <DATA>,"\n";

# Print corresponding lines
# they stop corresponding after a while, even with the seek
while (<DATA>) {
 print "new row is $_, corresponding old row was ",scalar(<OLD_DATA>),"\n";
}

# 11000+ lines follow
__DATA__
row 1
row 2
...
row 5100

old row 1
old row 2
....
old row 5100


More information about the SanFrancisco-pm mailing list