Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval & rmtree

Status
Not open for further replies.

1yura1

Programmer
Feb 7, 2003
36
0
0
UA
Hi all.
I'm using the following structure in my perl script:

eval { File::path::rmtree($dir) };
if ($@) {
do_somth();
}

But sometimes, the script prints to STDOUT/STDERR following warnings/errors:

Can't remove directory /some/dir: Directory not empty at script.pl line XXX

It is required to trap such warnings/errors, and NOT to print then to STDOUT/STDERR

Thanks.
 
From perldoc -f eval:
Beware that using "eval" neither silences perl from printing warnings to STDERR, nor does it stuff the text of warning messages into $@. To do either of those, you have to use the $SIG{__WARN__} facility, or turn off warnings inside the BLOCK or EXPR using "no warnings 'all'". See "warn", perlvar, warnings and perllexwarn.
From perldoc perlvar:
The routine indicated by $SIG{__WARN__} is called when a warning message is about to be printed. The warning message is passed as the first argument. The presence of a __WARN__ hook causes the ordinary printing of warnings to STDERR to be suppressed. You can use this to save warnings in a variable, or turn warnings into fatal errors, like this:

local $SIG{__WARN__} = sub { die $_[0] };
eval $proggie;
 
Thanks for your help

I found the same solution:

{
local $SIG{'__WARN__'} = sub {
print $_[0];
};
eval { File::path::rmtree($dir) };
}

Best Regards.
Yura.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top