in reply to Re^2: Creating a .zip file using perl
in thread Creating a .zip file using perl

The following code will use Archive::Zip to create a zip with all the .lib files from a specified directory.

This was pulled pretty much directly from the examples in the documentation

use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use File::Spec; use strict; use warnings; my $zipfile = shift or die "What's the zipfile?"; my $dir = shift or die "Where's the dir?"; my $zip = Archive::Zip->new(); opendir my $dh, $dir or die $!; while (readdir $dh) { next if !/\.lib$/; $zip->addFile(File::Spec->catfile($dir, $_), $_); } closedir $dh; # Save the Zip file unless ( $zip->writeToFileNamed($zipfile) == AZ_OK ) { die 'write error'; }

Replies are listed 'Best First'.
Re^4: Creating a .zip file using perl
by Anonymous Monk on May 05, 2011 at 07:12 UTC

    Hi Wind - I get the following error when running the code,any idea what is wrong?

    >perl c:\perl_scripts\zip.pl test C:\dir Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14.

      Well, since line 14 is:

      next if !/\.lib$/;

      I would surmise that $_ is not being initialized properly.

      Did you change the code in some way that might lead to this? Like by assigning the return value of the readdir call to a variable?

        wind is correct in that $_ is not initialized, readdir does not assign to $_ by default and so the readdir should be assigned to $_ directly.
        while ($_ = readdir( $dh)) {
        Then the script will work correctly.

        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

        I didnt change anything...ran as it is...is it working for you?

        printing $dh is showing the below value DIR:GLOB(0x1835244)

        I am running the script as perl c:\perl_scripts\zip.pl test C:\dir,is it not right? C:\dir is the directory it should find the libs,why is DIR not getting initialized?