Skip to content

Commit 02d13ad

Browse files
committed
Use SIG{__WARN__} when loading Rexfile instead of stderr redirection
1 parent aa078fd commit 02d13ad

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

lib/Rex/CLI.pm

+11-14
Original file line numberDiff line numberDiff line change
@@ -744,14 +744,11 @@ sub load_rexfile {
744744
}
745745
};
746746

747-
my ( $stdout, $stderr, $default_stderr );
748-
open $default_stderr, ">&", STDERR;
749-
750-
# we close STDERR here because we don't want to see the
751-
# normal perl error message on the screen. Instead we print
752-
# the error message in the catch-if below.
753-
local *STDERR;
754-
open( STDERR, ">>", \$stderr );
747+
# we don't want to see the
748+
# normal perl warning message on the screen. Instead we print
749+
# the warning message in the catch-if below
750+
my @warnings;
751+
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
755752

756753
# we can't use $rexfile here, because if the variable contains dots
757754
# the perl interpreter try to load the file directly without using @INC
@@ -761,13 +758,13 @@ sub load_rexfile {
761758
# update %INC so that we can later use it to find the rexfile
762759
$INC{"__Rexfile__.pm"} = $rexfile;
763760

764-
# reopen STDERR
765-
open STDERR, ">&", $default_stderr;
766-
767-
if ($stderr) {
768-
my @lines = split( $/, $stderr );
761+
if (@warnings) {
769762
Rex::Logger::info( "You have some code warnings:", 'warn' );
770-
Rex::Logger::info( "\t$_", 'warn' ) for @lines;
763+
for (@warnings) {
764+
# remove /loader/.../ prefix before filename
765+
s|/loader/[^/]+/||;
766+
Rex::Logger::info( "\t$_", 'warn' );
767+
}
771768
}
772769

773770
1;

0 commit comments

Comments
 (0)