perl

Command Line Zip for Windows

Windows doesn't have a command line utility for compressing files (that I know of), and I had to come up with a way to automate some file transfers today. So I whipped up a tiny Perl script that will zip up a file. I figure someone else might need it. (Or someone might know how to do this in one line.) You'll need the Archive::Zip module, and the following code—which is pretty much right out of the documentation:
# Grab the incoming file
my $argv = join(' ', @ARGV) or die "Usage: zipme.pl [file location]\n";

# Grab the file name
my($dir, $file) = $argv =~ m/(.*\\)(.*)$/;

# Create a Zip file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();

# Add the file
my $file_member = $zip->addFile($argv, $file);

# Save the Zip file
unless ( $zip->writeToFileNamed($argv.'.zip') == AZ_OK ) {
    die 'couldn\'t zip';
}
Save this code as zipme.pl, and you'll be set. Pass in a filename, and you'll get a compressed file of the same name plus the .zip extension. So:

C:\>perl zipme.pl C:\path\to\giant.file

will give you C:\path\to\giant.file.zip. It works well with Windows batch files, and will save me a bunch of bandwidth.

Compiling Perl modules on Windows

I've been doing quite a bit of Perl scripting for Yahoo! Hacks, and I can safely say it's my scripting language of choice now. A big reason for this is the abundance of pre-existing modules available. Why reinvent the wheel if someone else has been there before? I also develop most of the scripts on a Windows machine running ActivePerl. It works well, but the modules available via the ActiveState Perl Package Manager leave something to be desired. Many of the modules I want to use are only available via CPAN, which means I have to compile the modules for Windows myself.

Compiling Perl modules isn't normally a big deal. In fact, on my Mac it's a snap. I simply downloaded the Mac Developer Tools and started make-ing stuff all day long. I assume the same is true for Linux. Windows is another story.

Compiling modules would probably be easy if I was a C++ developer, and had a copy of Visual Studio Something-or-other with its own compiler. But I'm a thrifty scripter, and I just want some Perl modules to work. So I had to go through a series of steps, and put all of the pieces into place to be able to compile modules. I thought I'd write it up here in case anyone else out there is pulling their hair out like I was.

How to compile Perl modules on Windows without a Visual Studio:
  • Get nmake. (direct link to file.) This is your compiler.
  • Sometimes you'll need a file called Windows.h. Install the Windows SDK.
  • Sometimes you'll need a file called msvcrt.lib or msvcr70.lib. Install the .NET framework SDK.
  • For everything else you need install the Visual C++ Toolkit.
  • At this point, you will have installation fatigue. Take a break.
  • Inside the VC++ Toolkit is an important file called vcvars32.bat that adds some key directories to your system PATH variable. Edit this file so that the lib and bin directories of each of the kits you just installed will be added to PATH when you run this batch file.
With these behemoth kits in place, you should be set to start compiling Perl packages. Once in a while you may need the odd odbc32.lib—it's included with the SQL Server developer tools. Don't have it? Start installing. ;) And when you're done, don't forget to add its directory to your PATH.

Run vcvars32.bat before you start compiling a module to get your paths in order, and then run nmake like you would make:

nmake test
nmake install

I was almost in tears when things were compiling correctly. :) I have to let you know that I'm not a Perl expert, and compiling modules like this may wreak havoc with your system. But it's working fine for me.