====== Script for creating Perl CPAN packages ====== This article introduces a a utility that makes it easy to creates a Perl CPAN package from a directory that contains Perl source files. ===== Usage ===== ==== Building a CPAN package ==== Place the cpan.sh utility in the directory containing the project s source files, then run it as follows : > ./cpan.sh project version This will produce a project-version.tar.gz module that is ready for distribution. ==== Installing a CPAN package ==== In the target host where the module needs to be installed, ncompress the module using 'tar' > tar xfz project-version.tar.gz Then from inside the CPAN module type > make install To install the package is a non-standard path : > perl Maefile.pl INSTALL_BASE=SOMEPATH > make install ==== Notes ==== * When you add new dependencies in your code using the 'use' keyword, You just need to buid a new CPAN package again. The script parses the code for external dependencies and takes then in consideration when building the CPAN module. * If you add a new source file/module, you just need to buid a new CPAN package again. ==== Requirements ==== * Perl module "ExtUtils::MakeMaker" * awk, find, sed, grep, cat ===== The script ===== PRJ=$1 VER=$2 if [ $# -ne 2 ] then cat << USAGE Place the cpan.sh utility in the directory containing the project s source files, then run it as follows : ./cpan.sh project version USAGE exit fi mkdir /tmp/.lib cp -r * /tmp/.lib/ mkdir $PRJ-$VER mv /tmp/.lib $PRJ-$VER/lib cd $PRJ-$VER pwd for i in `find . -name *.p* -print`; do cat $i | sed 's/\^M//g' |grep -E '^use.*;'; done | cut -d" " -f2 | sed 's/()//g' | sed 's/;//g' | sed 's/ //g' | sort -u > .used for i in `find . -name *.p* -print`; do cat $i | sed 's/\^M//g' | grep -E '^package.*;'; done | cut -d" " -f2 | sed 's/;//g' | sort -u > .declared while read line ; do grep -q $line .declared; if ! [ $? -eq 0 ]; then echo "$line"; fi ; done < .used > .deps cat << MAKE-END-1 >> Makefile.PL use ExtUtils::MakeMaker; \$VERSION = '$VER'; WriteMakefile( NAME => '$PRJ', PREREQ_PM => { MAKE-END-1 for line in `cat .deps | grep -v -e '^[0-9]' ` do echo " '$line' => 0," >> Makefile.PL done cat << MAKE-END-0 >> Makefile.PL } ); MAKE-END-0 cat << MANIF > MANIFEST Makefile.PL MANIFEST MANIF # Create a makefile perl Makefile.PL # clean up rm -f .used .declared .deps cd .. tar cfz $PRJ-$VER.tar.gz $PRJ-$VER {{tag>unix coding howto}}