NAME ^

P6object - Perl 6-like methods and metaclasses for Parrot

SYNOPSIS ^

    .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"

DESCRIPTION ^

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).

CLASSES ^

P6object ^

P6object is the base class for objects that make use of the P6metamodel. It supplies the .WHAT and .HOW methods.

onload() :anon :init :load

Initializes the P6object system. Builds protoobjects for P6object and P6metaclass.

HOW()

Return the P6metaclass of the invocant.

WHAT()

Return the P6protoobject for the invocant.

PROTOOVERRIDES()

Return a list of methods to be overridden in protoobjects for the class. Defaults to 'new' (i.e., any '.new' method in a class will override the one given for P6protoobject below).

P6metaclass ^

WHAT()

Return the protoobject for this metaclass.

isa(x)

Return a true value if the invocant 'isa' x.

can(x)

Return a true value if the invocant 'can' x.

add_parent(parentclass [, 'to'=>parrotclass])

register(parrotclass [, 'name'=>name] [, 'protoobject'=>proto] [, 'parent'=>parentclass])

Sets objects of type 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.

The 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').

new_class(name [, 'parent'=>parentclass] [, 'attr'=>attr])

Create a new class called 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.

get_parrotclass(x)

Multimethod helper to return the parrotclass for x.

P6protoobject ^

get_string() (vtable method)

Returns the "shortname" of the protoobject's class.

defined() (vtable method)

Protoobjects are always treated as being undefined.

name() (vtable method)

Have protoobjects return their longname in response to a typeof_s_p opcode.

new()

Provides a default constructor for creating objects in the class.

Note that unlike Perl 6, the 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.)

ACCEPTS(topic)

AUTHOR ^

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 ^

Copyright (C) 2008, The Perl Foundation.


parrot