PMotD: Devel::Deprecate

July 27, 2009

in perl

Today’s interesting Perl module of the Day:  Devel::Deprecate by Curtis “Ovid” Poe.

Back in 2000, I started teaching a class called “Perl Saves the Day: Writing Small Perl Programs to Get Out of Big SysAdmin Pinches” which was essentially a class about Perl hacks and how to use them responsibly in System Administration. One of my slides was “How to Get Rid of Hacks,” which started with:

It can be hard. You may have to wait for the rewrite.
Step one: remember you did it.

  • Go back and document the code after the crisis.
  • Send yourself mail.
  • Set up an AT job.
  • Put it in your calendar.

Now there’s an even cooler way: Devel::Deprecate. Devel::Deprecate provides a deprecate() function that let’s you write code like this (to quote the doc):

deprecate (
    reason => 'Please use the set_name() method for setting names',
    warn   => '2008-11-01',    # also accepts DateTime objects
    die    => '2009-01-01',    # two month deprecation period
);

deprecate() only comes into play when the code is run during a test (which you are writing, right?). Each time it is run under this condition (and only this condition, it never comes into play when the code is run outside of a test), it produces output that is crystal clear:

# DEPRECATION WARNING
#
# Package:     Our::Customer
# File:        lib/Our/Customer.pm
# Line:        58
# Subroutine:  Our::Customer::name
#
# Reason:      Please use the set_name() method for setting names
#
# This warning becomes FATAL on (2009-01-01)

After the due date, it blows up just as promised with an equally verbose message. Very cool.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: