8

I am learning perl Inline::Python library. In the example of cpan website, we have

   print "9 + 16 = ", add(9, 16), "\n";
   print "9 - 16 = ", subtract(9, 16), "\n";

   use Inline Python => <<'END_OF_PYTHON_CODE';
   def add(x,y): 
      return x + y

   def subtract(x,y):
      return x - y

   END_OF_PYTHON_CODE

Is it possible to put python code into string so that I can create the python code in the runtime? For example, something like:

my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
use Inline Python => "$python_code";
print "9 + 16 = ", add(9, 16), "\n";

We have a projects that will dynamically create python functions at the runtime. And we want to call these functions. Is py_eval() the way to go? Thanks in advance.

2
  • Are you embedding a Python interpreter? Commented Aug 6, 2012 at 18:52
  • Yes, sort of. Our perl program needs to run python functions that are created at the runtime. Commented Aug 6, 2012 at 20:07

1 Answer 1

5

No experience with Inline::Python, but with Inline::C you can use the bind function to set code at runtime, so maybe this will work:

my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
Inline->bind( Python => $python_code );
print "9 + 16 = ", add(9, 16), "\n";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.