[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Finding out the number of requested return values for a Lua-Perl bridge
- From: Rob Hoelz <rob@...>
- Date: Wed, 28 Apr 2010 11:21:18 -0500
Hello lua-l,
So, first some background: I want to write a library that binds a Perl
interpreter to Lua. Now, among the various challenges that come along
with this, the first one that comes to mind is Perl's notion of context.
For those of you who don't know Perl, it has this thing called
context. Subroutines can be called in either void, scalar, or list
context. A subroutine can detect this context and behave differently
if it likes. For example:
my $count = keys %hash; # returns the number of keys in a hash
my @keys = keys %hash; # returns a list of keys in a hash
keys %hash; # resets the internal iterator in a hash
So, let's write up a quick example with my fake Lua-Perl bridge (name
suggestions would be appreciated):
require 'perl'
perl.eval([[
sub foo {
if(wantarray) { # wantarray is a function that returns the context of the calling function
return (1, 2);
} else {
return 3;
}
}
]])
local foo = perl.main.foo
If I were to call foo in Perl, I could do it several ways:
my $a = foo; # $a is 3
my @values = foo; # $values[0] is 1, $values[1] is 2
my ( $a, $b ) = foo; # $a is 1, $b is two
my ( $a ) = foo; # $a is 1, the second return value is discarded
The equivalent statements in Lua:
local a = foo()
local values = { foo() }
local a, b = foo()
local a = ({ foo() })[0] -- not sure how else to do this, really...
Since foo on the Lua side is a lua_CFunction, it needs to know how many return values are being asked for...I think this isn't currently possible in Lua, but I figured I'd ask.
Thanks,
Rob
Attachment:
signature.asc
Description: PGP signature