[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Finding out the number of requested return values for a Lua-Perl bridge
- From: Brian Maher <brimworks@...>
- Date: Wed, 28 Apr 2010 21:44:43 -0700
You could export several variations of your function. For example:
foo_scalar() and foo_array() would call the perl function foo() with
wantarray to false and true respectively. That could get ugly, unless
you did something like a "type map" which is a description of perl
function + contexts mapped to lua names.
Or you could make it be an optional parameter:
foo({wantarray = true})
...but that would mean all functions would need to take a table as the
first argument, unless tables are not allowed to be passed to perl.
Or you could export it via some sort of "builder" strategy (not sure
that this is the right term):
perl("main::foo"):wantarray(true):call()
The perl function would take the name of the desired function as an
argument and return an object that has a wantarray() method that can
be called to change the calling context, and it would also have a
call() method for actually making the subroutine call with a given set
of parameters. If wantarray() returns "self", then you can do method
chaining like that above.
IMHO, the "builder" strategy is probably the most flexible in the long run.
Cheers,
-Brian
On Wed, Apr 28, 2010 at 9:21 AM, Rob Hoelz <rob@hoelzro.net> wrote:
> 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
>
--
Brian Maher >> Glory to God <<