Wednesday, August 30, 2006

Vim user tip

This post ported from a test blog called 'nodding off' that I deleted. The timestamp is from the original post

Anyway, here's a little Perl hacker/vim user tip.

If you like using Perl's SelfLoader module, then you will need to have a defined __DATA__ marker in your file. This is what tells the Perl compiler to stop parsing, but allows the SelfLoader module to read in the rest for loading. When editing such a file(module) in vim, you will notice sadly that the code below the __DATA__ marker is all blue'd out, because typically this area doesn't have code in it. To fix this, you need to comment out the __DATA__ marker (like this #__DATA__), but you need to make sure you uncomment before you save. The following settings in your vimrc(gvimrc) file will do the trick:

au BufWritePre *.pm :%s/^#__DATA__ *$/__DATA__/e
au BufWritePost *.pm :%s/^__DATA__ *$/#__DATA__/e

The au(autocmd) verb assigns the actions (the substitutions: :%s....) to *.pm(module) files whenever the buffer is written. The BufWritePre occurs before the file is saved, the BufWritePost occurs after the file has been written. In this way, the __DATA__ marker is uncommented just before writing, and re-commented just after. The user doesn't even perceive that the buffer was changed, but the file ALWAYS is saved with the __DATA__ marker uncommented, making sure that SelfLoader works correctly.

Labels: , , ,

0 Comments:

Post a Comment

<< Home