NAME
Inline::Interp - Make Inline modules for interpreted languages easily
SYNOPSIS
package Inline::Foo;
require Inline;
require Inline::Interp;
@ISA = qw(Inline Inline::Interp);
sub register {
return {
language => 'Foo',
aliases => ['Foo', 'foo'],
type => 'interpreted',
suffix => 'foo',
};
}
sub do_load {
my ($funcs, $code) = @_;
while($code =~ m/function(\s+)([a-z0-9_]+)(\s*){(.*?)}/isg){
Inline::Interp::add_func($funcs, $2, $4);
}
}
sub load {
Inline::Interp::load(@_);
}
sub do_run {
my ($code, $io) = @_;
# evaluate $code here
# output a char
Inline::Interp::output_char($io, 'A');
# input a char
my $in = Inline::Interp::input_char($io);
}
DESCRIPTION
This module allows you to easily create an Inline module for an interpreted language. It handles all the messy Inline internals for you and provides a simple character IO layer.
FUNCTIONS YOU NEED TO IMPLEMENT
- register()
-
The standard Inline register routine which names your class
- do_load($funcs,$code)
-
Called when your class needs to cut up the code (in $code) into functions. For each function it finds it should call Inline::Interp::add_func, passing along the $funcs argument.
- load()
-
This is just a stub through to Inline::Interp::load. We need this because Inline doesn't always make object calls so inheritance doesn't work.
- do_run($code,$io)
-
Called when a function should be interpreted. Code is passed in $code. $io can be used in calls to Inline::Interp::input_char and Inline::Interp::output_char.
FUNCTIONS YOU CAN CALL
- add_func($funcs,$name,$code)
-
Registers a function. $funcs is the argument passed to do_load(). $name is the name of the function to register and $code contains the source code for the function (in the interpreted langauge, not perl!)
- input_char($io)
-
Returns a character from the input stream, or a null character if there is no more input.
- output_char($io,$char)
-
Outputs a character, depending on the IO settings.
USING AN Inline::Interp FUNCTION
To use a function declared through an Inline::Interp module, just call it like you would any perl function.
Passing arguments
The first parameter passed to an Inline::Interp function is converted to a stream of bytes. This stream is then accessable to the function via the IO layer.
If you pass a hash instead of a string, then Inline::Interp can change it's IO behavoir. The following keys are recognised:
- input
-
A plain old input buffer (a string)
- echo
-
Set to 1 to enable echoing of output to the screen. It is turned off by default when passing a hash.
- input_callback
-
A function ref which is called each time a character of input is needed. The function should return a 0 to indicate end of input.
- output_callback
-
A function ref which is called whenever a byte needs outputting. The byte is passed as a single character string in the first argument.
Return values
An Inline::Interp function returns it's output buffer as a string. If echo was enabled, or if it was implicitly on by using the scalar calling method, then this buffer will have already been echo'd. The buffer is always returned, regardless of the state of the echo flag or the existence of an output callback.
AUTHOR
Copyright (C) 2003, Cal Henderson <cal@iamcal.com>