|parrotcode: Perl 6-like methods and metaclasses for Parrot|
|Contents | Libraries|
P6object - Perl 6-like methods and metaclasses for Parrot
.sub 'main' # load this library load_bytecode 'P6object.pbc' ## grab the P6metaclass protoobject .local pmc p6meta p6meta = get_hll_global 'P6metaclass' ## create a new class ABC::Def with three attributes p6meta.'new_class'('ABC::Def', 'attr'=>'$a @b %c') ## get the protoobject for ABC::Def .local pmc defproto defproto = get_hll_global ['ABC'], 'Def' ## use the protoobject to create a new ABC::Def object .local pmc obj obj = defproto.'new'() ## get the class protoobject from any object $P0 = obj.'WHAT'() ## get the metaclass for any object $P0 = obj.'HOW'() ## create a new class MyHash as a subclass of Parrot's 'Hash' p6meta.'new_class'('MyHash', 'parent'=>'Hash') ## tell Parrot classes to use a specific protoobject $P0 = get_hll_global 'MyHash' p6meta.'register'('Hash', 'protoobject'=>$P0) $P1 = new 'Hash' # create a Hash $P2 = $P1.'WHAT'() # get its protoobject $S3 = $P2 # stringify say $S3 # "MyHash\n"
P6object is intended to add Perl 6-like behaviors to objects in Parrot. It creates and maintains protoobjects, and supplies
.HOW methods to objects and protoobjects in Parrot. Protoobjects also have a default
.new method for creating new instances of a class (classes are able to override this, however).
P6object is the base class for objects that make use of the P6metamodel. It supplies the
P6metaclassof the invocant.
P6protoobjectfor the invocant.
protoobject, and verifies that
parrotclasshas P6object methods defined on it. This happens either by setting
P6objectas a parent of
parrotclass, or by individually composing
P6object's methods into
nameparameter causes objects to be registered using a name that differs from the parrotclass name. This is useful when needing to map to a class name that already exists in Parrot (e.g., 'Hash' or 'Object').
nameas a subclass of
nameis a string, then double-colons will be treated as separators. If
parentclassisn't supplied, defaults to using
P6objectas the parent. The
attrparameter is a list of attribute names to be added to the class, specified as either an array or a string of names separated by spaces.
name. Return null if no protoobject exists, or whatever is present isn't a protoobject.
newmethod here exists only in the protoobject and not in the individual instances of the class. (If you want all objects in a class to have a
newmethod, then define one in the class and it will be used in lieu of this one.)
Written and maintained by Patrick R. Michaud,
pmichaud at pobox.com. Please send patches, feedback, and suggestions to the parrot-porters mailing list or to
Copyright (C) 2008, The Perl Foundation.