NAME Rubyish::Attribute - provide ruby-like accessor builder: attr_accessor, attr_writer and attr_reader.
VERSION version 0.01
SYNOPSIS #!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
{
package Animal;
use Rubyish::Attribute qw(:all); # use :all to import attr_accessor, attr_writer and attr_reader
attr_accessor( [qw(name color type)] );
# pass a arrayref as the only one parameter
# then create a constructer based on hashref
sub new {
$class = shift;
bless {}, $class;
}
1;
}
$dogy = Animal->new()->name("rock")->color("black")->type("unknown"); # new Animal with three attribute
say $dogy->name; #=> rock
say $dogy->color; #=> black
say $dogy->type; #=> unknown
FUNCTIONS
attr_accessor attr_accessor provides getters double as setters. Because all setter return instance itself, now we can manipulate object in ruby way more than ruby.
attr_accessor( [qw(name color type master)] )
$dogy = Animal->new()->name("lucky")->color("white")->type("unknown")->master("shelling");
Each attribute could be read by getter as showing in synopsis.
attr_reader attr_reader create only getter for the class you call it
attr_reader( [qw(name)] ) # pass arrayref
$dogy = Animal->new({name => "rock"}) # if we write initialize function in constructor
$dogy->name() #=> rock
$dogy->name("jack") #=> cause an exception.
attr_writer attr_writer create only setter for the class you call it.
attr_writer( [qw(name)] ) # pass arrayref
$dogy = Animal->new()->name("lucky") # initialize and set and get instance itself
$dogy->name("jack") # rename it and get the instance itself
$dogy->name # undef