tagged release 0.7.1
[parrot.git] / ext / Parrot-Embed / lib / Parrot / Interpreter.pm
blob86200399bf181b43694e34fb42ed1583a51c193d
1 package Parrot::Interpreter;
3 # $Id$
5 use strict;
6 use warnings;
7 our $VERSION = '0.02';
9 use Parrot::Embed;
13 __END__
15 =head1 NAME
17 Parrot::Interpreter - access a Parrot interpreter from Perl 5
19 =head1 VERSION
21 Version 0.02
23 =head1 SYNOPSIS
25 # the first interpreter created in the program
26 my $interp = Parrot::Interpreter->new();
28 # all subsequent interpreters need a parent
29 my $child_interp = $interp->new( $interp );
31 # load a file that Parrot can recognize as code
32 $interp->load_file( 'some_parrot_file.pbc' );
33 $interp->load_file( 'some_parrot_file.pir' );
34 $interp->load_file( 'some_parrot_file.pasm' );
36 # compile a string of Parrot code
37 $interp->compile( $some_parrot_code );
39 # find a subroutine to invoke
40 my $sub_pmc = $interp->find_global( 'some_parrot_sub' );
41 my $other_sub_pmc = $interp->find_global( 'another_sub', 'NameSpace' );
43 # invoke the subroutine
44 my $result_pmc = $sub_pmc->invoke( $signature, @args );
46 # get the values out of it
47 print "Invoking the Sub gave ", $result_pmc->get_string( $interp ), "!\n";
49 All Parrot access goes through an I<interpreter>, mediated through a
50 C<Parrot::Interpreter> object. There is always one or more interpreters active
51 in a system.
53 An interpreter allows you to load code, to compile code, and to find and store
54 global symbols in Parrot.
56 =head3 Memory and Resource Implications
58 If you have multiple active interpreters, the second and subsequent
59 interpreters must each have an active interpreter as a parent. In general,
60 this may not be an issue, but if you forget, you will receive strange error
61 messages.
63 Note that the parent interpreter must outlive its children, in Perl 5 terms.
64 In general, you do not need to worry about this. However, if you cache these
65 objects, be aware that they do keep references to each other appropriately
66 internally.
68 As well, all C<Parrot::PMC> objects keep references to their parent
69 interpreters for similar reasons.
71 =head1 METHODS
73 This class provides several methods:
75 =over 4
77 =item * C<new( [ $parent ] )>
79 This class method creates and returns a new C<Parrot::Interpreter> object. If
80 there is an existing and active C<Parrot::Interpreter> object, pass it as
81 C<$parent>. Otherwise, pass no argument.
83 =item * C<load_file( $filename )>
85 Given the path to a file on disk, loads and compiles the code into the
86 interpreter. This will throw an exception if Parrot could not load or compile
87 the code successfully.
89 =item * C<compile( $code )>
91 Given a string containing Parrot PIR code, compiles the code into the
92 interpreter. This will return a C<Parrot::PMC> object representing the code.
94 A future version of this method may allow compiling other types of code.
96 =item * C<find_global( $name, [ $namespace ] )>
98 Given the name of a global and, optionally, the namespace of the global,
99 attempts to find a global PMC associated with that name in the invoking
100 interpreter. This will return a C<Parrot::PMC> object if successful and
101 C<undef> if there is no PMC found.
103 This method right now supports only single-level string namespaces; this will
104 change in the future.
106 =back
108 =head1 AUTHOR
110 chromatic, C<< <chromatic at wgz.org> >>
112 =head1 BUGS
114 This code might be able to detect the presence or absence of a parent
115 interpreter and act appropriately.
117 This code needs to support more operations on interpreters.
119 Patches welcome.
121 Please report any bugs or feature requests to the Parrot Porters mailing list.
122 Someday there may be a CPAN version of this code. Who knows?
124 =head1 COPYRIGHT & LICENSE
126 Copyright (C) 2006-2007 The Perl Foundation / chromatic.
128 This program is free software; you can redistribute it and/or modify it
129 under the same terms as Parrot itself.
131 =cut
133 # Local Variables:
134 # mode: cperl
135 # cperl-indent-level: 4
136 # fill-column: 100
137 # End:
138 # vim: expandtab shiftwidth=4: