From: Nicholas Clark Date: Mon, 6 Jun 2011 18:45:13 +0000 (+0200) Subject: In IPC::Open3::_open(), refactor the common code into loops over @handles. X-Git-Tag: accepted/trunk/20130322.191538~3840 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c12e47ae20ac31659a9f4daa9df439caca65d08;p=platform%2Fupstream%2Fperl.git In IPC::Open3::_open(), refactor the common code into loops over @handles. As fh_is_fd() is now used in only one location, inline it. (This function isn't exported, isn't documented, and isn't (ab)used by anything on CPAN.) --- diff --git a/ext/IPC-Open3/lib/IPC/Open3.pm b/ext/IPC-Open3/lib/IPC/Open3.pm index 2560176..204fb37 100644 --- a/ext/IPC-Open3/lib/IPC/Open3.pm +++ b/ext/IPC-Open3/lib/IPC/Open3.pm @@ -185,10 +185,6 @@ sub xclose { or croak "$Me: close($_[0]) failed: $!"; } -sub fh_is_fd { - return $_[0] =~ /\A=?(\d+)\z/; -} - sub xfileno { return $1 if $_[0] =~ /\A=?(\d+)\z/; # deal with fh just being an fd return fileno $_[0]; @@ -231,19 +227,23 @@ sub _open3 { $handles[2]{parent} ||= $handles[1]{parent}; $handles[2]{dup_of_out} = $handles[1]{parent} eq $handles[2]{parent}; - $handles[0]{dup} = ($handles[0]{parent} =~ s/^[<>]&//); - $handles[1]{dup} = ($handles[1]{parent} =~ s/^[<>]&//); - $handles[2]{dup} = ($handles[2]{parent} =~ s/^[<>]&//); + my $package; + foreach (@handles) { + $_->{dup} = ($_->{parent} =~ s/^[<>]&//); - # force unqualified filehandles into caller's package - my $package = caller 1; - $handles[0]{parent} = qualify $handles[0]{parent}, $package unless fh_is_fd($handles[0]{parent}); - $handles[1]{parent} = qualify $handles[1]{parent}, $package unless fh_is_fd($handles[1]{parent}); - $handles[2]{parent} = qualify $handles[2]{parent}, $package unless fh_is_fd($handles[2]{parent}); + if ($_->{parent} !~ /\A=?(\d+)\z/) { + # force unqualified filehandles into caller's package + $package //= caller 1; + $_->{parent} = qualify $_->{parent}, $package; + } - xpipe $handles[0]{open_as}, $handles[0]{parent} if !$handles[0]{dup}; - xpipe $handles[1]{parent}, $handles[1]{open_as} if !$handles[1]{dup}; - xpipe $handles[2]{parent}, $handles[2]{open_as} if !$handles[2]{dup} && !$handles[2]{dup_of_out}; + next if $_->{dup} or $_->{dup_of_out}; + if ($_->{mode} eq '<') { + xpipe $_->{open_as}, $_->{parent}; + } else { + xpipe $_->{parent}, $_->{open_as}; + } + } my $kidpid; if (!DO_SPAWN) { @@ -360,9 +360,11 @@ sub _open3 { die "$Me: $@" if $@; } - xclose $handles[0]{open_as} if !$handles[0]{dup}; - xclose $handles[1]{open_as} if !$handles[1]{dup}; - xclose $handles[2]{open_as} if !$handles[2]{dup} && !$handles[2]{dup_of_out}; + foreach (@handles) { + next if $_->{dup} or $_->{dup_of_out}; + xclose $_->{open_as}; + } + # If the write handle is a dup give it away entirely, close my copy # of it. xclose $handles[0]{parent} if $handles[0]{dup};