2 presentation_topic: meta meta meta meta
3 presentation_title: Object Meta Programming
4 presentation_place: YAPC::EU::2007
5 presentation_date: Aug 29, 2007
8 {image:http://personal.inet.fi/taide/junttila/desert/2002/promopi.jpg}
12 {image: http://www.prints.co.nz/Merchant2/graphics/00000001/7104_Squares_with_Concentric_Circles_Kandinsky_Wassily.jpg}
15 {image: http://www.room101.net/door.jpg}
17 +* writing /code/ which /outputs/ or /manipulates/ /code/
20 {image: http://www.internationalhero.co.uk/m/manimal.jpg}
29 {image: http://www.junjan.org/weblog/images/Flying_Spaghetti_Monster-thumb.jpg}
30 +* Home grown snippets
31 +* Home grown packages
32 +* Stuff on the CPAN (e.g. `Class::Accessor`, `Code::Perl`)
33 +* Large systems (`Template::Toolkit`)
36 {image: http://www.evc.org/programs/TD_web_projects/westside_amy_2003_1/teen_times/peerpressure/images/16_peer_pressure_smoking.gif}
37 * You should already know it
39 +* That's the intended audience anyway ;-)
41 = Object Meta Programming
43 = Object Meta Programming
44 +* Code that outputs or manipulates /object oriented code/
45 +* Often written in OO
48 {image: http://www.wildoats.com/content/ApplePieSlice.jpg}
49 +* `Class::Accessor`, `Class::InsideOut`
50 +** Generates accessor methods
51 +* `Class::Prototyped`
52 +** Prototype object support in Perl
53 +* Lots of stuff on the CPAN
56 {image: http://www.nordicsubs.co.uk/covers/vogue.jpg}
57 +* High level object meta programming
59 +** The picture illustrates an object modelling with class =)
60 +** Seriously though...
63 {image: http://www.nordicsubs.co.uk/covers/vogue.jpg}
64 * High level object meta programming
79 {image: http://www.nordicsubs.co.uk/covers/vogue.jpg}
96 method distance_to { ... }
100 = Modeled with objects
107 Attribute->new( name => '$x' ),
108 Attribute->new( name => '$y' ),
112 name => "distance_to",
113 definition => sub { ... }
120 {image: http://www1.istockphoto.com/file_thumbview_approve/944779/2/istockphoto_944779_reception_bell.jpg}
122 +** Introspection/Reflection
125 +** Class transformation
129 {image: http://www.iinteractive.com/moose/images/a_moose.gif}
131 +* A deep meta object system
134 +** Custom metaclasses
139 {image: http://www.mileslight.com/images/perl-camel.gif}
141 +** Class = Package = Symbol table hash
142 ** Inheritence tree embedded in `@ISA` entry
143 ** `bless` links data to a class
147 +* Written in C (not very accessible)
150 {image: http://nothingmuch.woobling.org/MOP.jpg}
151 +* port of CLOS, more or less
152 +* MOP = Meta Object Protocol
155 {image: http://nothingmuch.woobling.org/MOP.jpg}
157 +** `Class::MOP::Class`
158 ** `Class::MOP::Method`
159 ** `Class::MOP::Attribute`
165 {image: http://nothingmuch.woobling.org/MOP.jpg}
166 +* Can "parse" packages into meta objects
167 +* Modifying the objects writes back to packages
169 +** Accessors from attributes
170 +** Constructor from attributes
173 = Moose's custom metaclasses
174 {image: http://www.iinteractive.com/moose/images/a_moose.gif}
175 +* Subclasses of `Class::MOP::Class` et al
178 +** `BUILD` etc a la Perl 6
182 {image: http://nothingmuch.woobling.org/sugar.jpg}
183 +* Pseudo-declarative syntax
184 +* Maps to metaclass manipulations
186 has foo => ( is => "rw" );
190 $metaclass->add_attribute(
191 Moose::Meta::Attribute->new(
192 foo => ( is => "rw" ),
197 = Moose vs. Class::Accessor
198 {image: http://nothingmuch.woobling.org/p-seal-at-toronto-zoo.jpg}
199 +* All that bloat just for the `has` syntax?
204 {image: http://www.threadpit.com/photos/all_styles/small/401.jpg}
205 +* Packaged meta code
210 {image: http://www.threadpit.com/photos/all_styles/small/401.jpg}
212 +** Customizable serialization through metaprogramming
213 +* `MooseX::AttributeHelpers`
214 +** Additional methods for collection type attributes
216 +** Additional constructor compiles attributes into a `Getopt` spec
218 +** Inversion of control integrated into the object system
221 {image: http://nothingmuch.woobling.org/cute_moose.jpg}
222 +* OO is less tedious
223 +* Helps you write meta code
224 +** Good APIs promote clean code
225 +** Easier to build on existing base
226 +** Conventions and structure let you play well with others (`MooseX::`)
227 +** Introspectable & tranformable metamodel
230 {image: http://www.rockies-ice.com/images/MainHeaderPic_5.gif}
233 +* Not even fun like the picture
236 {image: http://www.agiledata.org/images/legacyDataSources.jpg}
238 +** Tables <-> Classes
239 +** Fields <-> Attributes
243 +** Relationship fetchers
246 +* Meta code is in the base class
247 +* No clear schema modelling
248 +* No separation between regular & meta code
254 +** Schema objects fully model the SQL side
255 +** ResultSource etc partially model the OO side
256 +** Components for everything
262 +** Code generation (SQL, Perl)
264 = ORM related meta programming
269 {image: http://www.nzgeothermal.org.nz/geothermal_energy/images/big/b-silencer-water-vapour.jpg}
272 {image: http://www.nzgeothermal.org.nz/geothermal_energy/images/big/b-silencer-water-vapour.jpg}
275 +** Stevan says it's Moose 2.0's
276 +* Perl 5 & Haskell (in pugs)
279 {image: http://www5f.biglobe.ne.jp/~tantan-kids/I.syokuzai/lollypop.jpg}
281 +* Boring parts messy or unwritten
282 +* Lacking integration, sugar layer
283 +** mst promised to help ;-)
284 +* Some parts slow as $*!&%
287 {image: http://nothingmuch.woobling.org/rocks.jpg}
289 +* Very suited for meta transformations
290 +* Fine grained control over everything
291 +* Can introduce entirely new conceptions of OO
294 {image: http://www.narrabay.com/images/engineering_large.jpg}
296 +** Corresponds to compilation
298 +** Corresponds to runtime
301 {image: http://www.narrabay.com/images/engineering_large.jpg}
302 +* Compiler/sugar layer creates the modeling layer
303 +** Class objects are constructed, with all the details
304 +** No meta calculations happen yet
305 +* Modeling layer is "compiled" into responder layer
306 +** Can be done on demand or upfront
307 +* Obscurely named objects
311 {image: http://gallery.hd.org/_exhibits/light/_more2003/_more05/light-bulb-glowing-filament-light-blue-uncropped-lores-3-AHD.jpg}
312 +* The purest form of OO is prototypes
313 +** prototype OO can implement class OO
314 +* Concepts are new ways to express objects
315 +* A class is a concept
316 +* A role is a concept
318 = "Responder Interfaces"
319 {image: http://www.cs.usfca.edu/~parrt/course/652/lectures/images/vtable.jpg}
320 +* `$ri->dispatch( $responder, $invocation )`
321 +* Generated from concepts
323 +** The flattened method hierarchy
324 +** ... or something completely different
329 {image: http://www.maine.gov/spo/flood/images/fema_seal.gif}
330 +* Something that is the subject of invocations
331 +** An object instance
332 +** A class (class methods)
333 +* A simple tuple `( $data, $ri )`
336 {image: http://www.mobilenews.sk/wp-content/lg-banana.jpg}
337 +* A method call, multimethod, message... whatever
339 +* Whatever an RI will put up with
340 +** Extensible calling semantics
342 = MO compilation flow
343 {image: http://www.daveltd.com/photo/rolls/digital/water/waterfalls/snoqualmie-falls/snoqualmie-falls-flowing-3811-equalized.jpg}
344 +* Instantiate a `Class` object
345 +** Specify members (methods, attributes)
346 +** ... and ancestor classes & roles
348 +** `my $class_methods_ri = $class->class_interface()`
349 +** Instance method RI closed in the constructor, within `$ri`
350 +** Purely functional operation
353 {image: http://www.daveltd.com/photo/rolls/digital/water/waterfalls/snoqualmie-falls/snoqualmie-falls-flowing-3811-equalized.jpg}
354 +* Compute instance methods and attributes from ancestry
355 +* Compute instance slots from attributes
356 +* Generate accessors
358 MO::Run::ResponderInterface::MethodTable->new(
362 +* Generate constructor
365 my $data = process_params(@_);
366 return box( $data, $instance_ri );
371 {image: http://d6044171.u109.worldispnetwork.com/images/Creation-hands-L.jpg}
372 +* Lookup RI using class name
373 +* Dispatch constructor class method
374 +* Compose data from params
375 +** Slightly complicated
376 +* Link data with closed `$instance_ri`
377 +** Responder == ( Data, RI )
381 {image: http://www.w3.org/2005/Talks/05-maxf-xtech/telephone.jpg}
382 +* How to talk with your new object?
384 my $ri = $responder->responder_interface;
387 + my $method = MO::Run::Invocation::Method->new(
393 + $ri->dispatch( $responder, $method );
395 +* Arbitrary responder interfaces also allowed
396 +** Doesn't have to be `$responder->responder_interface`
399 {image: http://www.encyclopediadramatica.com/images/f/f5/Surprise-buttsecks.jpg}
400 +* What's wrong with the previous slide?
401 +* RIs are objects too!
402 +* Need to bootstrap method calls
403 +* Runtime must bootstrap low level OO
404 +** More on this soon
406 = Meta level polymorphism
407 {image: http://www.worth1000.com/entries/44500/44790qV87_w.jpg}
408 +* An important point
409 +** In fact, *the* point of MO
410 +* Responder interfaces are polymorphic
411 +** Method table, network proxy, whatever
412 +* Easy to specialize
415 +* Easy to mix several OO systems
416 +** Just compile to separate RIs
419 {image: http://www.vetaid.org/assets/shop/card-camel-108.jpg}
422 +* `MO::Run::Aux` wraps both
423 +** Share tests by running with different `%ENV` var
426 {image: http://www.philnjill.com/collections/kubricks/kub_alien/S2Secret.jpg}
427 +* Nested object system
428 +* Bootstraps with Perl 5
429 +** Native Perl OO == Low level, like VM opcodes
430 +** Virtualized MO == High Level
431 +** Completely separate levels
432 +* `$ri->dispatch( $responder, $method )`
433 +** not `$responder->$method()`
434 +* Full expressiveness
438 {image: http://www.sonofthesouth.net/american-indians/pictures/indian-camp.jpg}
439 +* Integrated object system
440 +* RIs are compiled into packages
441 +** Simple RIs are dissassembled and stuffed into the stash
442 +** Complex RIs use `AUTOLOAD`, delegating to the RI meta object
443 +* Only named method calls
444 +** No arbitrary call concepts
445 +** `->` doesn't support anything else
446 +* As fast as "regular" Perl OO
447 +** Even makes simple, standalone `.pmc`s
451 +** Usable with regular Perl OO {html:☺}
452 +** Lacks arbitrary invocations {html:☹}
454 +** Feature complete {html:☺}
455 +** Doesn't integrate {html:☹}
456 +** Slow {html:☹}
458 = Idealized MO toolchain
459 {image: http://www.ibiblio.org/wm/paint/auth/botticelli/botticelli.venus.jpg}
460 +* Compiler handles modeling
461 +** Constructs meta objects at compile time
462 +* VM Opcodes support standard RI
463 +* Custom RIs are just objects
464 +** Bootstrapped using standard runtime objects
466 +*** Named method invocation
469 {image: http://lolgeeks.com/wp-content/uploads/2007/05/lolgeeks016.jpg}
470 +* Perl 6 is the idealized MO toolchain
471 +** Can introduce syntax
472 +*** Invocation types
473 +*** Concept declarations
474 +** Implement bootstrap RIs in VM opcodes
475 +* Pugs might be using MO
476 +** I'm not really sure
477 +** It was ported a while ago
479 = Introducing new concepts
480 +* Roles are the shit
481 +* But what about next week's fad?
482 +* MO lets you introduce a new concept
483 +** Arbitrary at compile time
484 +** RI protocol at runtime
486 = Example - Prototype Objects
490 my ( $object, $invocation ) = @_;
492 my $method = $object->{ $invocation->name };
494 $object->$method( $invocation->arguments );
498 = Example - Attribute Grammars
499 {image: http://nothingmuch.woobling.org/village_people.jpg}
500 +* Crazy stuff from the 1970s
501 +* Renewed interest in {html:λ} land
502 +* Was pretty easy in MO
504 = Attribute Grammer Implementation
505 {image: http://grammar.ccc.commnet.edu/grammar/images/grammar.gif}
506 +* Introduce new concept objects
507 +** Attribute Grammar
508 +** Attribute Grammer Instance - one per AG per class
509 +* Runtime specialized RI
510 +** Shadows any RI with additional context sensitive methods
511 +* Additional runtime support code
514 {image: http://www.constructionownerslawblog.com/checklist.jpg}
515 +* Write a sugar layer
516 +** Make it fun to use
518 +* Refactor the Class objects
519 +** Bloated example code
520 +** There are some patterns to extract
524 +* Easier to maintain MO written in Moose
525 +* Need to have clean syntax
526 +* Stable `.pmc` compilation
529 {image: http://www1.istockphoto.com/file_thumbview_approve/2540021/2/istockphoto_2540021_painted_exclamation_mark.jpg}
530 +* Meta code is awesome code
531 +** Especially my meta code ;-)
534 {image: http://www1.istockphoto.com/file_thumbview_approve/2540021/2/istockphoto_2540021_painted_exclamation_mark.jpg}
535 * Meta code is awesome code
536 +** Lets you program in new ways
537 +** Helps you take care of your other code
538 +* Meta code is important code
539 +** It can affect *anything*
540 +** Keep it minimalistic, and clearly defined
541 +** No spaghetti monsters
542 +* Meta code can be simple
543 +** Only complicated if you aren't careful
546 {image: http://www.girlscoutsofblackhawk.org/Portals/0/webphotos/thanks.jpg}