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 .WHAT
and .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 .WHAT
and .HOW
methods.
P6object
and P6metaclass
.P6metaclass
of the invocant.P6protoobject
for the invocant.x
.parrotclass
to use protoobject
, and verifies that parrotclass
has P6object methods defined on it. This happens either by setting P6object
as a parent of parrotclass
, or by individually composing P6object
's methods into parrotclass
.name
parameter 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').name
as a subclass of parentclass
. If parentclass
isn't supplied, defaults to using P6object
as the parent. The attr
parameter 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.x
.typeof_s_p
opcode.new
method 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 new
method, 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 parrotbug@perl.org
.
Copyright (C) 2008, The Perl Foundation.
|