Guest User

Untitled

a guest
Jan 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <EXTERN.h> /* from the Perl distribution */
  2. #include <perl.h> /* from the Perl distribution */
  3.  
  4. #include <string.h>
  5.  
  6. static void xs_init (pTHX);
  7.  
  8. EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
  9. /* EXTERN_C void boot_Socket (pTHX_ CV* cv); */
  10. EXTERN_C void
  11.  
  12. xs_init(pTHX)
  13. {
  14. char *file = __FILE__;
  15. /* DynaLoader is a special case */
  16. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  17. /* newXS("Socket::bootstrap", boot_Socket, file); */
  18. }
  19.  
  20. static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
  21.  
  22. int main(int argc, char **argv, char **env)
  23. {
  24. char *arg1[] ={"", "-e", "0"};
  25.  
  26. PERL_SYS_INIT3(&argc,&argv,&env);
  27. my_perl = perl_alloc();
  28. perl_construct(my_perl);
  29. PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  30. perl_parse(my_perl, xs_init, 3, arg1, NULL);
  31.  
  32. perl_run(my_perl);
  33. eval_pv("use Socket; $main::a='Data Var A, 1';", TRUE);
  34. eval_pv("print $a; print \"\n\"", TRUE);
  35. SV* asv;
  36. asv=get_sv("main::a", 0);
  37. int strl;
  38. strl=SvCUR(asv);
  39. char * cstr;
  40. cstr=SvPV(asv, strl);
  41. printf ("c: %s\n", cstr);
  42. SV* aval;
  43. aval=get_sv("aval", GV_ADD);
  44. if(aval == NULL) {
  45. printf("aval undef\n");
  46. }
  47.  
  48. char * str1;; ;
  49. str1=malloc(400);
  50. strcpy(str1, "Data Store A2\n");
  51.  
  52. SvUPGRADE(aval, SVt_PV);
  53. SvCUR_set(aval, strlen(str1));
  54. SvPOK_only(aval);
  55. SvPV_set(aval, str1);
  56. SV* aval2;
  57. aval2=get_sv("aval2", GV_ADD);
  58. sv_setpv(aval2, "Data Store A3\n");
  59.  
  60. eval_pv ("print \"aval: $aval, $aval2\n\"; ", TRUE);
  61. char * a2str;
  62. a2str=SvPVX(aval2);
  63. a2str[2]='g';
  64. str1[2]='f';
  65. eval_pv("print 'hv3', $aval2, ':', $aval, \"\n\";", TRUE);
  66.  
  67.  
  68. HV* hashv1;
  69. SV* svstore2;
  70. svstore2=newSV(100);
  71.  
  72. sv_setpv(svstore2, "SV Store 2: Data Store");
  73. hashv1=get_hv("hashv1", GV_ADD);
  74. char k1[100]=("data store 1");
  75. int k1len;
  76. k1len=strlen(k1);
  77. hv_store(hashv1, k1, k1len, svstore2, 0);
  78.  
  79.  
  80. eval_pv("print \"hv2 $hashv1{'data store 1'}\\n\"; $hashv1{datastore2}=\"data store 2\";", TRUE);
  81.  
  82. SV** hvfetch1;
  83. strcpy (k1, "datastore2");
  84. k1len=strlen(k1);
  85. hvfetch1=hv_fetch(hashv1, k1, k1len, NULL);
  86.  
  87. k1len=SvCUR(*hvfetch1);
  88. printf("hv1 %s\n", SvPV(*hvfetch1, k1len));
  89.  
  90. HV* hv1;
  91. hv1=newHV();
  92. SV* hvsv3;
  93. hvsv3=newSV(100);
  94. sv_setpv(hvsv3, "data store 4");
  95.  
  96. strcpy(k1, "lstore1");
  97. k1len=strlen(k1);
  98.  
  99. hv_store(hv1, k1, k1len, hvsv3, 0);
  100.  
  101. SV* ref1;
  102. ref1=newSV(100);
  103. SvROK_on(ref1);
  104. SvRV_set(ref1, (SV*)hv1);
  105. char k2[]=("refstore1");
  106. int k2len;
  107. k2len=strlen(k2);
  108.  
  109.  
  110. hv_store(hashv1, k2, k2len, ref1, 0);
  111.  
  112. eval_pv("print \"reftest1: $hashv1{refstore1}{lstore1}\n\"; print (\"reftype:\", ref ($hashv1{refstore1}), \"\n\");", TRUE);
  113. eval_pv("$hashv1{refstore2}{lstore1}=\"Data Store 5\"", TRUE);
  114.  
  115. SV** ref2;
  116. strcpy(k1, "refstore2");
  117. k1len=strlen(k1);
  118.  
  119. ref2=hv_fetch(hashv1, k1, k1len, NULL);
  120.  
  121. HV* ref3;
  122. ref3 =(HV*) SvRV(*ref2);
  123.  
  124. strcpy(k1, "lstore1");
  125. k1len=strlen(k1);
  126.  
  127. SV** result5;
  128. result5=hv_fetch(ref3, k1, k1len, NULL);
  129. int r5len;
  130. r5len=SvCUR(*result5);
  131.  
  132. printf("hvrfetch: %s\n", SvPV(*result5, r5len));
  133.  
  134. eval_pv("sub myfunction { my ($times, $name1, $name2)=@_; my $count=0; while ($times > $count) { print \"Hello $name1 and $name2\
  135. \\n\"; ++$count; } return ($name1, $name2); }", TRUE);
  136. int mfa=10;
  137. char mfb[]="Mary";
  138. char mfc[]="Bill";
  139. int mfbl;
  140. int mfcl;
  141. mfbl=strlen(mfb);
  142. mfcl=strlen(mfc);
  143.  
  144. dSP;
  145. ENTER;
  146. SAVETMPS;
  147. PUSHMARK(SP);
  148. XPUSHs(sv_2mortal(newSViv(mfa)));
  149. XPUSHs(sv_2mortal(newSVpv(mfb, mfbl)));
  150. XPUSHs(sv_2mortal(newSVpv(mfc, mfcl)));
  151. PUTBACK;
  152. call_pv("myfunction", G_ARRAY);
  153. SPAGAIN;
  154. printf("%s %s\n", POPpx, POPpx);
  155. PUTBACK;
  156. FREETMPS;
  157. LEAVE;
  158.  
  159. eval_pv("myfunction(10, 'mark', 'alice');", TRUE);
  160. perl_destruct(my_perl);
  161. perl_free(my_perl);
  162. PERL_SYS_TERM();
  163. }
Add Comment
Please, Sign In to add comment