Monday, May 18, 2009

[Perl] CPAN

Introduction

Comprehensive Perl Archive Network(CPAN) is an organization for collecting most of Perl modules from all over the world. Below is a simple way to install the CPAN on your system.
Perl -MCPAN -e "shell"
After the installation completed, you probably need to update the CPAN by typing follow command:
install Bundle::CPAN
Here is an example for the use of CPAN:

Perl Message-Digest Program


Get or update Digest module
sudo cpan -i Digest
Waiting for few minutes for this installation. If you get any error, try to follow the instruction to solve it.

Coding

Here I just use the most popular message-digest algorithm MD5 for this demo.
#! /usr/bin/perl -w

use strict;
use warnings;
use Digest::MD5;

# load filename from argument
my $filename = shift;

# if user didn't give a filename, ask it.
unless($filename){
print "Please enter a filename:";
chomp($filename = <STDIN>);
}

# open file
open(FILE,"< $filename") or die "Fail to open [$filename] for MD5 checksum\n";

# create a new cipher text
my $ctx = Digest::MD5->new;

# add file into cipher text
$ctx->addfile(*FILE);

# get MD5 checksum
my $digest = $ctx->digest;

# print MD5 as hex code.
print "MD5:".unpack('H*', "$digest"),"\n";

Demo
md5check.pl test.dat
MD5:9e1837461d81e918f73c8e6fe7a5d0e2

0 意見:

Post a Comment