Thursday, December 11, 2008

Perl

Perl is a popular text processing language that has been extended into nearly every corner of computing. It is not one of my favorite scripting languages, but there are some things it does very well. I sometimes run across Perl applications that I want to run or customize. These tips can be useful in managing Perl.

Perl is part of the default install of OS X.

Set up CPAN

perl -MCPAN -e shell

Find all Perl modules currently installed

find `perl -e 'print "@INC"'` -name '*.pm' -print

Taint mode

For untrusted scripts, add -T flag to perl at the start of the script:
#!/usr/local/bin/perl -T

One liner to edit files in place

This command edits a group of files in place. In particular, it does a global search/replace. The -p switch tells perl to iterate over filename arguments, -i tells perl to edit files in place, and -e indicates that a one line program follows.

perl -p -i -e 's/248/949/g' *.txt

One liner to edit files in place and backup each one to *.old

perl -p -i.old -e 's/248/949/g' *.txt