| 1 | package Class::Member; | ||
| 2 | |||
| 3 | use 5.008_000; | ||
| 4 | use strict; | ||
| 5 | our $VERSION='1.6'; | ||
| 6 | |||
| 7 | use Carp 'confess'; | ||
| 8 | |||
| 9 | sub import { | ||
| 10 | my $pack=shift; | ||
| 11 | ($pack)=caller; | ||
| 12 | |||
| 13 | my $getset_hash=sub : lvalue { | ||
| 14 | my $I=shift; | ||
| 15 | my $what=shift; | ||
| 16 | unless( UNIVERSAL::isa( $I, 'HASH' ) ) { | ||
| 17 | confess "$pack\::$what must be called as instance method\n"; | ||
| 18 | } | ||
| 19 | $what=$pack.'::'.$what; | ||
| 20 | if( $#_>=0 ) { | ||
| 21 | $I->{$what}=shift; | ||
| 22 | } | ||
| 23 | $I->{$what}; | ||
| 24 | }; | ||
| 25 | |||
| 26 | my $getset_glob=sub : lvalue { | ||
| 27 | my $I=shift; | ||
| 28 | my $what=shift; | ||
| 29 | unless( UNIVERSAL::isa( $I, 'GLOB' ) ) { | ||
| 30 | confess "$pack\::$what must be called as instance method\n"; | ||
| 31 | } | ||
| 32 | $what=$pack.'::'.$what; | ||
| 33 | if( $#_>=0 ) { | ||
| 34 | ${*$I}{$what}=shift; | ||
| 35 | } | ||
| 36 | ${*$I}{$what}; | ||
| 37 | }; | ||
| 38 | |||
| 39 | my $getset=sub : lvalue { | ||
| 40 | my $I=shift; | ||
| 41 | my $name=shift; | ||
| 42 | |||
| 43 | if( UNIVERSAL::isa( $I, 'HASH' ) ) { | ||
| 44 | no strict 'refs'; | ||
| 45 | no warnings 'redefine'; | ||
| 46 | *{$pack.'::'.$name}=sub:lvalue { | ||
| 47 | my $I=shift; | ||
| 48 | &{$getset_hash}( $I, $name, @_ ); | ||
| 49 | }; | ||
| 50 | } elsif( UNIVERSAL::isa( $I, 'GLOB' ) ) { | ||
| 51 | no strict 'refs'; | ||
| 52 | no warnings 'redefine'; | ||
| 53 | *{$pack.'::'.$name}=sub:lvalue { | ||
| 54 | my $I=shift; | ||
| 55 | &{$getset_glob}( $I, $name, @_ ); | ||
| 56 | }; | ||
| 57 | } else { | ||
| 58 | confess "$pack\::$name must be called as instance method\n"; | ||
| 59 | } | ||
| 60 | $I->$name(@_); | ||
| 61 | }; | ||
| 62 | |||
| 63 | foreach my $name (@_) { | ||
| 64 | if( $name=~/^-(.*)/ ) { # reserved name, aka option | ||
| 65 | if( $1 eq 'CLASS_MEMBERS' ) { | ||
| 66 | local $_; | ||
| 67 | no strict 'refs'; | ||
| 68 | *{$pack.'::CLASS_MEMBERS'}=[grep {!/^-/} @_]; | ||
| 69 | } | ||
| 70 | } else { | ||
| 71 | no strict 'refs'; | ||
| 72 | *{$pack.'::'.$name}=sub:lvalue { | ||
| 73 | my $I=shift; | ||
| 74 | &{$getset}( $I, $name, @_ ); | ||
| 75 | }; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; # make require fail | ||
| 81 | |||
| 82 | __END__ |