Two Things I Learned Today
If you’re attempting to override a Perl builtin for a unit test in the following manner:
use Test::More qw|no_plan|;
use_ok( 'MyPackage::Bill' );
{
package MyPackage::Bill;
use subs 'mkdir';
package main;
*MyPackage::Bill::mkdir = sub {
## ...whatever you want mkdir todo...
};
## ... unit tests here ...
}
You can’t place the use_ok in a BEGIN block (as is the pattern in the Test::More documentation).
Well, not 100% true. It could be in a BEGIN block, but it would have to be after the subs pragma. Meaning, it’ll work if the BEGIN block is at the end of the file. However, I find that counterintuitive, so I’m filling it away in my brain as just don’t do it.
Thinking about it, I imagine it’s likely because the pragma is only going to affect code loaded after it. The BEGIN block pushes the use from runtime to compile time, meaning it’s too late to override the builtin by the time subs kicks in.
Note this only took about two hours to figure out.
Second, if you’re on old-skool emacs users, so old your .emacs pre-dates add-hook, go through and expunge your .emacs of any setq’s of mode hooks. I finally realized the reason my add-hook to cperl-mode wasn’t working because later in the file I was explicitly setting the hook (and throwing out my earlier changes).
On the upshot, I once again have dabbrev-expand bound to the tab key.
September 7th, 2007 at 7:41 pm
nerd.
September 8th, 2007 at 5:09 pm
Too bad nobody writes perl anymore
September 8th, 2007 at 8:27 pm
No, Ben! Don’t tell me you’ve drunk the PHP kool-aid!
September 11th, 2007 at 10:20 pm
gulp gulp chug
September 11th, 2007 at 10:31 pm
noooooooooooooooooooooooo!