Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Object accessors with unknown data

by mbethke (Hermit)
on Jan 26, 2015 at 10:56 UTC ( [id://1114508]=note: print w/replies, xml ) Need Help??


in reply to Object accessors with unknown data

You could do something like this without any eval magic:
my $o = Anything->new(qw/foo bar baz/); $o->bar; package Anything; use strict; use warnings; sub new { my ($class, @methods) = @_; my $self = bless {}, $class; for my $method (@methods) { no strict 'refs'; *$method = sub { print "this is method $method\n" }; } return $self; }
But I'd suggest you don't. Classes like this are usually a sign of a problem that's either not analyzed properly or that doesn't benefit from solving it in an OO way. In any case it doesn't exactly make your program easier to understand or debug.

Replies are listed 'Best First'.
Re^2: Object accessors with unknown data
by Anonymous Monk on Jan 26, 2015 at 11:02 UTC

    But I'd suggest you don't. Classes like this are usually a sign of a problem that's either not analyzed properly or that doesn't benefit from solving it in an OO way. In any case it doesn't exactly make your program easier to understand or debug.

    Or hes inventing a class builder, like Mo? Ha

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-06-17 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.