Sub::Clean - Clean subroutines with an attribute


version 0.01


  use Sub::Clean;
  sub whatever :Cleaned {
    return "This cannot be called as a method";


This module 'cleans' subroutines you mark with the Cleaned attribute, preventing them being called from places they haven't been already been bound by the perl compiler.

The main advantage in this is that you can declare subroutines and use them in your code as functions, but prevent them from being called as a method. This is particularly useful if your superclass (maybe unbeknownst to you) has a method of the same name as the function which would normally be 'shadowed' by your method.

For example, this:

  package Banner;
  sub text { "example text\n" }
  sub bar  { "-"x20 . "\n" }
  sub message {
    my $class = shift;
    return $class->bar . $class->text . $class->bar
  package PubBanner;
  use base qw(Banner);
  use Sub::Clean;
  sub text { bar() . "\n" }
  sub bar :Cleaned { 
    return (("Cheers","Quark's","Moe's")[rand(3)])
  package main;
  print PubBanner->message();

Print out the right thing:


Rather than just printing three horizontal lines.

Lexical Scope

Please note that this module enables the Cleaned attribute only in lexical scope of the use Sub::Clean statement. This prevents namespace pollution.


Written by Mark Fowler <>

Copryright Mark Fowler 2010. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


No bugs have been reported.

Please report any bugs or feature requests through the web interface at


The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). isit to find a CPAN site near you, or see

The development version lives at Instead of sending patches, please fork this project using the standard git and github infrastructure.


namespace::clean allows you to clean your namespace by dividing your code up into sections that should be cleaned or not cleaned.

namespace::autoclean automatically can clean imported subroutines from other namespaces.

Lexical::Sub allows you to declare subroutines that only exist in lexical scope.

Attribute::Lexical was used to create lexical attributes.