in reply to Embedded perl: allowing 'exit' in eval_pv without exiting C program

die should be used in that situation.

But let's say you can't edit the Perl code. Instead of evaluating the code directly, call myeval (defined below) with the the string to evaluate as an argument.

our $override_exit = 0; BEGIN { *CORE::GLOBAL::exit = sub(;$) { no warnings qw( exiting ); last EXIT_OVERRIDE if $override_exit; CORE::exit($_[0] // 0); }; } sub myeval { my $exit_was_called = 1; EXIT_OVERRIDE: { local $override_exit = 1; eval($_[0]); die $@ if $@; $exit_was_called = 0; } die("Exit called\n") if $exit_was_called; }

Seeking work! You can reach me at ikegami@adaelis.com

Replies are listed 'Best First'.
Re^2: Embedded perl: allowing 'exit' in eval_pv without exiting C program
by daveola (Sexton) on Mar 27, 2021 at 21:49 UTC
    But then what happens if they call exit(0)? Then we die() and get a failure from the eval.

    I suppose I can just catch that and not use the die()??

    I hate the idea of another layer of eval, but I guess that's what I need at this point?

      die("Exit called\n") is a placeholder for however you want to handle it, though it does allow the XS code to handle the situation (since XS can catch exceptions).

      Seeking work! You can reach me at ikegami@adaelis.com