You could make a generic method, something like this:
sub pack { my $self = shift; my $order = $self->{_order}; my $record = join(':', @$self{@$order}); return $record'; }
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.)

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:

sub pack { my $self = shift; my @order = $self->order(); my $result = join(':', @$self{@order}); return $result; }
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:
{ my @order = qw( name price description ); sub order { return @order; } }
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:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.