This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor t/run/runenv.t to use lexical file handles.
[perl5.git] / t / run / runenv.t
index 2d1ac1f..9af0b29 100644 (file)
@@ -40,22 +40,23 @@ sub runperl_and_capture {
   my $pid = fork;
   return (0, "Couldn't fork: $!") unless defined $pid;   # failure
   if ($pid) {                   # parent
-    my ($actual_stdout, $actual_stderr);
     wait;
     return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE;
 
-    open F, "< $STDOUT" or return (0, "Couldn't read $STDOUT file");
-    { local $/; $actual_stdout = <F> }
-    open F, "< $STDERR" or return (0, "Couldn't read $STDERR file");
-    { local $/; $actual_stderr = <F> }
-
-    return ($actual_stdout, $actual_stderr);
+    open my $stdout, '<', $STDOUT
+       or return (0, "Couldn't read $STDOUT file: $!");
+    open my $stderr, '<', $STDERR
+       or return (0, "Couldn't read $STDERR file: $!");
+    local $/;
+    # Empty file with <$stderr> returns nothing in list context
+    # (because there are no lines) Use scalar to force it to ''
+    return (scalar <$stdout>, scalar <$stderr>);
   } else {                      # child
     for my $k (keys %$env) {
       $ENV{$k} = $env->{$k};
     }
-    open STDOUT, "> $STDOUT" or exit $FAILURE_CODE;
-    open STDERR, "> $STDERR" and do { exec $PERL, @$args };
+    open STDOUT, '>', $STDOUT or exit $FAILURE_CODE;
+    open STDERR, '>', $STDERR and do { exec $PERL, @$args };
     # it didn't_work:
     print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n";
     exit $FAILURE_CODE;
@@ -128,9 +129,9 @@ try({PERL5OPT => '-Mstrict -Mwarnings'},
     "ok",
     "");
 
-open F, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!";
-print F "package Oooof; 1;\n";
-close F;
+open my $fh, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!";
+print $fh "package Oooof; 1;\n";
+close $fh;
 END { 1 while unlink "Oooof.pm" }
 
 try({PERL5OPT => '-I. -MOooof'},