Discussion:
Using persistent private variables in xs
(too old to reply)
Jeremy White
2006-01-09 16:02:12 UTC
Permalink
Given this code:

{
my $variable;
sub mysub {
# ... accessing $variable
}
}

from:

http://www.unix.org.ua/orelly/perl/cookbook/ch10_04.htm

Is there a way to get hold of the SV for $variable and manipulate it via XS?

I want to be able to write the contents of $variable in XS, then call mysub
via XS and then read the contents of $variable afterwards ($variable needs
to remain a closure as it is shared with other subs in the same block - the
block is evaled into existence during runtime).

All my attempts so far have failed, so I'm either being stupid, or can't it
be done?

Cheers,

jez.
Steven N. Hirsch
2006-01-09 23:11:59 UTC
Permalink
Post by Jeremy White
{
my $variable;
sub mysub {
# ... accessing $variable
}
}
http://www.unix.org.ua/orelly/perl/cookbook/ch10_04.htm
Is there a way to get hold of the SV for $variable and manipulate it via XS?
I want to be able to write the contents of $variable in XS, then call mysub
via XS and then read the contents of $variable afterwards ($variable needs to
remain a closure as it is shared with other subs in the same block - the block
is evaled into existence during runtime).
Since lexical variables don't appear in the symbol table, the only way I
know of to access it from XS would be to stash a reference somewhere and
pass that to the XS routine. Also, I may have simply been up too long,
but I don't believe your example represents a closure. Only (1) instance
of 'mysub' will exist and it's present in the package's symbol table.

Steve
Paul Johnson
2006-01-10 00:11:16 UTC
Permalink
Post by Steven N. Hirsch
Post by Jeremy White
{
my $variable;
sub mysub {
# ... accessing $variable
}
}
http://www.unix.org.ua/orelly/perl/cookbook/ch10_04.htm
Careful. Some people won't talk very nicely of you if you go bandying
URLs like that around.
Post by Steven N. Hirsch
Post by Jeremy White
Is there a way to get hold of the SV for $variable and manipulate it via XS?
I want to be able to write the contents of $variable in XS, then call mysub
via XS and then read the contents of $variable afterwards ($variable needs to
remain a closure as it is shared with other subs in the same block - the block
is evaled into existence during runtime).
Since lexical variables don't appear in the symbol table, the only way I
know of to access it from XS would be to stash a reference somewhere and
pass that to the XS routine.
You could take a look at the code in PadWalker on CPAN to get some
ideas, I think.
Post by Steven N. Hirsch
Also, I may have simply been up too long,
but I don't believe your example represents a closure. Only (1) instance
of 'mysub' will exist and it's present in the package's symbol table.
The docs (used to?) talk about closures being anonymous subroutines, but
I think most people nowadays would says that mysub closes over $variable
and hence it is a closure.
--
Paul Johnson - ***@pjcj.net
http://www.pjcj.net
Steven N. Hirsch
2006-01-10 01:02:31 UTC
Permalink
Post by Paul Johnson
Post by Steven N. Hirsch
Since lexical variables don't appear in the symbol table, the only way I
know of to access it from XS would be to stash a reference somewhere and
pass that to the XS routine.
You could take a look at the code in PadWalker on CPAN to get some
ideas, I think.
Oooooh. Cute. Thanks for the pointer to that!
Post by Paul Johnson
Post by Steven N. Hirsch
Also, I may have simply been up too long,
but I don't believe your example represents a closure. Only (1) instance
of 'mysub' will exist and it's present in the package's symbol table.
The docs (used to?) talk about closures being anonymous subroutines, but
I think most people nowadays would says that mysub closes over $variable
and hence it is a closure.
Yup, too long a day... Agreed.

Steve

Muppet
2006-01-10 00:43:19 UTC
Permalink
Post by Jeremy White
Is there a way to get hold of the SV for $variable and manipulate
it via XS?
Since it's a lexical, no. You can probably get away with an xsub
that returns a reference to an SV allocated and stored in your XS
file, like this:

Foo.xs -=-=-=-=-=-=-=-=-=-

static SV * the_foo;

MODULE = Foo PACKAGE = FOO

SV * get (class)
CODE:
if (!the_foo)
the_foo = newSV ();
RETVAL = newRV (the_foo);
OUTPUT
RETVAL

# more functions that use the_foo

Foo.pm -=-=-=-=-=-=-=-=-=-=-

{
sub mysub {
my $fooref = Foo->get;
$$fooref = "foo!";
}
}

--
"Ears! They help us -- Ears! They help us hear th-- Ea--E--E--E--
Ears!"
-- A Sesame Street singing toy, with Yvonne gleefully pressing
the button over and over and over and over and...
Loading...