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!

justin


In reply to 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.