You'd have to add _order to the member data of your object. Here, it's an anonymous list of the fields you want to be exported, in the correct order. We use a hash slice to get the fields for the join statement. (This is untested code, but you are looking for design hints so it's okay.)sub pack { my $self = shift; my $order = $self->{_order}; my $record = join(':', @$self{@$order}); return $record'; }
It's not beautiful, because it tends to break encapsulation. Perhaps you could add a method that returns the correct order, and the exportable keys. That's cleaner:
In your individual objects, you don't even have to make _order a member of the blessed hash. Use closurely-goodness to prevent anyone from messing with it, if you're so inclined:sub pack { my $self = shift; my @order = $self->order(); my $result = join(':', @$self{@order}); return $result; }
As a side note, you probably want to write your own DESTROY method, or at least escape it in your AUTOLOAD method. That can come back to haunt you later, if you extend this class:{ my @order = qw( name price description ); sub order { return @order; } }
sub DESTROY { # even if it's empty, it won't hit AUTOLOAD }
In reply to Re: Stumped on an OO Problem
by chromatic
in thread Stumped on an OO Problem
by skazat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |