Subject: | Buglet in 1.08: Filehandle clobbered |
Date: | Sat, 27 Oct 2007 00:14:56 +0200 |
To: | bug-XML-Tiny [...] rt.cpan.org |
From: | Max Maischein <corion [...] cpan.org> |
Hello David,
while browsing the source code of XML::Tiny, I noticed that the
filehandle in C<parsefile()> is not localized and thus might clobber
some other use of the C<*XML::Tiny::FH> filehandle (a bit esoteric this
case). Still, as your code might be used by people less aware of the
potential problems of global filehandles, the enclosed test case checks
that the filehandle does not get clobbered. The fix is trivial, just add
the localization of FH:
local *FH;
open(FH, $arg) || die(__PACKAGE__."::parsefile: Can't open $arg\n");
There also is a small syntax error in the SYNOPSIS:
=head1 SYNOPSIS
use XML::Tiny qw(parsefile);
-open($xmlfile, 'something.xml);
+open($xmlfile, 'something.xml');
my $document = parsefile($xmlfile);
Hope that helps,
-max
#!/usr/bin/perl -w
use strict;
use File::Basename;
use File::Spec;
use Test::More tests => 3;
=head1 DESCRIPTION
This test checks that XML::Tiny does localize
all filehandles it uses, so it doesn't clobber
filehandles from other parts of an application.
This is less a bug with newer versions of Perl
which encourage use of lexical filehandles but
in Perl before 5.6.0, lexical filehandles are
not supported.
=cut
use_ok 'XML::Tiny';
my $base = dirname $0;
open XML::Tiny::FH, $0
or die "Couldn't read '$0': $!"; # read ourselves in
seek XML::Tiny::FH, 10, 0
or die "Couldn't seek '$0': $!";
my $fh_pos = tell XML::Tiny::FH;
diag "At $fh_pos";
my $document = XML::Tiny::parsefile(File::Spec->catfile($base,'dummy.xml'));
isn't $document, undef, "We read 'dummy.xml'";
my $new_pos = tell *XML::Tiny::FH;
is $new_pos, $fh_pos, "The FH filehandle survived unchanged.";
<xml>
<some>tag</some>
<some><other>tag</other></some>
</xml>