in reply to Mapping URLs to code - in search of perlish dispatch schemes

Let's you and me get Ovid to release a copy of AI::Prolog which can callback into perl. Interpolation of variables in strings would be nice too.

% Edit! request :- url( 'edit' ), not( node( _ ) ), error( "Edit *which* node?!" ). request :- url( 'edit' ), node( Node ), do_edit_node( Node ), % dispatch perl print( " ... " ). % Create! request :- url( 'create' ), not( code( _ ) ), error( "You didn't give any code!" ). request :- url( 'create' ), not( name( _ ) ), error( "You didn't give a name!" ). request :- url( 'create' ), name( Name ), code( Code ), do_create_node( Name, Code ), % dispatch perl print( "Created $Name { $Code }" ). request :- error( "I can't handle the stress!" ).

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: Mapping URLs to code - in search of perlish dispatch schemes
by diotalevi (Canon) on Jul 28, 2006 at 07:01 UTC

    With AI::Prolog-0.735_01, the following would probably work. It's still an outline. It just uses the primitive perlcall/2 which is allowed to backtrack so I'm using cuts to prevent that. Versions later than that might have received this.

    % prolog follows. perlcall( Function, Args ) :- perlcall2( Function, Args ), !. do_edit_node( Node ) :- perlcall( "do_edit_node", [ Node ] ). error( Message ) :- perlcall( "error", [ Message ] ). do_create_node( Node, Code ) :- perlcall( "do_create_node", [ Node, Code ] ).

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^2: Mapping URLs to code - in search of perlish dispatch schemes
by Ovid (Cardinal) on Jul 28, 2006 at 11:23 UTC

    I wish I had seen this earlier. The patch I've looked at seems interesting and I'll see if I can fit it into to an "official" release this weekend. Does it support stuff like functions which keep returning different values, such as a call into a database? I've had some thoughts about that, but it would be tricky.

    Cheers,
    Ovid

    New address of my CGI Course.

      Earlier? I wrote the patch hours before you noticed it. I was just another iteration of wishing outloud for that feature we've talked about before but have now. There are some major deficiencies with my patch. I think I need to call your unify function but didn't. I think that the normal perlcall/2 should only succeed and call the perl function if all the variables are bound. I think I made a mess of converting your Terms back into perl data. I did something that worked but I didn't understand your organization (you didn't document it!) and probably got it wrong. The current perlcall2/2 searches upwards through the call stack to find the function named X. That should probably be saner - maybe introduce a perlpackage/1 builtin. Maybe tell perlcall2/2 how to tell the difference between fully qualified function names and relative names. There should be a perlmethod2/2 as well to support method calls. Yes?

      Much of this can be experimented with once there's a few builtins for looking back at perl.

      perlcall(X,Y) :- boundp(X), % uh... a predicate for asking that X need no further u +nification so perlcall only gets complete answers boundp(Y), perlcall2(X,Y), !.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊