Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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__

In reply to Re: Autocreating Classes by fenLisesi
in thread Autocreating Classes by water

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found