Inline-Python

 view release on metacpan or  search on metacpan

Python.pod  view on Meta::CPAN


   py_eval(<<'END');

   class Foo:
      def __init__(self):
         print "new Foo object being created"
         self.data = {}
      def get_data(self): return self.data
      def set_data(self,dat): 
         self.data = dat

   END

   py_bind_class("main::Foo", "__main__", "Foo", "set_data", "get_data");
   my $o = new Foo;

This call to py_bind_class() will generate this code and eval() it:

   package main::Foo;
   @main::Foo::ISA = qw(Inline::Python::Object);

   sub new {
     splice @_, 1, 0, "__main__", "Foo";
     return &Inline::Python::py_new_object;
   }

   sub set_data {
     splice @_, 1, 0, "set_data";
     return &Inline::Python::py_call_method;
   }

   sub get_data {
     splice @_, 1, 0, "get_data";
     return &Inline::Python::py_call_method;
   }

Note that if you want methods to be created as I've shown, you must pass
them to py_bind_class() yourself. It doesn't create anything except new()
and the @ISA array. It doesn't need to, since the base class knows how to
deal with any method call -- but it's also slower, since it has to walk up
the inheritance tree to the AUTOLOAD method. I recommend binding to
the functions you know about, especially if you're the one writing the code.
If it's auto-generated, use py_study_package(), described below.

=head2 py_study_package()

   py_study_package(["package"])

This function interrogates the Python interpreter about the given package
(or '__main__' if you don't specify one). It returns a list of key/value
pairs, so it should be used like this:

   py_eval('import pickle');
   my %namespace = py_study_package("pickle");

On my machine, %namespace looks something like this:

   $VAR1 = {
             'classes' => { ... },
             'functions' => [
                              '_keep_alive',
                              'loads',
                              'dump',
                              'load',
                              'dumps',
                              'test',
                              'whichmodule'
                            ]
           };

Each result can be fed to py_bind_function() and py_bind_class(), which is 
exactly what Inline::Python itself does.

=head2 py_is_tuple()

   my $array_ref = py_eval('(1, 2)')
   $is_tuple = py_is_tuple($array_ref)

This function can tell you if the array reference you got from calling some
Python code was a tuple in Python or not (e.g. a normal array). This can be
useful if an API requires a distinction between those cases. py_is_tuple works
by looking for a magic marker put onto array refs by Py2Pl. Bear in mind that
this marker may get lost when copying the array data.

=head1 SEE ALSO 

For information about using C<Inline>, see L<Inline>.

For information about other Inline languages, see L<Inline-Support>.

Inline::Python's mailing list is inline@perl.org

To subscribe, send email to inline-subscribe@perl.org

=head1 BUGS AND DEFICIENCIES

This is a production quality release of Inline::Python. It is fairly feature
complete and runs stable with no known crasher bugs or memory leaks. Further
testing and expanded support for other operating systems and platforms will be
a focus for future releases.

When reporting a bug, please do the following:

 - Put "use Inline REPORTBUG;" at the top of your code, or 
   use the command line option "perl -MInline=REPORTBUG ...".
 - Run your code.
 - Follow the printed instructions.

Here are some things to watch out for:

=over 4

=item 1

Note that the namespace imported into Perl is NOT recursively
traversed. Only Python B<globals> are imported into Perl --
subclasses, subfunctions, and other modules are not imported.

Example:

   use Inline Python => <<'END';



( run in 0.859 second using v1.01-cache-2.11-cpan-483215c6ad5 )