NAME

src/pmc/exporter.pmc - Export globals from one namespace to another

SYNOPSIS

You can use Exporter in PIR to import subs from a library. At its simplest:

 .sub main :main
     load_bytecode 'Test/More.pir'

     .local pmc exporter, src_ns, dest_ns
     src_ns   = get_namespace [ 'Test'; 'More' ]
     exporter = new 'Exporter'

     exporter.'import'( src_ns :named('source') 'plan ok' :named('globals') )
     plan(1)
     ok(1, 'exporter has imported the requested functions')
 .end

You can also specify the exporter attributes before making the import call, for example to import into the alternate namespace 'Foo' you could use the following code:

    src_ns   = get_namespace [ 'Test'; 'More' ]
    dest_ns  = get_namespace [ 'Foo' ]
    exporter.'source'(src_ns)
    exporter.'destination'(dest_ns)
    exporter.'import'('plan ok' :named('globals'))

You can even import subroutines under different names if globals is a hash:

    globals         = new 'Hash'
    globals['plan'] = 'steps'
    globals['ok']   = 'passed'
    exporter.'import'(globals :named('globals'))
    steps(1)
    passed(1)

DESCRIPTION

Exports globals from one namespace to another. Exporter always uses the typed namespace interface, as outlined in docs/pdds/pdd21_namespaces.pod.

Exporter is not derived from any other PMC, and does not provide any vtable interface--its interface consists solely of non-vtable methods.

Structure

The Exporter PMC structure (Parrot_Exporter) consists of three items:

ns_src
The source namespace -- a NameSpace PMC. A Null PMC is allocated during initialization.
ns_dest
The destination namespace -- a NameSpace PMC. A PMC representing the current namespace is allocated upon initialization.
globals
The globals to export -- a PMC that implements a hash, an array, a String containing a list of space-separated subroutine names or Null.A Null PMC is allocated during initialization.

Functions

void init()
Initializes an Exporter PMC.
void mark()
Mark referenced strings and PMCs in the structure as live.

Methods

METHOD source(PMC *src :optional, int got_src :opt_flag)
Accessor for the source NameSpace object (ns_src.) Sets the value if src is passed, otherwise returns the value. Throws an exception if a non-NameSpace PMC is passed.
METHOD destination(PMC *dest :optional, int got_dest :opt_flag)
Accessor for the destination NameSpace object (ns_dest.) Sets the value if dest is passed, otherwise returns the value. Throws an exception if a non-NameSpace PMC is passed.
METHOD globals(PMC *glb :optional, int got_glb :opt_flag)
Accessor for the globals to export (globals.) Sets the value if glb is passed, otherwise returns the value. If glb is a String, it is split on ascii whitespace, and each array member is added as a hash key. If glb implements the array interface, each member is added as a hash key. if glb implements the hash interface, it is assigned to Exporter's globals attribute. Throws an exception if an unknown PMC type is passed.
METHOD import(PMC *dest :optional :named("destination"), int got_dest :opt_flag, PMC *src :optional :named("source"), int got_src :opt_flag, PMC *globals :optional :named("globals"), int got_globals :opt_flag)
Import globals from the src namespace to the dest namespace. If src, dest, or globals are passed, they override the current value. import follows the semantics of the export_to method of the NameSpace PMC. in particular, if a NULL value is passed for globals, the default set of items will be imported. Throws an exception upon error.

STABILITY

Unstable. This PMC is under active development; major portions of the interface have not yet been completed.

SEE ALSO

docs/pdds/pdd17_basic_types.pod, docs/pdds/pdd21_namespaces.pod.