Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Autocreating Classes

by fenLisesi (Priest)
on Oct 07, 2006 at 12:46 UTC ( #576835=note: print w/ replies, xml ) Need Help??


in reply to Autocreating Classes

eval came to my mind (I hope I am not totally missing the point here)

StaticMammal.pm (The equivalent of your A):

#!/usr/bin/perl -wT package StaticMammal; use strict; use warnings; sub new { my ($class) = @_; my $new_critter = bless {}, $class; return $new_critter; } sub walk { my ($self) = @_; print "A " . ref( $self ) . " walks " . $self->walks_how() . "\n"; return; } sub talk { my ($self) = @_; print "A " . ref( $self ) . " says " . $self->talks_how() . "\n"; return; } sub walks_how { return q(ponderously); } sub talks_how { return q(Grrr); } 1; __END__
DynamicMammals.pm (The middleman that generates the equivalent of your Bn):
#!/usr/bin/perl -wT package DynamicMammals; use strict; use warnings; use lib '.'; my %style_of = ( Cat => { talks => q(meow), walks => q(in beauty, like the night), }, Dog => { talks => q(woof), }, FrankenMammal => { ## empty hash }, ); my $package_str = q(); foreach my $mammal (keys %style_of) { $package_str .= qq( package Dynamic$mammal; use base qw(StaticMammal); ); my $action_href = $style_of{ $mammal }; foreach my $action (keys %$action_href) { $package_str .= sprintf q( sub %s_how { return q(%s); } ), $action, $action_href->{ $action}; } } eval $package_str; if (my $msg = $@) { die "Eval error: $msg\nwhile trying to eval:\n$package_str"; } 1; __END__
Something that calls the equivalent of your Bn:
#!/usr/bin/perl -wT use strict; use warnings; use lib '.'; use DynamicMammals; my $felix = DynamicCat->new(); my $spot = DynamicDog->new(); my $thing = DynamicFrankenMammal->new(); foreach my $critter ($felix, $spot, $thing) { $critter->walk(); $critter->talk(); } __END__


Comment on Re: Autocreating Classes
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://576835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-04-20 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls