Here is a simple example of how you can do this by passing a fake file descriptor to your XML parser. This object overloads the operator readline( <>) to return your fake root tags with lines from the file between them.
package FakeFile;
use strict;
use warnings;
use overload '<>' => \&my_readline;
sub new {
my $class = shift;
my $filename = shift;
open my $fh, '<', $filename or die "open $filename: $!";
return bless { fh => $fh }, $class;
}
sub my_readline {
my $self = shift;
return if $self->{done};
if ( not $self->{started} ) {
$self->{started} = 1;
return '<fake_root_tag>';
}
if ( eof $self->{fh} ) {
$self->{done} = 1;
return '</fake_root_tag>';
}
return readline $self->{fh};
}
1;
This will not work if your parser expects a genuine file descriptor (for example, using something like sysread), but you might find it inspiring.
Usage example:
echo "one
two
three" > myfile
perl -MFakeFile -E 'my $f = FakeFile->new( "myfile" ); print while <$f>'
source
share