Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EXTERN.h> /* from the Perl distribution */
- #include <perl.h> /* from the Perl distribution */
- #include <string.h>
- static void xs_init (pTHX);
- EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
- /* EXTERN_C void boot_Socket (pTHX_ CV* cv); */
- EXTERN_C void
- xs_init(pTHX)
- {
- char *file = __FILE__;
- /* DynaLoader is a special case */
- newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
- /* newXS("Socket::bootstrap", boot_Socket, file); */
- }
- static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
- int main(int argc, char **argv, char **env)
- {
- char *arg1[] ={"", "-e", "0"};
- PERL_SYS_INIT3(&argc,&argv,&env);
- my_perl = perl_alloc();
- perl_construct(my_perl);
- PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
- perl_parse(my_perl, xs_init, 3, arg1, NULL);
- perl_run(my_perl);
- eval_pv("use Socket; $main::a='Data Var A, 1';", TRUE);
- eval_pv("print $a; print \"\n\"", TRUE);
- SV* asv;
- asv=get_sv("main::a", 0);
- int strl;
- strl=SvCUR(asv);
- char * cstr;
- cstr=SvPV(asv, strl);
- printf ("c: %s\n", cstr);
- SV* aval;
- aval=get_sv("aval", GV_ADD);
- if(aval == NULL) {
- printf("aval undef\n");
- }
- char * str1;; ;
- str1=malloc(400);
- strcpy(str1, "Data Store A2\n");
- SvUPGRADE(aval, SVt_PV);
- SvCUR_set(aval, strlen(str1));
- SvPOK_only(aval);
- SvPV_set(aval, str1);
- SV* aval2;
- aval2=get_sv("aval2", GV_ADD);
- sv_setpv(aval2, "Data Store A3\n");
- eval_pv ("print \"aval: $aval, $aval2\n\"; ", TRUE);
- char * a2str;
- a2str=SvPVX(aval2);
- a2str[2]='g';
- str1[2]='f';
- eval_pv("print 'hv3', $aval2, ':', $aval, \"\n\";", TRUE);
- HV* hashv1;
- SV* svstore2;
- svstore2=newSV(100);
- sv_setpv(svstore2, "SV Store 2: Data Store");
- hashv1=get_hv("hashv1", GV_ADD);
- char k1[100]=("data store 1");
- int k1len;
- k1len=strlen(k1);
- hv_store(hashv1, k1, k1len, svstore2, 0);
- eval_pv("print \"hv2 $hashv1{'data store 1'}\\n\"; $hashv1{datastore2}=\"data store 2\";", TRUE);
- SV** hvfetch1;
- strcpy (k1, "datastore2");
- k1len=strlen(k1);
- hvfetch1=hv_fetch(hashv1, k1, k1len, NULL);
- k1len=SvCUR(*hvfetch1);
- printf("hv1 %s\n", SvPV(*hvfetch1, k1len));
- HV* hv1;
- hv1=newHV();
- SV* hvsv3;
- hvsv3=newSV(100);
- sv_setpv(hvsv3, "data store 4");
- strcpy(k1, "lstore1");
- k1len=strlen(k1);
- hv_store(hv1, k1, k1len, hvsv3, 0);
- SV* ref1;
- ref1=newSV(100);
- SvROK_on(ref1);
- SvRV_set(ref1, (SV*)hv1);
- char k2[]=("refstore1");
- int k2len;
- k2len=strlen(k2);
- hv_store(hashv1, k2, k2len, ref1, 0);
- eval_pv("print \"reftest1: $hashv1{refstore1}{lstore1}\n\"; print (\"reftype:\", ref ($hashv1{refstore1}), \"\n\");", TRUE);
- eval_pv("$hashv1{refstore2}{lstore1}=\"Data Store 5\"", TRUE);
- SV** ref2;
- strcpy(k1, "refstore2");
- k1len=strlen(k1);
- ref2=hv_fetch(hashv1, k1, k1len, NULL);
- HV* ref3;
- ref3 =(HV*) SvRV(*ref2);
- strcpy(k1, "lstore1");
- k1len=strlen(k1);
- SV** result5;
- result5=hv_fetch(ref3, k1, k1len, NULL);
- int r5len;
- r5len=SvCUR(*result5);
- printf("hvrfetch: %s\n", SvPV(*result5, r5len));
- eval_pv("sub myfunction { my ($times, $name1, $name2)=@_; my $count=0; while ($times > $count) { print \"Hello $name1 and $name2\
- \\n\"; ++$count; } return ($name1, $name2); }", TRUE);
- int mfa=10;
- char mfb[]="Mary";
- char mfc[]="Bill";
- int mfbl;
- int mfcl;
- mfbl=strlen(mfb);
- mfcl=strlen(mfc);
- dSP;
- ENTER;
- SAVETMPS;
- PUSHMARK(SP);
- XPUSHs(sv_2mortal(newSViv(mfa)));
- XPUSHs(sv_2mortal(newSVpv(mfb, mfbl)));
- XPUSHs(sv_2mortal(newSVpv(mfc, mfcl)));
- PUTBACK;
- call_pv("myfunction", G_ARRAY);
- SPAGAIN;
- printf("%s %s\n", POPpx, POPpx);
- PUTBACK;
- FREETMPS;
- LEAVE;
- eval_pv("myfunction(10, 'mark', 'alice');", TRUE);
- perl_destruct(my_perl);
- perl_free(my_perl);
- PERL_SYS_TERM();
- }
Add Comment
Please, Sign In to add comment