Once while writing this node about object properties I got the idea of specifying every aspect of an object programmatically by saving the lexical state information in a blessed closure! This might be useful in implementing an AbstractClassFactory pattern.

The GenericObject imports all of its properties and methods and saves it as state information inside its blessed closure. The constructor is called with 3 arrayrefs; an arrayref of property names, and 2 arrayrefs that map method-names to code-refs. Some notes:

package GenericObject; use strict; use warnings; use Carp; use Hash::Util qw/lock_keys lock_hash/; sub create { my $class = shift; $class = ref($class) || $class; my $prop_names = shift; my $method_names = shift; my $code_refs = shift; my (%properties, %methods); @properties{ @$prop_names } = map { '' } (0..$#$prop_names); lock_keys(%properties); @methods{ @$method_names } = @$code_refs; lock_hash(%methods); my $closure = sub { my $magick = shift; # first resolve any property sets or gets if (exists $properties{ $magick }) { $properties{ $magick } = shift if @_; return $properties{ $magick }; } # next resolve any method calls, making # sure to include a ref to the %properties hash elsif (exists $methods{ $magick }) { return &{ $methods{ $magick } }(\%properties, @_); } else { croak "Magick $magick not defined"; } }; return bless ($closure, $class); } sub incant { my $self = shift; return &{ $self }(@_); } 1; #file: test_object.pl #!/usr/local/bin/perl use strict; use warnings; use GenericObject; use Carp; my @props = qw/red green blue/; my @method = qw/getHexRGB print_colors init/; my @code_ref = (\&getHexRGB, \&print_colors, \&init); sub getHexRGB { my $props = shift; return sprintf("%02X%02X%02X", $props->{red}, $props->{green}, $prop +s->{blue}); } sub init { my $props = shift; my %colors = @_; for (keys %$props) { $props->{$_} = $colors{$_}; } } sub print_colors { my $props = shift; print "\nColor table\n"; for (keys %$props) { print "Color $_\t\t", $props->{$_}, "\n"; } } my $genobj = GenericObject->create([@props], [@method], [@code_ref]); $genobj->incant('init', red=>19, green=>255, blue=>9); $genobj->incant('print_colors'); print "The colorref of \$genobj is\n", $genobj->incant('getHexRGB'), " +\n"; print "\nChanging red to ", $genobj->incant('red', 55), "\n"; $genobj->incant('print_colors'); print "The colorref of \$genobj is\n", $genobj->incant('getHexRGB'), " +\n";

So, there you have it. A completely generic object generated at run-time.


In reply to AbstractClassFactory by hypochrismutreefuzz

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.