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.

5 Responses to “Two Things I Learned Today”

  1. rick Says:

    nerd.

  2. ben Says:

    Too bad nobody writes perl anymore

  3. wdr1 Says:

    No, Ben! Don’t tell me you’ve drunk the PHP kool-aid!

  4. ben Says:

    gulp gulp chug

  5. wdr1 Says:

    noooooooooooooooooooooooo!

Leave a Reply