Rubyish-Attribute

 view release on metacpan or  search on metacpan

lib/Rubyish/Attribute.pm  view on Meta::CPAN

package Rubyish::Attribute;
use 5.010;

=head1 NAME

Rubyish::Attribute - ruby-like accessor builder: attr_accessor, attr_writer and attr_reader.

=cut

use Want;

sub import {
  my $caller = caller;
  for (qw(attr_accessor attr_reader attr_writer)) {
    *{$caller . "::" . $_} = *{$_};
  }
  eval qq{package $caller; use PadWalker qw(peek_my);};
}


=head1 VERSION

This document is for version 1.2

=cut

our $VERSION = "1.2";

=head1 SYNOPSIS

    #!/usr/bin/env perl
   
    use 5.010;

    use strict;
    use warnings;

    {
        package Animal;
        
        use Rubyish::Attribute; 
        # import attr_accessor, attr_writer and attr_reader

        BEGIN {
          attr_accessor "name", "color", "type"; 
        }
        # pass a list as the only one parameter
        # invoke it in compile time to avoid using parenthesis when using instance variable as below

        # then create a constructer based on hashref
        sub new {
            $class = shift;
            bless {}, $class;
        }

        sub rename_as {
          my ($self, $new_name) = @_;
          __name__ = $new_name;

          # __name__ is accurately a lvalue subroutine &__name__() which refer to $self->{name}
          # now it looks like a instance variable.
        }

        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

=head1 FUNCTIONS

=head2 attr_accessor(@list)



( run in 1.202 second using v1.01-cache-2.11-cpan-e1769b4cff6 )