OK, I think this'll work (I tested this by making test1.txt a normal text file, and then translating every "\n" into "\007" into test2.txt):
#!/usr/bin/perl -w
use strict;
package MyHandle;
use Tie::Handle;
use IO::Handle;
use base 'Tie::StdHandle';
my %seps;
sub READLINE
{
my $self = shift;
local $/ = $seps{$self};
$self->SUPER::READLINE(@_);
}
sub set_input_separator
{
my $self = shift;
$seps{$self} = shift;
}
sub DESTROY
{
delete $seps{shift};
}
package main;
my $fh = tie *FH2, 'MyHandle';
$fh->set_input_separator( "\007" );
open FH1, 'test1.txt';
open FH2, 'test2.txt';
while (<FH1>)
{
my $data1 = $_;
chop($data1);
my $data2 = <FH2>;
chop($data2);
print "(1)$data1\n(2)$data2\n";
}
update: added DESTROY to avoid mem leaks
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.