Giter VIP home page Giter VIP logo

perlcpp's Introduction

perlcpp

Embed perl into cpp file ,and build into executed files.

demo 1

  • use "./interp.cpp" as template, notice one only need change the code in "TODO AREA".
INIT_PERL_INTERP ;

// -------- TODO ----------
// ------------------------

char *id_cmd = " $a=qq/ABC_/;$a=$a x 10; $a =~ s/.$//;@arr=split m/_/,$a;  ";
P_eval( id_cmd );	// run the perl statement in C++ 
P_arr_print( "arr" );

// ------------------------
// ------------------------
DESTROY_PERL_INTERP ;
return 0;

to build that, please run : sh bld_cpp.sh , on Windows, I recommend Strawberry Perl. use the g++ or gcc under Strawberry. (if one see error msg, please comment the first line and uncomment 2nd line to use g++ with -std=c++11.)

if not lucky enough to build, check you GCC version, later than version 4.9.2 is recommend.

if everything is ok, a file "interp" will be generated. run it. this will get following output:

$ ./interp

0 : ABC
1 : ABC
2 : ABC
3 : ABC
4 : ABC
5 : ABC
6 : ABC
7 : ABC
8 : ABC
9 : ABC

demo 2

  • also based on ./interp.cpp, edit the TODO.
// -------- TODO ----------
// ------------------------

char *id_cmd = " @arr=(0..99); $float_num=1.234; $str=qq(STRINGS); ";
P_eval( id_cmd );

int num_idx_3 = P_arr_Int_i("arr", 3);   // => 3 
char *str = Str("str"); 	// => "STRINGS"
float *float_num = Float("float_num") ; // => 1.234 

// ------------------------
// ------------------------

demo 3

  • also based on ./interp.cpp, edit the TODO.
// -------- TODO ----------
// ------------------------

int m_ok = m("abc", "m/b/");  // m_ok => 1 
int m_not_ok = m("abc", "m/b/");  // m_not_ok => 0
char *ss = s("abc", "s/b/_B_/"); // ss => a_B_c   ,this is perl $a =~ s/xx/XX/g

// ------------------------
// ------------------------
	

good enough? or wanna explore more API to see the magic?

API Description

  • I think this is self-explaination. no need extra description :)
  INIT_PERL_INTERP;                             init perl and perl interp :my_perl
...
  P_eval("$_=111");                             run perl statement
  Int("a");                                     get $a from eval()
  Float("a");                                   dotto
  Str("a");                                     dotto
  P_arr_len("arr");                             get @arr length
  P_arr_Str_i("arr", 3);                        get @arr[3] as Str;
  P_arr_Int_i("arr", 3);                        get @arr[3] as Int;
  P_arr_Float_i("arr", 3);                      get @arr[3] as Float;
  P_arr_print("arr");                           print out @arr and its index,very slowly...
  int xx = m("abc", "m/b./");                   perl match
  char *ss = s("abc", "s/b/B/g");               perl substitute
...
  DESTROY_PERL_INTERP;                          Destroy my_perl

 

--By Jidor Tang [email protected] at 2017-03-07

todo next

  • test new env
  • add t folder for testcases

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.