XML-XQL

 view release on metacpan or  search on metacpan

lib/XML/XQL/Plus.pm  view on Meta::CPAN


# Note that certain functions (like mkdir) are not considered "constant"
# because we don't want their invocation values cached. (We want the
# function to be called every time the Invocation is solved/evaluated.)
my %PerlFunc =
(
 # Format: 
 #  "funcName", => [ARGCOUNT, RETURN_TYPE [, CONSTANT = 0, [QUERY_ARG = 0]]]

 #-------- Arithmetic Functions

 "abs" => [1, "Number", 1], 
 "atan2" => [2, "Number", 1, -1], 
 "cos" => [1, "Number", 1], 
 "exp" => [1, "Number", 1], 
 "int" => [1, "Number", 1], 
 "log" => [1, "Number", 1], 
 "rand" => [[0, 1], "Number", 0, -1], 
 "sin" => [1, "Number", 1], 
 "sqrt" => [1, "Number", 1], 
 "srand" => [[0, 1], "Number", 0, -1], 
 "time" => [0, "Number", 0, -1], 

 #-------- Conversion Functions

 "chr" => [1, "Text", 1], 
# "gmtime" => [1, "List of Number", 1], 
 "hex" => [1, "Number", 1], 
# "localtime" => [1, "List of Number", 1], 
 "oct" => [1, "Number", 1], 
 "ord" => [1, "Text", 1], 
 "vec" => [3, "Number", 1], 
 "pack" => [[1, -1], "Text", 1, -1], #?? how should this work??
# "unpack" => [2, "List of ?", 1], 

 #-------- String Functions

 "chomp" => [1, "Text", 1], 
 "chop" => [1, "Text", 1], 
 "crypt" => [2, "Text", 1], 
 "lindex" => [[2, 3], "Number", 1],	# "index" is already taken by XQL
 "length" => [1, "Number", 1], 
 "lc" => [1, "Text", 1], 
 "lcfirst" => [1, "Text", 1], 
 "quotemeta" => [1, "Text", 1], 
 "rindex" => [[2, 3], "Number", 1], 
 "substr" => [[2, 3], "Text", 1], 
 "uc" => [1, "Text", 1], 
 "ucfirst" => [1, "Text", 1], 
 "reverse" => [1, "Text", 1], 
 "sprintf" => [[1, -1], "Text", 1, -1],

 #-------- Array Functions

 "join" => [[1, -1], "Text", 1], 
# "split" => [[2, 3], "List of Text", 1], 

 #-------- File Functions

 "chmod" => [2, "Boolean", 0, 1],
 "chown" => [3, "Boolean", 0, 2],
 "link" => [2, "Number", 0, -1],		#?? no return value
# "lstat" => [1, "List of Number"], 
 "mkdir" => [2, "Boolean"],		#?? or is 1 arg also allowed?
 "readlink" => [1, "Text"], 
 "rename" => [2, "Boolean", 0, -1],
 "rmdir" => [1, "Boolean"],
# "stat" => [1, "List of Number"], 
 "symlink" => [2, "Boolean", 0, -1],
 "unlink" => [1, "Boolean"],
 "utime" => [3, "Boolean", 0, 2],
 "truncate" => [2, "Number"],		#?? no return value

 #-------- System Interaction

 "exit" => [[0, 1], "Number"], 
# "glob" => [1, "List of Text"], 
 "system" => [[1, -1], "Number", 0, -1], 
# "times" => [0, "List of Number"],

 #-------- Miscellaneous

 "defined" => [1, "Boolean"],	# is this useful??
 "dump" => [[0, 1], "Number", 0, -1], 
 "ref" => [1, "Text"],
);
#?? die, warn, croak (etc.), 
#?? file test (-X), tr// (same as y//)
#?? array functions, sort

# Generate wrapper for Perl builtin function on the fly
sub generatePerlWrapper
{
    my ($name) = @_;
    my $args = $PerlFunc{$name};
    return undef unless defined $args;	# not found

    my ($argCount, $returnType, $const, $queryArg) = @$args;
    my $funcName = $name;
    if ($name eq "lindex")	# "index" is already taken
    {
	$funcName = "index";
    }    
    generateFunction ($name, $funcName, $returnType, $argCount, 0, $const, 
		      $queryArg);
    $Func{$name};
}

#?? Inline functions, do they make sense? E.g. 'elem!sub("code", "arg1")'
#?? Normally, user should use defineFunction, but if most of them have
#?? a lot of common code, I could provide the pre- and post-code.
#?? After processing the user-supplied code block, how should I convert the
#?? user's result back to an Invocation result. E.g. do I get a single value
#?? or a list back?

defineFunction ("eval",  \&XML::XQL::xql_eval,		[1, 2]);
defineFunction ("subst", \&XML::XQL::subst,		[3, 5], 1);
defineFunction ("s",	 \&XML::XQL::subst,		[3, 5], 1);
defineFunction ("match", \&XML::XQL::match,		[1, 2]);
defineFunction ("m",     \&XML::XQL::match,		[1, 2]);
defineFunction ("map",   \&XML::XQL::xql_map,		2,      1);



( run in 0.880 second using v1.01-cache-2.11-cpan-5511b514fd6 )