Re: How to catch error message instead of printing to stderr for rmtree



Hi

Thank you for the feedback. But I don't see any difference between my
script and CPAN's documentation. Actually, I do follow it to write my
script.



On Fri, Sep 26, 2008 at 12:17 AM, Yue Chen <godsarmycy@xxxxxxxxx> wrote:

Hi

I want to use rmtree to delete a dir and use variables to catch the
error message. However, when i am about to delete a dir that does not
belong to me, it still print the error message to stderr. Does anyone
have clue?

my script: test.pl

use File::Path;
File::Path::rmtree( '/root', {error => \$err, safe => 1, result =>
\$list, keep_root => 1} );
for my $diag (@$err) {
my ($file, $message) = each %$diag;
print "problem unlinking $file: $message\n";
}
print "unlinked $_\n" for @$list;

$ test.pl
Can't make directory /root read+writeable: Operation not permitted at
a.pl line 2
Can't read /root: Permission denied at a.pl line 2
rmdir /root
Can't remove directory /root: Permission denied at a.pl line 2
and can't restore permissions to 0750
at a.pl line 2

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/



from File::Path documentation:

rmtree(
'foo/bar/baz', '/zug/zwang',
{ verbose => 1, error => \my $err_list }
);

error
If present, will be interpreted as a reference to a list, and
will
be used to store any errors that are encountered. See the ERROR
HANDLING section for more information.

If this parameter is not used, certain error conditions may raise
a
fatal error that will cause the program will halt, unless trapped
in an "eval" block.

ERROR HANDLING
If "mkpath" or "rmtree" encounter an error, a diagnostic message will
be printed to "STDERR" via "carp" (for non-fatal errors), or via
"croak" (for fatal errors).

If this behaviour is not desirable, the "error" attribute may be used
to hold a reference to a variable, which will be used to store the
diagnostics. The result is a reference to a list of hash references.
For each hash reference, the key is the name of the file, and the
value
is the error message (usually the contents of $!). An example usage
looks like:

rmpath( 'foo/bar', 'bar/rat', {error => \my $err} );
for my $diag (@$err) {
my ($file, $message) = each %$diag;
print "problem unlinking $file: $message\n";
}





.



Relevant Pages

  • Re: Tutorial of implementing a Matlab dll
    ... project using reference I get the following error message: ... A reference could not be added. ... If the documentation on the website didn't work, you should contact Technical Support and let them know that it didn't work, and ask them to help you make it work and to update that document. ...
    (comp.soft-sys.matlab)
  • Re: What is client startup script?
    ... It would help if you mentioned what documentation you were reading. ... I DID see a reference to "client-side script," ... > reference to "client startup script" as "Determines if the client startup ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Property Set with more than one argument
    ... | the me builtin reference to the current object instance. ... I didn't know about the "me" reference! ... Script V5.6 Documentation", ... Though it's used in some topics in that documentation (IsObject Function, ...
    (microsoft.public.scripting.vbscript)
  • What is client startup script?
    ... While reading the documentation on IsStartupScriptRegistered, ... reference to "client startup script" as "Determines if the client startup ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: window object
    ... Within a script, every reference to an object amounts to two ... calls from the scripting engine to the DHTML Object Model. ... var sText = document.all.div1.innerText; ...
    (comp.lang.javascript)