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.
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 @_.
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/.