From 2f07f21a4449d389a4eeb1391c99d2d0b1d7fbf5 Mon Sep 17 00:00:00 2001 From: Florian Ragwitz Date: Fri, 29 Oct 2010 09:03:23 +0200 Subject: Recommend a more reliable way of identifying magic Also illustrate it with some example code. --- pod/perlguts.pod | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'pod/perlguts.pod') diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 3a5f475dcd..078eb68af0 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -1125,8 +1125,20 @@ Note that because multiple extensions may be using C or C magic, it is important for extensions to take extra care to avoid conflict. Typically only using the magic on objects blessed into the same class as the extension is sufficient. -For C magic, it may also be appropriate to add an I32 -'signature' at the top of the private data area and check that. +For C magic, it is usually a good idea to define an +C, even if all its fields will be C<0>, so that individual +C pointers can be identified as a particular kind of magic +using their C field. + + STATIC MGVTBL my_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 }; + + MAGIC *mg; + for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { + if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &my_vtbl) { + /* this is really ours, not another module's PERL_MAGIC_ext */ + my_priv_data_t *priv = (my_priv_data_t *)mg->mg_ptr; + } + } Also note that the C and C functions described earlier do B invoke 'set' magic on their targets. This must -- cgit v1.2.1