rjahrman has asked for the wisdom of the Perl Monks concerning the following question:

I've looked around but haven't found anything helpful . . .

I'm trying to zip up the files in a directory. The script is running in a cgi-bin (shared server). The files that I need to zip are in subdirectories of the cgi-bin. (so /cgi-bin/folder/subfolder/* needs to end up in /cgi-bin/archiveName.zip) Because I will be moving this script around, I can't rely on any modules being there.

I tried:
`zip -r folder/subfolder archiveName`;

But that didn't work. It's been a while since I've done Perl, so I don't remember how to capture errors on a shell command like this. That would probably be helpful if anyone could tell me.

Any other ideas on what else might be going wrong?

Replies are listed 'Best First'.
Re: zip at shell not working
by Zaxo (Archbishop) on May 25, 2005 at 04:15 UTC

    mifflin gives you the correct solution for calling the external utility (though I'd just test for false or $@ instead of zero).

    If you are on a system where .zip is the usual archive format you ought to have Archive::Zip available. That will make your choice of files to include in the package much more flexible.

    I don't like your use of relative paths for this. You don't know that the http server will make the cgi directory current for a run. If some DirectoryIndex points to your script it will run in that directory, instead.

    After Compline,
    Zaxo

Re: zip at shell not working
by mifflin (Curate) on May 25, 2005 at 03:52 UTC
    Don't use back ticks to shell out and execute something unless you really want what it returns to stdout.
    Use system instead.
    my $rc = system("zip -r folder/subfolder archiveName"); if ($rc != 0) { # see error message in $! }
    for more info on the system function execute the following at the command prompt
    perldoc -f system