Home > Net >  How to check if Archive::Tar's write is successful?
How to check if Archive::Tar's write is successful?

Time:09-17

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:

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.

  • Related