Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > where is my magic (XS related)

Reply
Thread Tools

where is my magic (XS related)

 
 
Pavel Hlavnicka
Guest
Posts: n/a
 
      03-01-2005
Hi all,

Abstract: If I assign some magic to a value returned from FIRSTKEY (tie
method) this magic is not present as this value is passed to NEXTKEY.

My XS provides functions for hash 'tie' magic (TIEHASH, FETCH...), all
went fine until I started with FIRSTKEY and NEXTKEY. My extension works
with a complex C++ library and for a hash iteration I need to keep some
library specific iteration key. My idea was to use `~` or `U` magic and
associate it with a key returned from FIRSTKEY and use it later in
NEXTKEY. Like this:

SV *
FIRSTKEY(obj)
SV * obj
PREINIT:
AG_SLOT_ENTRY entry;
char * name;
CODE:
// -------- following really does not matter -------
frame = SV2FRAME(obj);
entry = frame -> GetStartSlot(); //C++ API I use
if (entry) {
name = frame -> getName(entry);
RETVAL = newSVpv((const char*)name, 0);
// ------- here I assign some magic to RETVAL --------
sv_magic(RETVAL, sv_2mortal(newSVuv(PTR2UV(entry))),
PERL_MAGIC_ext, NULL, 0);
} else {
RETVAL = &PL_sv_undef;
}
OUTPUT:
RETVAL


SV*
NEXTKEY(obj, last)
SV * obj;
SV * last;
PREINIT:
AG_SLOT_ENTRY entry;
char * name;
CODE:
frame = SV2FRAME(obj);
MAGIC * magic = mg_find(last, PERL_MAGIC_ext);
// --------- OOPS magic is always null ------------


What's really confusing is that SvTYPE(last) still claims some magic is
present, but there is no magic assigned to this SV at all
(SvMAGIC(last) is NULL)

Using Perl 5.8.5, Fedora 3 vanilla.

Any answer to this long message would be really appreciated!!
Thx

Pavel

P.S.:
I have a XS wrapper round C++ library, my XS is compiled with g++ as
described e.g. here <http://www.johnkeiser.com/perl-xs-c++.html>
Actually I do not suppose this affects my problem.
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      03-01-2005


Pavel Hlavnicka wrote:
> Abstract: If I assign some magic to a value returned from FIRSTKEY (tie
> method) this magic is not present as this value is passed to NEXTKEY.


Abstract: AFAIK, magic applies to lvalues not to rvalues. When the the
lvalue is resolved to and rvalue, such as when value of the magic SV is
copied to another SV it looses it's magic.

I don't know what would happen if you try to return an value with magic
from a function that's expected to return an rvalue. The behaviour is
probably undefined.

If you want something like magic but which is preserved as the value is
copied from SV to SV you need to go for an object with overload. I've
never done this from XS.

 
Reply With Quote
 
 
 
 
Joe Schaefer
Guest
Posts: n/a
 
      03-01-2005
Pavel Hlavnicka <(E-Mail Removed)> writes:

> Abstract: If I assign some magic to a value returned from FIRSTKEY
> (tie method) this magic is not present as this value is passed to
> NEXTKEY.
>
> My XS provides functions for hash 'tie' magic (TIEHASH, FETCH...), all
> went fine until I started with FIRSTKEY and NEXTKEY. My extension
> works with a complex C++ library and for a hash iteration I need to
> keep some library specific iteration key. My idea was to use `~` or
> `U` magic and associate it with a key returned from FIRSTKEY and use
> it later in NEXTKEY. Like this:


Try adding it to the object instead of the key. MAGIC isn't
preserved across an assignment, which is what perl must do here:
it makes an internal copy of FIRSTKEY's (mortal) RETVAL, to
eventually pass to NEXTKEY as "last".

--
Joe Schaefer
 
Reply With Quote
 
Pavel Hlavnicka
Guest
Posts: n/a
 
      03-02-2005
Thanks,

you're right it seems. Actually meanwhile I've noticed formerly
overlooked fact that perl allows only single concurrent iteration of the
hash so storing my internal key with an object (not with key) is
sufficient (or even conforming if you want).

What's strange (but understandable) is that my RETVAL with magic is
copied with its type (SvMAGIC returns true) but no magic is retained.
Keeping SV upgraded to PLMG seems to be obsolete. But it really doesn't
hurt much - it was just a bit confusing.

Thank you for your help, I really appreciate it.

Pavel

>
> Try adding it to the object instead of the key. MAGIC isn't
> preserved across an assignment, which is what perl must do here:
> it makes an internal copy of FIRSTKEY's (mortal) RETVAL, to
> eventually pass to NEXTKEY as "last".
>

 
Reply With Quote
 
Joe Schaefer
Guest
Posts: n/a
 
      03-02-2005
Pavel Hlavnicka <(E-Mail Removed)> writes:

> What's strange (but understandable) is that my RETVAL with magic is
> copied with its type (SvMAGIC returns true) but no magic is retained.

^^^^^^^

Do you mean SvMAGICAL(sv), or are you referring to SvTYPE(sv, SVt_PVMG)?
SvMAGICAL probably shouldn't be true, but SvTYPE certainly should be.

> Keeping SV upgraded to PLMG seems to be obsolete.


Some internal magics are preserved (taint, v-string), but that's not
really the point. SvTYPE just tells how much room is available in
the sv_any slot (which is tiered via SvUPGRADE). It doesn't say
anything about the sv's actual semantics.

--
Joe Schaefer
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
Network Magic 2.0 Gene Hora Wireless Networking 10 01-02-2006 11:03 PM
Wrapper on magic line? Nilsson Mats Perl 0 12-09-2003 02:48 PM
"Magic" password to the IBM 8265 switch - HELP ! gaucho Cisco 2 10-17-2003 08:29 PM



Advertisments