I am looking for a way to unload (and after that, reload) a module at run time. I have found this code: https://metacpan.org/source/ALEXMV/Module-Refresh-0.17/lib/Module/Refresh.pm and am using bits of it:
# class.pm package class; use 5.014; use strict; use warnings; use autodie; sub new {bless {}, 'class'} sub talk {print "111513\n"} # code.pl #!/usr/bin/env perl use strict; use warnings; use autodie; use 5.014; use Data::Dumper; BEGIN { $^P |= 0x10; eval 'sub DB::sub' if $] < 5.008007; } sub _unload_subs { my $file = shift || return; for my $sym ( grep { index( $_, "$file:" ) == 0 } keys %DB::sub ) +{ eval { undef &$sym }; warn "$sym: $@\n" if $@; delete $DB::sub{$sym}; } return 1; } do './class.pm'; my $obj = class->new(); $obj->talk; delete $INC{'./class.pm'}; _unload_subs('class'); #say Dumper \%DB::sub; my $dummy = <>; # we change class.pm meanwhile do './class.pm'; say $@ if $@; $obj = class->new(); $obj->talk; # results: # $ ./code.pl # 111513 # # panic: attempt to copy freed scalar 12dcbf0 to 1414870 at ./class.pm + line 3, <> line 1. # # Undefined subroutine &class::new called at ./code.pl line 38, <> lin +e 1. # Removing "use 5.014" from class.pm fixes the panic but is there a be +tter fix? # Also, why is this panic error happening?

In reply to Reloading a module at run time by alpha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.