Commits

Anonymous committed 5872eaf

add SvRVObject

Comments (0)

Files changed (5)

 from _perl import *
 
+
+class PerlClass(object):
+
+    def __init__(self, interp, name):
+        self._interp = interp
+        self.name = name
+
+    def __call__(self, ctor="new", *args):
+        ins = self._interp.call(ctor, (self.name,) + args)
+        self.ins = ins
+        return ins
+
+
     ext_modules=[
         Extension(
             name='_perl', 
-            sources = ['src/_interp.c', 'src/_object.c'],
+            sources = ['src/_interp.c', 'src/object.c', 'src/convert.c'],
             include_dirs = [perl_path],
             #library_dirs = [perl_path],
             libraries = ['perl', 'm'],
 
 #include "Python.h"
 #include "_interp.h"
-#include "_object.h"
+#include "convert.h"
 
 
 static PyTypeObject Perl_Type;
 PyDoc_STRVAR(perl_eval_doc, "");
 
 PyObject *
-call_perlfunc(Perl_Object *self, SV *sv, I32 gimme, PyObject *args)
+call_perlcall(Perl_Object *self, I32 gimme, PyObject *args, int method)
 {
-    PyObject *res;
+    PyObject *res, *name;
     int i, retcnt, arglen, errsv;
 
     arglen = PyTuple_Size(args);
 	    XPUSHs(sv_2mortal(py2sv(self, PyTuple_GET_ITEM(args, i))));
     }
     PUTBACK; 
-    
-    retcnt = call_sv(sv, gimme|G_EVAL);
+    if(method){
+        char *methodname;
+        retcnt = call_method(methodname, gimme|G_EVAL);
+    }else{
+        name  = PyTuple_GetItem(args, 0);
+        SV *sv = py2sv(self, name);
+        retcnt = call_sv(sv, gimme|G_EVAL);
+    }
     errsv = SvTRUE(ERRSV);
 
     SPAGAIN;
 perl_call(Perl_Object *self, PyObject *args)
 {
     dPCTX;
-    PyObject *name;
+    //PyObject *name;
     //TODO len check
-    name  = PyTuple_GetItem(args, 0);
+    //name  = PyTuple_GetItem(args, 0);
     
-    SV *sv = py2sv(self, name);
-    return call_perlfunc(self, sv, G_SCALAR, args); 
+    //SV *sv = py2sv(self, name);
+    return call_perlcall(self, G_SCALAR, args, 0); 
 }
 
 PyDoc_STRVAR(perl_call_doc, "");
 
 PyObject * 
-perl_eval_require(Perl_Object *self, char *module, int use)
+perl_perlrequire(Perl_Object *self, char *module)
 {
     PyObject *res;
     SV *code;
 
     ENTER;
     SAVETMPS;
-    if(use){
-        code = newSVpv("use ", 0);
-    }else{
-        code = newSVpv("require ", 0);
-    }
+    code = newSVpv("require ", 0);
     sv_catpv(code, module);
 
     res_sv = eval_pv(SvPVx(code, n_a), FALSE);
 
     if (!PyArg_ParseTuple(args, "s:perl.require", &module))
 	    return NULL;
-    return perl_eval_require(self, module, 0);
+    return perl_perlrequire(self, module);
 }
 
 PyDoc_STRVAR(perl_require_doc, "");
 
-static PyObject * 
-perl_use(Perl_Object *self, PyObject *args)
-{
-    char *module;
-
-    if (!PyArg_ParseTuple(args, "s:perl.use", &module))
-	    return NULL;
-    return perl_eval_require(self, module, 1);
-
-}
-
-PyDoc_STRVAR(perl_use_doc, "");
 
 static PyMethodDef perl_methods[] = {
     {"eval", (PyCFunction)perl_eval, METH_VARARGS, perl_eval_doc},
     {"call", (PyCFunction)perl_call, METH_VARARGS, perl_call_doc},
     {"require", (PyCFunction)perl_require, METH_VARARGS, perl_require_doc},
-    {"use", (PyCFunction)perl_use, METH_VARARGS, perl_use_doc},
     {NULL, NULL},
 
 };
 
 static PyTypeObject Perl_Type = {
-	PyVarObject_HEAD_INIT(NULL, 0)
+	PyVarObject_HEAD_INIT(&PyType_Type, 0)
 	"Perl",					/* tp_name */
 	sizeof(Perl_Object),				/* tp_basicsize */
 	0,						/* tp_itemsize */
     return $a ** $b;
 }
 
+sub add_str{
+    my ($a, $b) = @_;
+    $a . $b;
+}
+print(add_str("A","B"))
+

tests/test_perl.py

 import util 
-import sys
 
 from perlpy import Perl
 
     p = Perl()
     assert p != None
 
-    
 
+def test_eval():
+    p = Perl()
+    ret = p.eval("$a = 1")
+    assert ret == 1
+
+def test_evalcall():
+    p = Perl()
+    p.eval("""
+    sub add{
+      my ($a, $b) = @_;
+      $a + $b;
+    }
+    """)
+    ret = p.call("add", 1, 2)
+    assert ret == 3
+
+def test_call():
+    p = Perl("tests/power.pl")
+    ret = p.call("expo", 2,2)
+    assert ret == 4.0
+    ret = p.call("add_str", "A", "B")
+    assert ret == "AB"
+
+def test_require():
+    p = Perl()
+    ret = p.require("HTTP::Request")
+    print ret
+
+"""def test_class():
+    p = Perl()
+    cls = p.perlclass("HTTP::Request")
+"""