Feature Suggestion - Custom Object Handlers|Kirsle|casey@cuvou.net|1237155743|Kirsle|xx|0|69.232.35.156|[b]Note:[/b] YaBB doesn't like script tags and I don't feel like hacking it to like them, so I used [script] instead of the proper HTML equivalent.<br /><br />Here's an idea I had last night for a feature for the Perl RiveScript interpreter: being able to define custom handlers for object types from different programming languages.<br /><br />So, for instance, if you use RiveScript on the web (possibly with an ajax front-end), you could let RiveScript know how to handle &quot;javascript&quot; objects by setting your own handlers for it. The syntax from the Perl side might look sorta like this:<br /><br />[code]#!/usr/bin/perl -w<br /><br />use CGI;<br />use RiveScript;<br />my $q = new CGI();<br /><br />my $rs = new RiveScript();<br /><br /># Handle JavaScript objects.<br />$rs-&gt;setObjectHandler (javascript =&gt; \&amp;handle_javascript);<br />my $js_objects = &#123;&#125;;<br /><br /># The usual stuff...<br />$rs-&gt;loadDirectory(&quot;./replies&quot;);<br />$rs-&gt;sortReplies();<br /><br /># Get a reply.<br />my $reply;<br />if ($q-&gt;param('user') &amp;&amp; $q-&gt;param('message')) &#123;<br />   $reply = $rs-&gt;reply($q-&gt;param('user'),$q-&gt;param('message'));<br />&#125;<br />print $q-&gt;header();<br />print $reply;<br /><br />sub handle_javascript &#123;<br />   my ($rs,$action,$name,$data) = @_;<br /><br />   # $action will be &quot;load&quot; during the parsing phase, or &quot;call&quot;<br />   # when called via a &lt;call&gt; tag in a reply.<br /><br />   # loading<br />   if ($action eq &quot;loading&quot;) &#123;<br /> &nbsp; &nbsp; &nbsp;# In this case, $data is the source code from the &gt;object tag<br /> &nbsp; &nbsp; &nbsp;$js_objects-&gt;&#123;$name&#125; = $data; # store the code by name<br />   &#125;<br />   else &#123;<br /> &nbsp; &nbsp; &nbsp;# The user is calling the code via &lt;call&gt;.<br /> &nbsp; &nbsp; &nbsp;my $return = qq&#123;<br />[script type=&quot;text/javascript&quot;]<br />document.writeln( function() &#123; $js_objects-&gt;&#123;$name&#125; &#125;);<br />[/script]<br />&#125;;<br />    return $return;<br />&#125;[/code]<br /><br />So in this example, the Perl script adds a handler so that RiveScript will know about the &quot;javascript&quot; type when it sees an &gt;object tag. When loading reply files, if it finds a javascript object, it will call the &amp;handle_javascript method there with the action of &quot;load&quot;. The Perl script simply stores a copy of the object's source code in a hashref. Then, if the user provokes a reply that &lt;call&gt;'s the javascript object, &amp;handle_javascript is called with the action of &quot;call&quot;, and the Perl returns some JavaScript source code that would execute the previously loaded code.<br /><br />Here's an example RiveScript source file:<br /><br />[code]// A simple object that returns the user's screen resolution<br />&gt; object screensize javascript<br />   var width = screen.width;<br />   var height = screen.height;<br />   var dims = width + &quot;x&quot; + height;<br />   return dims;<br />&lt; object<br /><br />// A reply that calls this object.<br />+ do you know my screen resolution<br />- Yes, your screen resolution is &lt;call&gt;screensize&lt;/call&gt;.[/code]<br /><br />With this code, and our example Perl above, &amp;handle_javascript is called while the replies are being loaded and given an action of &quot;load&quot;, a name of &quot;screensize&quot;, and the data is that JavaScript code from the object. Then, if the user said &quot;do you know my screen resolution?&quot;, the &amp;handle_javascript is provoked with the action of &quot;call&quot;, name of &quot;screensize&quot;, and the data here would be undef, because in this case there were no other arguments.<br /><br />The handle_javascript method would return this:<br /><br />[code][script type=&quot;text/javascript&quot;]<br />document.writeln( function() &#123;<br />   var width = screen.width;<br />   var height = screen.height;<br />   var dims = width + &quot;x&quot; + height;<br />   return dims;<br />&#125;);<br />[/script][/code]<br /><br />So now, internally, the $reply variable inside RiveScript will now include that whole [script][/script] tag. i.e.<br /><br />[code]$reply = &quot;Yes, your screen resolution is [script]...[/script].&quot;[/code]<br /><br />...because that's what the &amp;handle_javascript sub returned. It'd simply be placed where that &lt;call&gt; tag used to be.<br /><br />And then when this reply is given to the end user to be displayed in a web browser, the JavaScript executes, and the user might see:<br /><br />[b][color=red]InternetUser:[/color][/b] Do you know my screen resolution?<br />[b][color=blue]RiveScript:[/color][/b] Yes, your screen resolution is 1440x900.<br /><br />The exact implementation details are &quot;to do&quot; still, this is just an example.<br /><br />But this way you can create custom handlers for programming languages that the Perl RiveScript interpreter can't directly process. There would be a default handler for &quot;perl&quot;, which does what it already does now: collect all the code, and call $self-&gt;setSubroutine to register it as an object. But if the programmer wanted to set a custom handler for even Perl objects, they could override it easily.<br /><br />Or, you could delete the handler for Perl objects by setting it to undef:<br /><br />[code]$rs-&gt;setObjectHandler(perl =&gt; undef);[/code]<br /><br />And then you'd get Perl warnings during the loading of RiveScript code:<br /><br />[quote]Skipping object of language perl: not known by interpreter, at test.rs line 10.[/quote]<br /><br />Any comments? Ideas? Suggestions?||1237155793|Kirsle|
Test, just a test|XRumerTest|yourmail@gmail.com|1272213632|XRumerTest|xx|0|66.195.158.253|Hello. And Bye.||||
