# perlembed core sub replacing



## Alt (Jan 29, 2011)

I'm developing some application on C using perlembed. 

Main idea is loading a .pm module with perl_parse() and then call a sub with call_argv().

Got a problem while trying to prevent sub from calling exit() because it terminates main C program. Now I'm trying to prevent exiting with eval 
	
	



```
eval_pv("*CORE::GLOBAL::exit = sub{ print 'exit called'; }", TRUE);
```
 but this not working (if I place this line before perl_parse() - it leads to segfault, but after perl_parse() it is gives no effect (program terminates anyway)).. 

Construction is really work in 'clean perl', preventing exit. 
	
	



```
BEGIN { *CORE::GLOBAL::exit = sub{ print 'exit called'; } }
```
 But what I can do when I can't change .pm module?

Anyone have been working around this? Maybe I can change CORE::GLOBAL::exit sub to my own with sv_set* functions, but I can't find how to work with subs using this technique..

Tnx


----------



## Alt (Jan 30, 2011)

If anyone looking this I solved problem with dummy perl_parse(), then fopen/fread/eval_sv


```
char *embedding[] = { "", "-e", "0" };
perl_parse(p->interp, xs_init, 3, embedding, NULL);
eval_pv("*CORE::GLOBAL::exit = sub{ print STDERR '(EXIT INTERCEPT)'; }", FALSE);
...
# ... fopen/fstat/buf=malloc/fread
eval_sv(newSVpv(buf,0), G_KEEPERR | G_EVAL | G_SCALAR);

if( SvTRUE(ERRSV) ) {
   fprintf(stderr, "compile(%s) fail: %s\n", filepath, SvPV_nolen(ERRSV));
   ...
}
```

Note I found eval_pv() does not catch any sytax error in perl source, so using eval_sv() with G_KEEPERR (it returns not many info as I want but at least has signal error)..


----------

