Class::Member::Dynamic - A module to make the module developement easier


Class-Member documentation Contained in the Class-Member distribution.

Index


Code Index:

NAME

Top

Class::Member::Dynamic - A module to make the module developement easier

SYNOPSIS

Top

 package MyModule;
 use Class::Member::Dynamic qw/member_A member_B/;

DESCRIPTION

Top

See Class::Member.

AUTHOR

Top

Torsten Foertsch <Torsten.Foertsch@gmx.net>

COPYRIGHT

Top


Class-Member documentation Contained in the Class-Member distribution.

package Class::Member::Dynamic;

use strict;
our $VERSION='1.6';

use Carp 'confess';

sub import {
  my $pack=shift;
  ($pack)=caller;
  my $dummy;

  my $getset=sub : lvalue {
    my $I=shift;
    my $what=shift;
    my $rc=\$dummy;

    if( UNIVERSAL::isa( $I, 'HASH' ) ) {
      $what=$pack.'::'.$what;
      if( $#_>=0 ) {
	$I->{$what}=shift;
      }
      $rc=\$I->{$what};
    } elsif( UNIVERSAL::isa( $I, 'GLOB' ) ) {
      $what=$pack.'::'.$what;
      if( $#_>=0 ) {
	${*$I}{$what}=shift;
      }
      $rc=\${*$I}{$what};
    } else {
      confess "$pack\::$what must be called as instance method\n";
    }
    $$rc;
  };

  foreach my $name (@_) {
    if( $name=~/^-(.*)/ ) {	# reserved name, aka option
      if( $1 eq 'CLASS_MEMBERS' ) {
	local $_;
	no strict 'refs';
	*{$pack.'::CLASS_MEMBERS'}=[grep {!/^-/} @_];
      }
    } else {
      no strict 'refs';
      *{$pack.'::'.$name}=sub:lvalue {
	my $I=shift;
	&{$getset}( $I, $name, @_ );
      };
    }
  }
}

1;

__END__