Parse::Tinymush - A simple tinymush parser
use Parse::Tinymush; my $functions = { cat => [sub { "@_" }, -1], add => [sub { $_[0] + $_[1] }, 2], }; my $parser = Parse::Tinymush->new(functions => $functions); print $parser->parse("add(1,2)");
The Parse::Tinymush module is an implemenation of the tinymush parser written in perl. This implementation comes with no built-in %-variables or functions, but they can be easily added by passing arguments to the constructor.
new is the constructor. It sets up various instance variables. The constructor takes a number of optional arguments:
new
This must be a reference to a hash with the form:
{ function_name => [code_ref, argument_count, flags] }
function_name must be lowercase for the parser to work correctly. argument_count can be one of the following:
function_name
argument_count
* -1, which means any number of arguments
* A number, which means that exact number of arguments
* An array ref, whose first entry is the minimum number of arguments and second entry is the maximum number of arguments.
The flags field is optional. The possible values for it are:
flags
* FNC_NO_FLAGS (0) - Same as not having any flags
* FNC_PASS_NAME (1) - Pass the function name as the first argument to the function. This allows the same code to have different function names. This is useful if you wish two functions to have similar features, but do not want to repeat code.
Example:
my $functions = { "add" => [ \&fnc_add, 2, ], "cat" => [ \&fnc_cat, -1, FNC_NO_FLAGS ], "lcon" => [ \&fnc_lcon, [1, 2], FNC_PASS_NAME ], };
The variables hash is used to handle evaluation of the %-variables. It must be a reference to a hash with the form:
variables
{ var_name => var_eval }
var_name is case-sensitive. For both "C" and "c" to mean the same thing, they must both be in the table. var_eval can be one of the following:
var_name
var_eval
* A simple scalar
* An array ref, in which case the current character is used as the index
* A hash ref, in which case the current character is used as the key
* A code ref, in which case the code is called, passing it the current character and the Parse::Tinymush object as $_[0] and $_[1]
* An object with an eval method, in which case the method is called and passed the current character and the Parse::Tinymush object
eval
my @array = ( 0 .. 9 ); my %hash = ( y => "z" ); my $variables = { "!" => $$, "0" => \@array, "y" => \%hash, "l" => sub { getcwd; ), "o" => HTML::Parser->new(), };
options is a hash reference of various parser options. Currently, the only supported option is space_compression.
options
space_compression
* space_compression - default: on - When on, multiple spaces will be compressed into one space.
my $parser = Parse::Tinymush->new(options => {space_compression => 0});
parse parses the given string, returning a string as the result.
parse
my $output = $parser->parse("cat(1,2,3,4,5)");
# $output = 12345
None
The tinymush project: <http://www.godlike.com/tm3/>
Eric Kidder, <kageneko.at.evilkitten.org>
Copyright (C) 2003 by Eric Kidder
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
=back doesn't take any parameters, but you said =back 4
To install Parse::Tinymush, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::Tinymush
CPAN shell
perl -MCPAN -e shell install Parse::Tinymush
For more information on module installation, please visit the detailed CPAN module installation guide.