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!
In reply to Stumped on an OO Problem by skazat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |