Compile-Time Pollution Checking

| 2 Comments

I fixed a bug in some under-tested code yesterday while writing unit tests.

package Some::Module
{
    use Modern::Perl;
    use Moose;

    # Moose attributes here

    sub some_useful_method
    {
        my ($self, $value) = @_;

        my $scrubbed_value = Some::Helper::some_function( $value );
        $self->set_value( $scrubbed_value );
    }
}

The details aren't interesting, but this is the minimal example necessary to show the problem. If you don't immediately see it, here's an alternate version of the method which would have made the error obvious:

   sub some_useful_method
    {
        my ($self, $value) = @_;

        my $scrubbed_value = some_function( $value );
        $self->set_value( $scrubbed_value );
    }

In this case, the desire not to pollute the current namespace with auxiliary functions defined elsewhere was the culprit. The solution was to add a single use Some::Helper; to the file.

Unfortunately, there was no way to catch this without writing a test which exercised the code path which triggered this call. The discipline of comprehensive testing would have caught it, as would the practice of importing functions from other packages explicitly. Couple that with namespace::autoclean to alleviate namespace pollution (thanks, Python!) and the damage is minimal, as long as everyone working on this code demonstrated sufficient discipline.

Then again, I can imagine a Perl 5 pragma which attempts to resolve all such symbols, qualified and not, at the end of compile-time and aborts the program otherwise. (Yes, there are AUTOLOAD concerns, but those are tractable.)

Sometimes a little more static typing up front can be useful.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

affiliated with ModernPerl.net

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on November 10, 2010 11:16 AM.

When You Can't Write Methods Too Small was the previous entry in this blog.

Sure It's Obsolete, but at Least It's Enterprisey! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Sponsored by Blender Recipe Reviews and the Trendshare how to invest guide

Powered by the Perl programming language

what is programming?