I just patched my copy of AI::Prolog to allow calls back into arbitrary perl code. The following prolog query works just like you'd expect the complementary perl code to operate. I think that perlcall2/2 should succeed if it can call the function and fail if it can't. There oughta be a related perlcall/2 which is implemented by preventing backtracking through the function. Probably for convenience there oughta be some kind of interpolation of perl variables into strings. Probably also some predicates with arity 1 for a simple case when there's no arguments. Uh... other goo to make things simple. Whatever. This is just a primitive. I suppose there oughta be a method calling primitive too.
% In prolog...
perlcall2( "my_function", [ "my", "args" ] ).
# In perl...
my_function( 'my', 'args' )
This is so Mapping URLs to code with Prolog could possibly work. The diff against AI-Prolog-0.734 is at http://diotalevi.isa-geek.net/AI-Prolog-perl-callbacks.diff. It suffers from just searching upwards in the call stack to find the function so it could find the wrong thing.