This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In IPC::Open3::_open(), refactor the fork/exec code to loop over @handles.
authorNicholas Clark <nick@ccl4.org>
Tue, 7 Jun 2011 10:37:28 +0000 (12:37 +0200)
committerNicholas Clark <nick@ccl4.org>
Sat, 11 Jun 2011 06:48:16 +0000 (08:48 +0200)
ext/IPC-Open3/lib/IPC/Open3.pm

index 204fb37..60051ba 100644 (file)
@@ -269,29 +269,18 @@ sub _open3 {
                    $handles[2]{parent} = $tmp;
                }
 
-               if ($handles[0]{dup}) {
-                   xopen \*STDIN, '<&', $handles[0]{parent} if fileno \*STDIN != xfileno($handles[0]{parent});
-               } else {
-                   xclose $handles[0]{parent};
-                   xopen \*STDIN,  "<&=", fileno $handles[0]{open_as};
-               }
-               if ($handles[1]{dup}) {
-                   xopen \*STDOUT, '>&', $handles[1]{parent} if fileno \*STDOUT != xfileno($handles[1]{parent});
-               } else {
-                   xclose $handles[1]{parent};
-                   xopen \*STDOUT, ">&=", fileno $handles[1]{open_as};
-               }
-               if (!$handles[2]{dup_of_out}) {
-                   if ($handles[2]{dup}) {
-                       xopen \*STDERR, '>&', $handles[2]{parent}
-                           if fileno \*STDERR != xfileno($handles[2]{parent});
+               foreach (@handles) {
+                   if ($_->{dup_of_out}) {
+                       xopen \*STDERR, ">&STDOUT"
+                           if defined fileno STDERR && fileno STDERR != fileno STDOUT;
+                   } elsif ($_->{dup}) {
+                       xopen $_->{handle}, $_->{mode} . '&', $_->{parent}
+                           if fileno $_->{handle} != xfileno($_->{parent});
                    } else {
-                       xclose $handles[2]{parent};
-                       xopen \*STDERR, ">&=", fileno $handles[2]{open_as};
+                       xclose $_->{parent};
+                       xopen $_->{handle}, $_->{mode} . '&=',
+                           fileno $_->{open_as};
                    }
-               } else {
-                   xopen \*STDERR, ">&STDOUT"
-                       if defined fileno \*STDERR && fileno \*STDERR != fileno \*STDOUT;
                }
                return 0 if ($_[0] eq '-');
                exec @_ or do {