2012-08-06 1 views
8

Ich lerne Perl Inline :: Python-Bibliothek. Im Beispiel der cpan Website, haben wirPerl Inline :: Python-Modul, wie man Code in eine Zeichenfolge einfügt

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 

Ist es möglich, Python-Code in Zeichenfolge zu setzen, so dass ich den Python-Code in der Laufzeit erstellen kann? Zum Beispiel etwas wie:

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"; 

Wir haben ein Projekte, die dynamisch Python-Funktionen zur Laufzeit erstellen. Und wir wollen diese Funktionen aufrufen. Ist py_eval() der Weg zu gehen? Danke im Voraus.

+0

Sie ein Python-Interpreter einbetten? –

+0

Ja, irgendwie. Unser Perl-Programm muss Python-Funktionen ausführen, die zur Laufzeit erstellt werden. – biajee

Antwort

4

Keine Erfahrung mit Inline::Python, aber mit Inline::Cyou can use the bind function to set code at runtime, vielleicht wird diese Arbeit:

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"; 
+0

Sie haben Recht. Das funktioniert. Vielen Dank, Mob. – biajee

Verwandte Themen