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

Can you elaborate or provide a reference? It's still unclear to me why My::Run() doesn't get redefined here:
require My; print "a: "; My::Run(); # a: Running undef &My::Run; require My; print "b: "; My::Run(); # Undefined subroutine &My::Run called at ...

Replies are listed 'Best First'.
Re^3: Refresh a Module
by LanX (Saint) on Sep 26, 2024 at 00:50 UTC
    Well yes, undef &sub is a way to destroy a sub. Again I doubt the wisdom of doing so.

    I'll try to come up with some example code tomorrow...°

    Though others might be faster ;)

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

    °) My guess is that most of the confusion is caused by the deletes in the stash

      Actually, in my example I forgot to delete $INC{"My.pm"}; so the second require wasn't redefining My::Run. When I add the delete it works as expected, printing:
      Loading... a: Running Loading... b: Running
      In the OPs example there is also a delete $My::{Run}. So
      require My; print "a: "; My::Run(); # a: Running undef &My::Run; delete $INC{"My.pm"}; delete $My::{Run}; require My; print "b: "; My::Run(); # Undefined subroutine &My::Run called at ...
      says My::Run() is undefined in the second call. What does delete $My::{Run} do?
        > What does delete $My::{Run} do?

        It deletes the entry for the symbol (typeglob) in the namespaces/symbol table/ STASH %My::

        I can't test now, but my guess is that the compiled code is "linked" to the old typeglob's CODE slot. (IIRC that's what B::Concise shows ¹)

        Redefining the sub in a require or eval or whatever will repopulate the slot.

        But after deleting the entry in the symbol table a new typeglob is created and autovivified into the stash. °

        I could create rather complicated tests to prove all of this with all interpretations of what the OP probably wants to achieve.

        Unfortunately I'm too busy for guesswork, and honorable ikegami or Dave Mitchell should know the implementation better.

        You might be interested into following introspection tools

        Looking up following documentation might help
        • symbol table hash aka STASH
        • typeglobs
        • import/export mechanisms
        • require and eval
        • execution phases perlmod
        • garbage collection and refcounts
        • perlguts
        • the Panther book from O'Reilly

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

        Updates

        °) Also diving into implementation details is kind of non productive if undef and sub already do the job. And typeglobs are particularly icky Perl4 stuff which are badly explained and supported by introspection.

        ¹) see Re^2: Refresh a Module

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

        > 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