gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm working on a module that need to add a new flag to a SV that is stored by it. Actually I just need to find a way to mark that SV by this module, so, will be possible to identify this SVs from the others.

*Why use a flag?

Because if I just use the number of a SV and its type, I can't guarantee that is the same SV that I have stored, since Perl can reuse that SV after clean it. If I just use the type and the number is possible that until the next access to this SV, that SV was actually destroied, than a new SV can be created with the same type and number. So, adding a flag I can guarantee that it wasn't cleanned until that, since when a SV is cleanned its flag is reseted.

I need to know if someone already done something like that, and if you, all the developers that hack Perl internals, think that this is the best way to do that, and if it is, if this is a secure thing to do.

Also I need to know if exits some reserved range for external flags defined in Perl, since I can't use a number for the flag that is in use, or any range that can be in use in the future.

*Why do that?

Is for the module Hash::NoRef, where I need to store a value/object without actually make a reference to it. I previously tried to store a reference value and decrement its REFCNT, but this will create a lot of problems, including CORE dump of the interpreter, specially if we try to access this value after the destruction of it, since is impossible to know if a reference was destroied when the REFCNT is artificialy changed, and also we can access a recycled SV. As a new approach I will store the type and number of the SV, and mark that SV to guarantee that it wasn't destroied. After this I can access the SV getting it directly from the list of SVs in use.

Thanks in advance and wish me luck! ;-P

UPDATE:
Resolved with weak references. Thanks to broquaint and bart that helped in the CB.

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: PerlHack - Adding new flag into a SV
by broquaint (Abbot) on Apr 23, 2004 at 12:34 UTC
    And here is the chatterbox conversation that gmpassos is talking about in his update (editted to contain the relevant content/folk)

    [11:44:57] gmpassos: Yes, but I also can't change the SV of the user!
    [11:45:01] broquaint: bart: correct re DBI having magic
    [11:45:30] bart: If you're allowed to use it for whatever you like, than other people can too, and there's no guarantee these two won't clash.
    [11:45:58] gmpassos: The user will paste to me any type of SV (HV, AV...), I just need to mark it, but I can't change it since I can break some behavior
    [11:47:00] gmpassos: bart, in Perl we always can do everything, we can't start to discuss in this area! ;-P
    [11:47:39] broquaint: gmpassos: shouldn't you be storing a reference of some kind to what is pasted to you instead of marking it? it seems a bit of an odd approach ...
    [11:47:55] gmpassos: broquaint, if I add a magic to a SV, I change the SV or create a new SV from it?
    [11:48:14] bart: You won't get a blessing from P5P, I'm sure. And those are the people who decide.
    [11:48:39] gmpassos: broquaint Actually the main idea is to not store a reference. Read the last text blopck of the node...
    [11:49:36] broquaint: aah (just read the rest of the node), just store the pointer bit i.e push @ptrs, ref($thing) =~ /\(([a-z0-9]+)\)$/; and then use something like Devel::Pointer to access it again
    [11:49:43] gmpassos: bart I don't need to change Perl to add a new flag.
    [11:49:44] bart: A weak reference?
    [11:50:32] gmpassos: broquaint Now read the 2nd block (Why use a flag) ;-P
    [11:50:44] bart: You don't? How can you be sure then what you're doing won't clash with what perl does itself, in future versions?
    [11:52:19] gmpassos: bart This is why I'm asking if exists some reserved range. The only thing that I can guarantee is to check if it wasn't broken in the future
    [11:52:54] bart: Anyway, there's also the option that perl has simply destroyed the SV, without telling you.
    [11:53:09] broquaint: gmpassos: then use a weak reference (a la Scalar::Util's weaken) as bart suggested
    [11:54:15] bart: So... what does DBI's "magic" do?
    [11:56:13] gmpassos: weaken?
    [11:56:45] gmpassos: where I can read about a weaken reference
    [11:56:52] broquaint: gmpassos: you *could* take the magic route as you can always add magic to magic, but that seems somewhat dangerous and *can* change the behaviour of the SV
    [11:57:47] broquaint: gmpassos: in the Scalar::Util documentation? what it does is decreases the refcnt of the thing that holds the reference so what it points to will get cleaned up correctly
    [11:58:04] bart: WeakRef, later absorbed into Scalar::Utils
    [12:03:31] gmpassos: Well, I think that weak ref is just what I need! thanks!~But I need to know if this will work in my tests! eeheheh

    Time passes while tests are fiddled and fixed

    [12:53:37] gmpassos: broquaint With wek references is just working wonderful! thanks for the advice
    [13:00:05] broquaint: np gmpassos, reckon I should post a reply?
    [13:00:58] gmpassos: broquaint Will be nice, just to end the thread and keep what we have talked on CB.
    [13:01:03] bart: Well, somebody should.
    [13:01:52] gmpassos: broquaint All my tests and the tests of the 2 modules that use Hash::NoRef are ok now. And I 'don't need to change anything! heeh!
    [13:02:29] gmpassos: Well, I have added at least an update
    [13:02:33] broquaint: nice one, gmpassos. I'll do a quick post now (should I include CB chatter?)
    [13:03:09] gmpassos: To what you think that you have to do! 8-P
    [13:03:26] gmpassos: s/to/do/i
    There you have it, conclusive proof that the CB is more than just a meeting room.
    HTH

    _________
    broquaint

Re: PerlHack - Adding new flag into a SV
by mpeppler (Vicar) on Apr 23, 2004 at 11:32 UTC
    Personally I'd look into using magic to handle this. I know that magic is not very well documented, but at least it is there for this purpose. IIRC the '~' magic is reserved for third-party use - that's what I'd use to hook the additional information that you need.

    Check out the sv_magic() and mg_xxx() calls in perlguts and perlapi.

    Michael