Ensure instance and class methods are called properly
Perl
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/Method
t
.gitignore
Changes
README.mkdn
dist.ini
perlcritic.rc
weaver.ini

README.mkdn

NAME

Method::Assert - Ensure instance and class methods are called properly

VERSION

version 0.0.1

SYNOPSIS

package MyClass;

use Method::Assert;
use Carp qw(confess);

sub new {
    my ($class, @args) = &class_method;
    my $self = {};
    bless $self, $class;
    $self->_init(@args);
    return $self;
}

sub _init {
    my ($self, @args) = @_; # Perl Critic prefers it this way
    &instance_method;       # still works
    ...
}

sub get_output_filename {
    &instance_method;
    return shift->{'output_filename'};
}

sub set_output_filename {
    my $self = &instance_method;
    confess("No parameter specified") unless @_;
    confess("File already exists") if -e $_[0];
    $self->{'output_filename'} = $_[0];
    return $self;
}

DESCRIPTION

This module will export the two functions named below into the namespace of the package using it. These two functions are useful to do typical checks at the start of functions that are supposed to be either class or instance methods.

Always remember to call these functions as &class_method and &instance_method, or else they will not work properly!

If you call them as class_method() or instance_method() a new version of @_ will be initialized, and manipulation of @_ will not work properly.

FUNCTIONS

class_method

Use this function to check that the sub is called as a class method. If the sub is called as a function, or as an instance method this function will die.

If called in scalar context, will shift of the first argument of the @_ array and return that value. If called in list or void context it will not change @_.

instance_method

Use this function to check that the sub is called as an instance method. If the sub is called as a function, or as a class method this function will die.

If called in scalar context, will shift of the first argument of the @_ array and return that value. If called in list or void context it will not change @_.

CAVEATS

The two functions can NOT be called by the fully qualified method name because they are generated as closures in the calling package's namespace during import(). Writing use Method::Assert () will cause import() not to be executed, which doesn't not make sense. See the section on import in perldoc -f use for more information.

SEMANTIC VERSIONING

This module uses semantic versioning concepts from http://semver.org/.

SEE ALSO

SUPPORT

Perldoc

You can find documentation for this module with the perldoc command.

perldoc Method::Assert

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.

  • Search CPAN

The default CPAN search engine, useful to view POD in HTML format.

http://search.cpan.org/dist/Method-Assert

  • RT: CPAN's Bug Tracker

The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Assert

  • AnnoCPAN

The AnnoCPAN is a website that allows community annonations of Perl module documentation.

http://annocpan.org/dist/Method-Assert

  • CPAN Ratings

The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.

http://cpanratings.perl.org/d/Method-Assert

  • CPAN Forum

The CPAN Forum is a web forum for discussing Perl modules.

http://cpanforum.com/dist/Method-Assert

  • CPANTS

The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.

http://cpants.perl.org/dist/overview/Method-Assert

  • CPAN Testers

The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.

http://www.cpantesters.org/distro/M/Method-Assert

  • CPAN Testers Matrix

The CPAN Testers Matrix is a website that provides a visual way to determine what Perls/platforms PASSed for a distribution.

http://matrix.cpantesters.org/?dist=Method-Assert

  • CPAN Testers Dependencies

The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

http://deps.cpantesters.org/?module=Method::Assert

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-method-assert at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Assert. You will be automatically notified of any progress on the request by the system.

Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)

http://github.com/robinsmidsrod/Method-Assert

git clone git://github.com/robinsmidsrod/Method-Assert.git

AUTHOR

Robin Smidsrød robin@smidsrod.no

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Robin Smidsrød.

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