Created
November 27, 2014 17:11
-
-
Save acidlemon/8184d17059bd4f606865 to your computer and use it in GitHub Desktop.
simple embedded perl in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EXTERN.h> | |
#include <perl.h> | |
#include <stdio.h> | |
int main(int argc, char* argv[], char* env[]) { | |
PerlInterpreter* perl; | |
// PerlのCランタイムの初期化(全体で1回) | |
PERL_SYS_INIT3(&argc, &argv, &env); | |
// Perlインタプリタの初期化 | |
perl = perl_alloc(); | |
PERL_SET_CONTEXT(perl); | |
perl_construct(perl); | |
// Perlインタプリタの実行 | |
char empty[16] = "", e[16] = "-e", zero[16] = "0"; | |
char *embedding[] = { empty, e, zero }; | |
perl_parse(perl, NULL, 3, embedding, NULL); | |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; | |
perl_run(perl); | |
// Perlの文を評価してスカラ値を取得 | |
SV *val = Perl_eval_pv("my $powawa = 3; $powawa *= 3", TRUE); | |
printf("powawa=%ld\n", SvIV(val)); | |
// Perlのインタプリタの後始末 | |
perl_destruct(perl); | |
perl_free(perl); | |
// PerlのCランタイムの後始末(全体で1回) | |
PERL_SYS_TERM(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
でコンパイルして
test.o
を実行すると、と出力が得られる