rje has asked for the wisdom of the Perl Monks concerning the following question:
I can now do neato things like this:package TEST; use strict; ############################################## # # A generic object with fields specified # at creation time. # ############################################## my %data; sub new { my $self = bless [], shift; my @required_fields = @_; foreach my $field (@required_fields) { chomp $field; my $eval = "sub $field : lvalue { \$data{+shift}->{$field}; }"; eval $eval; } return $self; } sub DESTROY { delete $data{+shift}; } 1;
-Robmy $test = new TEST qw( foo bar baz ); $test->foo = 3; $test->bar = "Boy is this a messed-up object!"; $test->baz = "I bet someone else has a better way to do this.";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Automagic subroutines
by borisz (Canon) on Jan 03, 2005 at 23:40 UTC | |
Re: Automagic subroutines
by osunderdog (Deacon) on Jan 04, 2005 at 00:07 UTC | |
Re: Automagic subroutines
by revdiablo (Prior) on Jan 04, 2005 at 01:13 UTC | |
Re: Automagic subroutines
by Limbic~Region (Chancellor) on Jan 04, 2005 at 16:54 UTC | |
by rje (Deacon) on Jan 05, 2005 at 16:32 UTC | |
Re: Automagic subroutines
by Courage (Parson) on Jan 04, 2005 at 13:34 UTC |