Simon Cozens wrote:
> Well, hmm. That's true and it's not true. Consider how
> $a = $b
> works in Perl 5: the gvsv operations produce two SVs, one for the value of
$a
> and for the value of $b. Then the value of $b is assigned to the value of
$a,
> and $a is changed. No difference between lvalue SVs (variables) and rvalue
SVs
> (values).
This thing has bitten me the first time I wrote XS code. I was embedding
some C++ objects in a Perl object. I didn't want the Perl object to contain
the address of the C++ object, because not careful use of it in Perl could
cause problems with memory addresses. So I chose to `hide' the C++ object
address in a magic table. I set up a scalar, called sv_magic with the '~'
slot (user-defined magic), and returned the scalar, like this:
sv_setmagic(ST(0), '~', NULL, address_of_cpp_object);
/* Is this right? I think I can't do XS anymore... */
but when I did
$x = my_sub_that_returns_a_sv_with_magic();
I noticed I couldn't get it to work. The thing is that $x = ... makes a
sv_setsv, what copies the value of the other SV (ST(0) in this case), but
not its magic, and other stuff. Here is the difference between `variable'
and `value'. In Perl5, at least, I could attach magic to a variable, but not
to a value (AFAIK).
Of course I found a workaround. I set up the magic scalar, and returned (a
value!) a reference to it. When I deref it, I have the original magical
thing. But from that day I actually saw that SV is a variable, definitely
*not* a value.
- Branden