Fun with ctags

by gorthx

A while back I’d created a Perl module to hold two “odds & ends” subroutines that I used in a lot of my programs. I gave it the unfortunate name of “Misc.pm”. Of course, it lived up to its name & grew over time to contain many more functions; I should have just named it JunkDrawer.pm and been done with it. I decided it was time for a cleanup, and I split everything out into more appropriately-named modules. But then I had the problem of what to do with existing programs that referenced Misc.pm. I could have just loaded all the new modules, but that seemed messy. I had to have a way to figure out which functions each program used & thus track them back to their shiny new module. jshirley suggested I try ctags.

First, install exuberant ctags if you don’t already have it.

Make sure it supports Perl:
:::-->ctags --list-languages | grep -i perl
Perl

Then, cd to the directory containing the modules you want to tag, and then create your tags file:
:::-->ctags *.pm

Too easy. There should be a new file called “tags” in that directory – check it out. All the subs that ctags found are listed here, along with the file it found them in, including duplicates (this could bite you in the ass if you’re not paying attention; see below).

Open up the Perl program you want to edit in vim. Load your new tags file with this command:
:set tags=/path/to/tags

Now when you find a questionable function, put your cursor on top of it & type ctrl-]. This will open up the module file & take you directly to the subroutine you’re looking for. (This is where having duplicate entries in your tags file could bite you – you’re going to get the first instance in the tags file.)

Navigate back to the original file with ctrl-t or :e#.

If you have a lot of files/subroutines, you may want something a bit more robust. I had about 20, spread over 5 modules, and about 30 programs that referenced them, so tracking them through this way didn’t take me too long.

Advertisements
Tags: ,
%d bloggers like this: