NAME
Rubyish::Attribute - provide ruby-like accessor builder: attr_accessor, attr_writer and attr_reader.
VERSION
version 0.04
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(\@arrayref)
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(\@arrayref)
attr_reader create only getter for the class you call it
attr_reader( [qw(name)] ) # pass an arrayref
$dogy = Animal->new({name => "rock"}) # if we write initialize function in constructor
$dogy->name() #=> rock
$dogy->name("jack") #=> undef (with warn msg)
attr_writer(\@arrayref)
attr_writer create only setter for the class you call it.
attr_writer( [qw(name)] ) # pass an arrayref
$dogy = Animal->new()->name("lucky") # initialize and set and get instance itself
$dogy->name("jack") #=> instance itself
$dogy->name #=> undef (with warn msg)
DEPENDENCE
SEE ALSO
Sub::Exporter, autobox::Core, List::Rubyish
http://ruby-doc.org/core-1.8.7/classes/Module.html#M000423
http://chupei.pm.org/2008/11/rubyish-attribute.html chinese introduction
AUTHOR
shelling <navyblueshellingford at gmail.com>
gugod <gugod at gugod.org>
acknowledgement
Thanks to gugod providing testing script and leading me on the way of perl
REPOSITORY
host: http://github.com/shelling/rubyish-attribute/tree/master
checkout: git clone git://github.com/shelling/rubyish-attribute.git
BUGS
please report bugs to <shelling at cpan.org> or <gugod at gugod.org>
COPYRIGHT & LICENCE
Coryright © 2008 shelling, gugod, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 201:
Non-ASCII character seen before =encoding in '©'. Assuming UTF-8