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: , , ,

Monday, August 21, 2006

Personal Airships

Something I always thought would be a good idea, but after looking at this picture I'm having second thoughts. That's a mighty big piece of personal transportation.

Thursday, August 17, 2006

A little perl

I haven't posted any perl in a while(I haven't posted anything in a while).

I have an occasional need to reverse a file by lines. Usually, I would use tac, the gnu utility for that purpose, but I wanted to be able to do it with perl in a pinch. Then I found this one-liner:

perl -e 'print reverse <>' file1 file2 file3 ....

This is a *NIX style one-liner, for windows you would type:

perl -e "print reverse <>" file1 file2 file3 ....

I was suspicious of it, and didn't bother to try it for a while. The 'reverse' function is a list operator, and I thought it would load the file into memory to reverse it (for large files this is a big no-no). But, it turns out that reverse has a 'diamond-operator'(the <> above) context, and this contsruct will tell perl to zoom to the end of the file and start reading it in backwards. Very handy.

Outside of a 'one-liner' context, this construct can be used to perform operations on a file like this:

foreach (reverse(<>)) {
do some stuff to the line....

A little more video...

I've had this one saved since June, and I never got around to posting it. The Jedi Breakfast. There are a few F-Bombs in it.

UPDATE: wrong link, now it's fixed.