injunjoel has asked for the wisdom of the Perl Monks concerning the following question:
package Some_Class; use strict; our %ClassData = ( # our() is new to perl5.6 CData1 => "", CData2 => "", ); for my $datum (keys %ClassData) { no strict "refs"; # to register new methods in package *$datum = sub { shift; # XXX: ignore calling class/object $ClassData{$datum} = shift if @_; return $ClassData{$datum}; } }
package Some_Class; use strict; our %Some_Class = ( # our() is new to perl5.6 CData1 => "", CData2 => "", ); # tri-natured: function, class method, or object method sub _classobj { my $obclass = shift || __PACKAGE__; my $class = ref($obclass) || $obclass; no strict "refs"; # to convert sym ref to real one return \%$class; } for my $datum (keys %{ _classobj() } ) { # turn off strict refs so that we can # register a method in the symbol table no strict "refs"; *$datum = sub { use strict "refs"; my $self = shift->_classobj(); $self->{$datum} = shift if @_; return $self->{$datum}; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help understanding perltooc examples?
by bart (Canon) on Feb 19, 2005 at 23:17 UTC | |
|
Re: Help understanding perltooc examples?
by dragonchild (Archbishop) on Feb 19, 2005 at 22:43 UTC | |
by injunjoel (Priest) on Feb 19, 2005 at 22:58 UTC | |
|
Re: Help understanding perltooc examples?
by perlfan (Parson) on Feb 21, 2005 at 00:47 UTC |