skazat has asked for the wisdom of the Perl Monks concerning the following question:
i'm trying to make an almost tranparent package i guess you could call it...
i want to make a class that can change depending on what info i give it. (this is right from perlobj)
package PRODUCT; use Carp; use strict; use vars qw($AUTOLOAD); my %fields = ( name => undef, price => undef, description => undef, ); sub new { my $product = shift; my $class = ref($product) || $product; my $self = { _permitted => \%fields, %fields, }; bless $self, $class; return $self; } sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; #strip fully-qualified portions unless (exists $self ->{_permitted} ->{$name} ) { croak "Can't access `$name' field in class $type"; } if (@_) { return $self -> {$name} = shift; }else{ return $self-> {$name}; } }
do you see how the data fields are shown right up top there? why (or how..) can i do that for other functions in the package? So instead of hardcoding:
### Pack s the current field into a comma delimited record ### and returns it sub pack { my ( $self ) = @_; my $record = join(':', $self->{name} , $self->{price}, $self->{desc +ription}); return $record; }
i could just dynamically do that using the data in %fields? that way, i could make a wholly universal package for multiple classes and i don't have to retype functions (and change them just that much) and the code would be super exportable
Can anyone help me on this?
thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stumped on an OO Problem
by chromatic (Archbishop) on Jun 27, 2000 at 22:29 UTC | |
|
Re: Stumped on an OO Problem
by ZZamboni (Curate) on Jun 27, 2000 at 22:41 UTC | |
by btrott (Parson) on Jun 27, 2000 at 22:50 UTC | |
by ZZamboni (Curate) on Jun 27, 2000 at 23:11 UTC | |
|
Re: Stumped on an OO Problem
by btrott (Parson) on Jun 27, 2000 at 22:31 UTC | |
|
Re: Stumped on an OO Problem
by skazat (Chaplain) on Jun 28, 2000 at 01:38 UTC |