<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Parrot - Export globals from one namespace to another</title> <link rel="stylesheet" type="text/css" href="../../../resources/parrot.css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <div id="wrapper"> <div id="header"> <a href="http://www.parrot.org"> <img border=0 src="../../../resources/parrot_logo.png" id="logo" alt="parrot"> </a> </div> <!-- "header" --> <div id="divider"></div> <div id="mainbody"> <div id="breadcrumb"> <a href="../../../html/index.html">Home</a> » <a href="../../../html/pmc.html">PMCs</a> » Export globals from one namespace to another </div> <h1><a name="NAME" >NAME</a></h1> <p>src/pmc/exporter.pmc - Export globals from one namespace to another</p> <h1><a name="SYNOPSIS" >SYNOPSIS</a></h1> <p>You can use Exporter in PIR to import subs from a library. At its simplest:</p> <pre> .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</pre> <p>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:</p> <pre> 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'))</pre> <p>You can even import subroutines under different names if globals is a hash:</p> <pre> globals = new 'Hash' globals['plan'] = 'steps' globals['ok'] = 'passed' exporter.'import'(globals :named('globals')) steps(1) passed(1)</pre> <h1><a name="DESCRIPTION" >DESCRIPTION</a></h1> <p>Exports globals from one namespace to another. Exporter always uses the typed namespace interface, as outlined in <em>docs/pdds/pdd21_namespaces.pod</em>.</p> <p>Exporter is not derived from any other PMC, and does not provide any vtable interface--its interface consists solely of methods, not vtable functions.</p> <h2><a name="Structure" >Structure</a></h2> <p>The Exporter PMC structure (<code>Parrot_Exporter</code>) consists of three items:</p> <dl> <dt><a name="ns_src" ><b><code>ns_src</b></code></a></dt> The source namespace -- a NameSpace PMC. A Null PMC is allocated during initialization. <dt><a name="ns_dest" ><b><code>ns_dest</b></code></a></dt> The destination namespace -- a NameSpace PMC. A PMC representing the current namespace is allocated upon initialization. <dt><a name="globals" ><b><code>globals</b></code></a></dt> 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.</dl> <h2><a name="Functions" >Functions</a></h2> <dl> <dt><a name="void_init()" ><b><code>void init()</b></code></a></dt> Initializes an Exporter PMC. <dt><a name="void_mark()" ><b><code>void mark()</b></code></a></dt> Mark referenced strings and PMCs in the structure as live.</dl> <h2><a name="Methods" >Methods</a></h2> <dl> <dt><a name="METHOD_source(PMC_*src_:optional,_int_got_src_:opt_flag)" ><b><code>METHOD source(PMC *src :optional, int got_src :opt_flag)</b></code></a></dt> Accessor for the source NameSpace object (<code>ns_src</code>.) Sets the value if <code>src</code> is passed, otherwise returns the value. Throws an exception if a non-NameSpace PMC is passed. <dt><a name="METHOD_destination(PMC_*dest_:optional,_int_got_dest_:opt_flag)" ><b><code>METHOD destination(PMC *dest :optional, int got_dest :opt_flag)</b></code></a></dt> Accessor for the destination NameSpace object (<code>ns_dest</code>.) Sets the value if <code>dest</code> is passed, otherwise returns the value. Throws an exception if a non-NameSpace PMC is passed. <dt><a name="METHOD_globals(PMC_*glb_:optional,_int_got_glb_:opt_flag)" ><b><code>METHOD globals(PMC *glb :optional, int got_glb :opt_flag)</b></code></a></dt> Accessor for the globals to export (<code>globals</code>.) Sets the value if <code>glb</code> is passed, otherwise returns the value. If <code>glb</code> is a String, it is split on ascii whitespace, and each array member is added as a hash key. If <code>glb</code> implements the array interface, each member is added as a hash key. if <code>glb</code> implements the hash interface, it is assigned to Exporter's <code>globals</code> attribute. Throws an exception if an unknown PMC type is passed. <dt><a ><b><code>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)</b></code></a></dt> Import <code>globals</code> from the <code>src</code> namespace to the <code>dest</code> namespace. If <code>src</code>, <code>dest</code>, or <code>globals</code> are passed, they override the current value. <code>import</code> follows the semantics of the <code>export_to</code> method of the <code>NameSpace</code> PMC. in particular, if a NULL value is passed for <code>globals</code>, the default set of items will be imported. Throws an exception upon error.</dl> <h1><a name="STABILITY" >STABILITY</a></h1> <p>Unstable. This PMC is under active development; major portions of the interface have not yet been completed.</p> <h1><a name="SEE_ALSO" >SEE ALSO</a></h1> <p><em>docs/pdds/pdd17_basic_types.pod</em>, <em>docs/pdds/pdd21_namespaces.pod</em>.</p> </div> <!-- "mainbody" --> <div id="divider"></div> <div id="footer"> Copyright © 2002-2011, Parrot Foundation. </div> </div> <!-- "wrapper" --> </body> </html>