I have a monthly Script running to archive files from past month. I'm using Archive::Tar
to generate the archive. How can I check if calls to the ->write
method are successful?
Does the following work? I didn't manage to fail $tar->write()
yet.
unless ($tar->write( $name . '.tar.xz', COMPRESS_XZ )) {
die("cant write tar"); # or any other doings instead
}
CodePudding user response:
Yes, the code in your question will catch any error within ->write
.
The documentation of Archive::Tar
does not specify what the return value of write
is (except when no argument is provided, which isn't the case here). However, looking at the code of the module, write
returns undef
in case of an error and a true value in case of success:
1
if it was writing to a file (this is the case for the code in your question)
Note that if something goes wrong and Archive::Tar
returns undef
, then it will also print an error message (unless you set $Archive::Tar::WARN
to 0
by doing $Archive::Tar::WARN = 0
). If you want to do something specific depending on the error, you can access the error message using the ->error
method.