On 2006-05-01, at 09:34:21 +0100, Nick Ing-Simmons wrote: > Vaclav Barta <[EMAIL PROTECTED]> writes: > >Hi, > > > >seems rather quiet in here - anybody listening? Anyway, I've written an XS > >module for perl 5.8.7 (Regexp::Compare, on CPAN) using regular expressions. > >It compiles them (from caller-supplied strings) with pregcomp, and I gather > >the compiled regexps should be released with pregfree. That works, but I'm > >concerned about invalid values. For some inputs (i.e. '[a'), pregcomp croaks > >and control returns to the perl caller, which is fine, except my function > >compiles *two* regexps and when the second one croaks, it doesn't get a > >chance to release the first... Is there some equivalent of try/catch in XS, > >or a way to register the compiled regexp as a Perl object, to be released > >automatically when not needed? > > try/catch (i.e. eval {} in perl speak) is a little tricky to do from pure XS. > I usually call (a trivial) perl sub and use G_EVAL on the call.
There's a way to do try/catch (under certain circumstances) in XS. If you use a recent ppport.h, it's even portable back to perl 5.003: XCPT_TRY_START { risky_stuff(); } XCPT_TRY_END XCPT_CATCH { /* cleanup things here */ XCPT_RETHROW; } The only limitation is that you _have to_ rethrow the exception in the catch block, i.e. you cannot catch an exception and just ignore it. This is documented in the perlguts and perlapi manpages. Marcus -- With your bare hands?!?