A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Perl tools

Perl Needs Better Tools

IDEs

  • Padre is free, supposedly it knows more about Perl internals than other IDEs.
    • Syntax highlighting: View|Style|Evening
    • Current line highlight colour: Tools|Preferences|Appearance|Editor Current Line Background Colour
  • Eclipse is free but, seems very heavyweight. Extensible with plugins (Version control, etc. See EPIC for Perl)
  • Komodo is paid, but is explicitly designed for Perl. It has a remote debugging feature, which works! You have to install a script on the remote server, and have access to the remote fileystem to display the source code.
  • NetBeans is free
  • IntelliJ IDEA is paid, and has no Perl plugin
Text editors
  • vim - I use this a lot. It has keyword lookup (if you can configure it), a powerful but difficult-to-use scripting language, and tabs, but no debugging support. Lots of scripts/add-ons available.
  • Emacs - some of my colleagues use it, can be very powerful with add-ons/perl modules
  • UltraEdit
  • Textpad - Windows only. Nice to use, has plugins
  • Sublime - A nice text editor
Further reading

  • Why should I use an IDE?
  • TODO: [an article on coding horror or somewhere "what would a good IDE look like?"] where is it?

Catch runaway processes on Mac

#!/bin/sh

# kill runaway processes at 100% CPU usage (Mac OSX)
export PNAME=$1
if [[ ! $PNAME ]]
then
    echo "usage: runaways.sh [process name]"
    exit
fi

export PPERCENT='100.0'
SECONDS_TO_TEST=20
THRESHOLD=15

SECONDS_RUNAWAY=$(top -l $SECONDS_TO_TEST | perl -lne'BEGIN{$c=0};/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && do { $c++ };END{print$c}');
if [[ $SECONDS_RUNAWAY -gt $THRESHOLD ]]
then
    echo "killing $PNAME at $PPERCENT"%...
    PID=$(top -o cpu -l 2 | perl -lne'/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && print $1')
    if [[ $PID ]]
    then
        echo "Killing $PNAME"
        kill -9 $PID
    else
        echo "$PNAME does not exceed the threshold for a runaway"
    fi
else
    echo "$PNAME is not runaway"
fi

Combine STDOUT and STDERR

It's supposed to be done by adding 2>&1 to the end of the command, but that doesn't work for certain commands like time.

Instead make a script combine.sh containing this:
$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} ${16} ${17} ${18} ${19} ${20} 2>&1

Call it like this:
./combine.sh command_to_capture with parameters 2>&1 > outputfile 
 

Mount a directory using Samba

mount -t smbfs //host/subdir hostname -o username=yourusername,fmask=555,dmask=555,ro 
 
Don't use Samba though, use SSHFS.

XSL array/hash/lookup table

The lookup file:

<?xml version="1.0"?>
<lookup>
<location id="1769" guide="TT00379a" average="TT123456"/>
<location id="1230" guide="TT003999" average="TT000001"/>
</lookup>

The code: NOTE: $id is a local variable:


<!-- look up the country guide ID -->
<xsl:variable name="guide">
<xsl:for-each select="document('lookup.xml')">
<xsl:value-of select="key('map',$id)/@guide"/>
</xsl:for-each>
</xsl:variable>