|
Class::Args - Write and Call methods in your style. |
Class::Args - Write and Call methods in your style.
package Some::Module; use Class::Args;
# constructor
sub new { bless {},shift };
sub hoge {
my ($self, $arg1, $arg2) = args(@_);
# You can call other functions with $self.
# Moreover, you can use $self as hash reference even
# this function was called in non object-oriented style.
if($self->{_cache}){
$rv = $self->{_cache}; # use cache
}
else{
$rv = $self->other_function($arg1, $arg2);
$self->{_cache} = $rv; # store cache
}
return $rv;
}
# in your script
use Some::Module;
my $obj = new Some::Module;
print $obj->hoge('foo', 'bar'); # ok
print Some::Module::hoge('foo', 'bar'); # ok
# same as
print Some::Module->hoge('foo', 'bar'); # ok
There's more than one way to do it!
There are object oriented style and procedural style in this world. But if you are using Class::Args, you can write methods in your favorite style and users can call it in their favorite style.
Function ``args'' was imported by default. If you don't want to import it, write as below:
use Class::Args();
and in function write as below:
sub hoge {
my ($self, $arg1, $arg2) = Class::Args::args(@_);
...
}
Instead of writing ``Class::Args::args(@_)'', you can write ``Class::Args::a(@_)''. I don't like to write long chars. This is why I named this module not ``Class::Argument'' but ``Class::Args''.
args()my ($self, $arg1, $arg2) = args(@_); # or my ($self, @args) = args(@_);
Adjust function arguments. Always function receives blessed hash reference ($self) as first argument. Class::Args creates and supplies it if first argument is not $self. Auto-created $self is cached so three calling styles as below are equal. They share same $self.
Some::Module::func('foo', 'bar');
Some::Module->func('foo', 'bar');
func('foo', 'bar'); # if func() was imported.
Of course, $self will be different if function was called in object oriented style.
$obj1->func('foo', 'bar');
$obj2->func('foo', 'bar');
Notice that auto-created $self is not initialized. It is created as an empty blessed hash reference.
a()Alias of ``args''.
I will make option which create object ( do ``new Some::Module'' ) and set it in auto-created $self instead empty hash reference.
Kagurazaka Mahito , < mahito@cpan.org >
Copyright (C) 2005 by Kagurazaka Mahito
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
|
Class::Args - Write and Call methods in your style. |