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

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?

Replies are listed 'Best First'.
Re^5: Refresh a Module
by LanX (Saint) on Sep 26, 2024 at 13:54 UTC
    > 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