If you want to build this yourself, you can. A class is really just a namespace. Subroutines can be exported into any namespace by assigning a subref to the typeglob. Here's a simple example:

package main; { no warnings 'once'; *main::foo = sub {print "Foo\n"} foo(); }

This will print Foo. Our assignment of a subref in this case is virtually synonymous with this:

package main; sub foo {print "Foo\n"}

...except that the typeglob version can be wielded dynamically. It can even be done in a BEGIN block. Now here is a fully functional implementation that installs a constructor, a getter, and a setter for a class that is described by a data structure.

use strict; use warnings; my %classes = ( student => { name => [qw(get set)], id => [qw(get set)], }, ); foreach my $class (keys %classes) { my $cname = ucfirst $class; $cname =~ s/_(\w)/uc $1/eg; my @data_members; foreach my $data_member (keys %{$classes{$class}}) { push @data_members, $data_member; foreach my $accessor (@{$classes{$class}{$data_member}}) { if ($accessor eq 'get') { no strict 'refs'; no warnings 'once'; print "creating sub ${cname}::${data_member}_get\n"; *{$cname . '::' . $data_member . '_get'} = sub {my $se +lf = shift; return $self->{$data_member}}; } elsif ($accessor eq 'set') { no strict 'refs'; no warnings 'once'; print "creating sub ${cname}::${data_member}_set\n"; *{$cname . '::' . $data_member . '_set'} = sub {my $se +lf = shift; return $self->{$data_member} = shift}; } else { die "Unrecognized accessor type: ${cname}::${data_memb +er}_$accessor.\n"; } } } { no strict 'refs'; no warnings 'once'; print "creating sub ${cname}::new with data members (@data_mem +bers)\n"; *{$cname . '::' . 'new'} = sub { my ($class, $args) = @_; my $self = {}; $self->{$_} = $args->{$_} for keys %$args; return bless $self, $class; }; } } # Instantiate an object of class Student: my $s = Student->new({name => 'John Doe', id => 1234}); print "\n\n"; # Use the 'get' accessors for an object of class Student: print $s->id_get, ": ", $s->name_get, "\n"; # Use one of the 'set' accessors for the object: $s->name_set('Mark Doe'); # Verify the change: print $s->id_get, ": ", $s->name_get, "\n";

The output will be:

creating sub Student::name_get creating sub Student::name_set creating sub Student::id_get creating sub Student::id_set creating sub Student::new with data members (name id) 1234: John Doe 1234: Mark Doe

Dave


In reply to Re: Building Perl classes dynamically based on an input schema/template by davido
in thread Building Perl classes dynamically based on an input schema/template by tito80

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.