Posted on 2005-02-28 22:10:28-08 by tkc in response to 13
Re: This is not your CPAN's Meta

I use Class::Meta. I'm considering making it a way of life... Thanks!

I've extended it a bit with a Constructor that calls _initialize when it's done. I've also created a MethodCheckParameter class which creates the actual method (like a constructor with create=>1). That method validates the paramters passed in and creates and instance of a special dynamically built class::meta to hold those parametets. It uses goto to make the final call which is nice because "caller" still works as expected.

Unfortunately, I had to fork Meta.pm as opposed to extend it because the add_method sub did not add the method to $classes->{<package>}->{build_meth_ord}. I filed a CR with bugs-class-meta@cpan.org (#11689) about this.

It works like this:
#!/usr/bin/perl -w use strict; package myclass; use TKC::Meta; use TKC::Meta::ConstructorInitialize; use TKC::Meta::MethodCheckParameters; use Class::Meta::Types::String qw(semi-affordance); BEGIN { my $cm = TKC::Meta-&gt;new(constructor_class =&gt; "TKC::Meta::ConstructorInitialize", method_class =&gt; "TKC::Meta::MethodCheckParameters", ); $cm-&gt;add_constructor(name=&gt;"new", create=&gt;1); my $meth = $cm-&gt;add_method(name=&gt;"mymeth"); $meth-&gt;add_parameter(name=&gt;'parameter', type=&gt;'string'); $cm-&gt;build(); } sub _initialize { my ($this) = @_; print "INITIALIZE for: ",$this,"\n"; } sub _mymeth { my ($this, $params) = @_; print "THIS : ",$this, "\n"; print "PARAMS : ",$params, "\n"; print "CALLER : ",join(", ",caller), "\n"; print "PARAMETER: ",$params-&gt;parameter, "\n"; } 1;
When called by a little program in mytest.pl:
#!/usr/bin/perl -w use strict; use myclass; my $c = myclass-&gt;new(); $c-&gt;mymeth(parameter=&gt; "this is a string"); exit(0);
You get the following output:
# perl mytest.pl INITIALIZE for: myclass=HASH(0x80f606c) THIS : myclass=HASH(0x80f606c) PARAMS : myclass::mymeth_parameters=HASH(0x81008a4) CALLER : main, mytest.pl, 6 PARAMETER: this is a string
Direct Responses: 192 | Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.