in reply to Re^3: Refresh a Module
in thread Refresh a Module

Why bother with un defining a sub when you can just delete it? 🤔

Replies are listed 'Best First'.
Re^5: Refresh a Module
by LanX (Saint) on Sep 26, 2024 at 09:36 UTC
    > Why bother with un defining a sub when you can just delete it?

    Two very, very different things.

    The delete removes the typeglob from the stash. This doesn't necessarily destroy the typeglob if it's still referenced elsewhere (like in another stash, compare refcount and garbage collection).

    Anyway, the sub's ref is in the CODE slot of the typeglob!

    Same story, destroying the typeglob doesn't mean destroying the sub, if it's referenced elsewhere.

    (Think of a typeglob like of a hash with only 6 slots, CODE is one of them)

    I could do a lot of experiments now to determine the internal mechanisms, question is rather if we are even allowed to rely on them. And others here know the C code better.

    Fact is that we already have clean interfaces for subs

    • undef to destroy
    • redefine to overwrite
    Those mechanisms are guaranteed to work.

    Please keep in mind that typeglobs are a Perl4 mechanism, refs came with Perl5. Not sure to which degree refcounts and garbage collection are applied to typeglobs and/or their slots.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery