Perl SDK: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
Currently there is not a Perl SDK as such, but you can use the following code to call a function on an MTA server, assuming you have access:
Currently there is not a Perl SDK as such, but you can use the following code to call a function on an MTA server, assuming you have access:


<syntaxhighlight lang="lua">[perl]
<syntaxhighlight lang="lua" lang="perl">
using JSON;
using JSON;


Line 26: Line 26:


Syntax:
Syntax:
<syntaxhighlight lang="lua">[perl]
<syntaxhighlight lang="lua" lang="perl">
my $ret = callFunction("hostname:httpPort", "resourceName", "functionName" [, args ...]);
my $ret = callFunction("hostname:httpPort", "resourceName", "functionName" [, args ...]);
</syntaxhighlight>
</syntaxhighlight>
The function returns an array of the values returned by the function.
The function returns an array of the values returned by the function.

Revision as of 18:02, 18 July 2009

Currently there is not a Perl SDK as such, but you can use the following code to call a function on an MTA server, assuming you have access:

using JSON;

sub callFunction {
	my ($server, $resourcename, $functionname) = @_;
	my $content = objToJson([@_[3..$#_]]);
	$ua = LWP::UserAgent->new;
	$ua->parse_head(0);
	$ua->agent("");
	my $url = "http://" . $server . "/" . $resourcename . "/call/" . $functionname;
	$req = HTTP::Request->new(POST => $url);
	$req->content_type('application/x-www-form-urlencoded');
        $req->content($content);
	$req->header('Accept' => 'text/html');
	$res = $ua->request($req);
	my $ret = "";
	eval {
		$ret = jsonToObj($res->content);
	};
	return $ret;
}

This requires that you install the JSON package. You can do this easily by navigating to your perl 'bin' directory and executing "cpan", then typing "install JSON".

Syntax:

my $ret = callFunction("hostname:httpPort", "resourceName", "functionName" [, args ...]);

The function returns an array of the values returned by the function.