Perl (Pete's notes)

Changing a string in multiple files

Do, like,
perl -i -p -e 's/contacts.html/contacts.shtml/g' *.html

Installing Perl modules: General

There are 3 things that will make your use of CPAN better:

  1. Become root. Installation of modules fail in weird ways if you don't do this. I've also had it succeed - I don't know the "right" answer here. It always works if you're root.
  2. Set the environment variable named FTP_PASSIVE to 1. This will allow CPAN to use FTP in passive mode instead of just hanging. Better, put this in your .bashrc file:
    export FTP_PASSIVE=1
    				
  3. Start X11. Some CPAN modules, like Tk, run tests that require X to be running. If it's not, you'll get "couldn't connect to display ":0"".

Under Mac OS X, Perl is already installed, with several modules in various places. It's not clear how to cleanly add more modules in a CPAN-like way, so I created a fresh CPAN repository in ~/root/cpan. This will cause duplicate packages to be loaded, but I don't know a better way. Sigh. Once done, you have to tell the installed Perl to add your CPAN modules to the @INC variable, with something like

export PERL5LIB=/Users/siemsen/.cpan/build/Tk-804.027
		
This almost worked - something is still screwy - it couldn't find Event.pm.

You can load modules from the CPAN 3 ways:

  1. Using the CPAN.pm module. This is best because it follows dependencies.
  2. With RPMs. Second best because there may not be RPMs for the modules you want.
  3. Manually. Third best because there is no chance of following dependencies.

Installing Perl modules with CPAN.pm (best way)

Use the CPAN.pm module. To read about it, do "perldoc CPAN", or in XEmacs use the Perldoc pull-down when you're editing a Perl file.

The first time you use CPAN.pm, it will ask a long series of questions, the answers for which can be found below. Don't answer them until you've installed ncftp on the local machine.

If you've already installed CPAN and just want to use it, do like
(as root)
(sudo) perl -MCPAN -e shell
install Log::Log4perl
install HTML::TokeParser::Simple
h
q
	

The above will install Log4perl in /usr/lib/perl5/site_perl/5.6.1/Log/Log4perl.

I've had it happen that after a successful install like the above (no failure messages during the install) a Perl program still coudn't find the module. The .pm file was there, and in a directory listed in Perl's INC paths, but I still got the "Can't locate xyz.pm in @INC" message. It turned out that some of the intermediate directories above the .pm file were protected without 'x', so I didn't have permissions to read the directories. To fix it at the time, I did

(as root)
find /usr/local/lib/site_perl -type d -exec chmod +x {} \;
		

The CPAN.pm module will store modules in, like, /usr/local/lib/perl5/site_perl/5.8.4, with your version of perl. If you run CPAN.pm with a different version of perl, modules will get stored in a different place.

The CPAN module stores information about what's installed in a .cpan directory. Be careful about having multiple such directories laying around. Ideally, you want one of them on your machine.

Some Perl modules contain C code. When you install them, they'll try to compile the code. On Solaris, by default, you may try to use cc, which on Solaris is /usr/ucb/cc, which is a script, not a C compiler, and will fail miserably. I had success replacing /usr/ucb/cc with a link to gcc, but the more elegant solution is ...?

The first time you use CPAN.pm, it will ask a long series of questions. A couple of them are very important:

If CPAN doesn't work or works very slowly, it may be because it can't use LWP or Net::FTP through the firewall. Just wait for it to timeout tryng those two methods, and it'll then try ncftpget, which hopefully will work. To make this process more bearable, reduce the list of mirror sites to just one so you dan't have to wait through lets of timeouts.

To reconfigure CPAN, do

cpan> o conf init
		

Modules to install include

Installing Perl modules with RPM (2nd best way)

If you can, find the RPM for the module you want. For instance, to install the Log::Agent module, I did did

rpm -i perl-Log-Agent-0.303-8.noarch.rpm
		

Installing Perl modules manually (3rd best way)

For example, web to the CPAN. Get p5-Palm-1.2.4.tar.gz. Save it in /usr/src.

			cd /usr/src
			gunzip p5-Palm-1.2.4.tar.gz
			tar xf p5-Palm-1.2.4.tar
			rm p5-Palm-1.2.4.tar
			cd p5-Palm-1.2.4
			perl Makefile.PL
			make
			make install
			make clean
			chown -R siemsen /usr/src/p5-Palm-1.2.4
			chgrp -R datacomm /usr/src/p5-Palm-1.2.4
		

Installing Net::SNMP and SNMP::Info

This describes what I did to install SNMP::Info on netserver.

SNMP::Info is layered on top of Net::SNMP, so you first have to install Net::SNMP. When you use apt-get to install the "snmp" package, it'll install the "libsnmp-perl" package, which is the Perl piece of Net::SNMP. This should install SNMP.pm into your Perl system, into, like, /usr/local/share/perl/5.8.4/Net/SNMP.pm. You can then happily use Net::SNMP.

There is no Debian package for SNMP::Info, so you have to install it manually.

as root
cd SNMP-Info-1.04
perl Makefile.PL
make
make install
make clean
I still had problems. Sigh.

Logging with log4perl

Log4perl and log4j provide a de-facto standard way to output messages from Perl and Java programs. I use them in my own standard way to provide a logging framework that meets almost all of my needs. My standard way to use them is described in my Logging page.

using the Palm module

To run a sample program that displays the contents of my backed-up Address Book database, do
/usr/src/p5-Palm-1.2.4/util/pdbdump -nohex ~/pilot/Backup/AddressDB.pdb
To learn about the modules, read, the Palm::PDB doc page. To look at the same information as text from within XEmacs, do this:
M-! pod2text /usr/src/p5-Palm-1.2.4/Palm/PDB.pm

Pete Siemsen
Last modified: Fri Jan 12 18:14:13 MST 2007