Skip to content

Commit

Permalink
Convert XML::Parser to use 3 arg opens
Browse files Browse the repository at this point in the history
Do not use bare word file handles as they tend to leak.
  • Loading branch information
toddr committed Sep 24, 2019
1 parent 2c72ffa commit 5ccabfc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
25 changes: 12 additions & 13 deletions Expat/Expat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ sub load_encoding {
}
}

local (*ENC);
open( ENC, $file ) or croak("Couldn't open encmap $file:\n$!\n");
binmode(ENC);
open( my $fh, '<', $file ) or croak("Couldn't open encmap $file:\n$!\n");
binmode($fh);
my $data;
my $br = sysread( ENC, $data, -s $file );
my $br = sysread( $fh, $data, -s $file );
croak("Trouble reading $file:\n$!\n")
unless defined($br);
close(ENC);
close($fh);

my $name = LoadEncoding( $data, $br );
croak("$file isn't an encmap file")
Expand Down Expand Up @@ -499,11 +498,11 @@ sub parsestring {
sub parsefile {
my $self = shift;
croak 'Parser has already been used' if $self->{_State_};
local (*FILE);
open( FILE, $_[0] ) or croak "Couldn't open $_[0]:\n$!";
binmode(FILE);
my $ret = $self->parse(*FILE);
close(FILE);

open( my $fh, '<', $_[0] ) or croak "Couldn't open $_[0]:\n$!";
binmode($fh);
my $ret = $self->parse($fh);
close($fh);
$ret;
}

Expand Down Expand Up @@ -685,9 +684,9 @@ XML::Parser::Expat - Lowlevel access to James Clark's expat XML parser
$parser->setHandlers('Start' => \&sh,
'End' => \&eh,
'Char' => \&ch);
open(FOO, '<', 'info.xml') or die "Couldn't open";
$parser->parse(*FOO);
close(FOO);
open(my $fh, '<', 'info.xml') or die "Couldn't open";
$parser->parse($fh);
close($fh);
# $parser->parse('<foo id="me"> here <em>we</em> go </foo>');
sub sh
Expand Down
18 changes: 9 additions & 9 deletions Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,22 @@ sub parsestring {
sub parsefile {
my $self = shift;
my $file = shift;
local (*FILE);
open( FILE, $file ) or croak "Couldn't open $file:\n$!";
binmode(FILE);

open( my $fh, '<', $file ) or croak "Couldn't open $file:\n$!";
binmode($fh);
my @ret;
my $ret;

$self->{Base} = $file;

if (wantarray) {
eval { @ret = $self->parse( *FILE, @_ ); };
eval { @ret = $self->parse( $fh, @_ ); };
}
else {
eval { $ret = $self->parse( *FILE, @_ ); };
eval { $ret = $self->parse( $fh, @_ ); };
}
my $err = $@;
close(FILE);
close($fh);
die $err if $err;

return unless defined wantarray;
Expand Down Expand Up @@ -345,9 +345,9 @@ XML::Parser - A perl module for parsing XML documents
$p3->setHandlers(Char => \&text,
Default => \&other);
open(FOO, 'xmlgenerator |');
$p3->parse(*FOO, ProtocolEncoding => 'ISO-8859-1');
close(FOO);
open(my $fh, 'xmlgenerator |');
$p3->parse($foo, ProtocolEncoding => 'ISO-8859-1');
close($foo);
$p3->parsefile('junk.xml', ErrorContext => 3);
Expand Down

0 comments on commit 5ccabfc

Please sign in to comment.