Class::Member::GLOB

Class::Member::GLOB is a module to make the module developement easier.
Download

Class::Member::GLOB Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Torsten Fortsch
  • Publisher web site:
  • http://search.cpan.org/~cmungall/

Class::Member::GLOB Tags


Class::Member::GLOB Description

Class::Member::GLOB is a module to make the module developement easier. Class::Member::GLOB is a module to make the module development easier.SYNOPSIS package MyModule; use Class::Member::HASH qw/member_A member_B -CLASS_MEMBERS/; or package MyModule; use Class::Member::GLOB qw/member_A member_B -CLASS_MEMBERS/; or package MyModule; use Class::Member qw/member_A member_B -CLASS_MEMBERS/; or package MyModule; use Class::Member::Dynamic qw/member_A member_B -CLASS_MEMBERS/;Perl class instances are mostly blessed HASHes or GLOBs and store member variables either as "$self->{membername}" or "${*$self}{membername}" respectively. This is very error prone when you start to develope derived classes based on such modules. The developer of the derived class must watch the member variables of the base class to avoid name conflicts. To avoid that "Class::Member::XXX" stores member variables in its own namespace prepending the package name to the variable name, e.g. package My::New::Module; use Class::Member::HASH qw/member_A memberB/; will store "member_A" as "$self->{'My::New::Module::member_A'}". To make access to these members easier it exports access functions into the callers namespace. To access "member_A" you simply call. $self->member_A; # read access $self->member_A($new_value); # write access $self->member_A=$new_value; # write access (used as lvalue) "Class::Member::HASH" and "Class::Member::GLOB" are used if your objects are HASH or GLOB references. But sometimes you do not know whether your instances are GLOBs or HASHes (Consider developement of derived classes where the base class is likely to be changed.). In this case use "Class::Member" and the methods are defined at compile time to handle each type of objects, GLOBs and HASHes. But the first access to a method redefines it according to the actual object type. Thus, the first access will last slightly longer but all subsequent calls are executed at the same speed as "Class::Member::GLOB" or "Class::Member::HASH". "Class::Member::Dynamic" is used if your objects can be GLOBs and HASHes at the same time. The actual type is determined at each access and the appropriate action is taken. In addition to member names there is (by now) one option that can be given: "-CLASS_MEMBERS". It lets the "import()" function create an array named @CLASS_MEMBERS in the caller's namespace that contains the names of all methods it defines. Thus, you can create a contructor that expects named parameters where each name corresponds to a class member: use Class::Member qw/member_A member_B -CLASS_MEMBERS/; our @CLASS_MEMBERS; sub new { my $parent=shift; my $class=ref($parent) || $parent; my $I=bless {}=>$class; my %o=@_; if( ref($parent) ) { # inherit first foreach my $m (@CLASS_MEMBERS) { $I->$m=$parent->$m; } } # then override with named parameters foreach my $m (@CLASS_MEMBERS) { $I->$m=$o{$m} if( exists $o{$m} ); } return $I; } Requirements: · Perl


Class::Member::GLOB Related Software