Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Adding code to a Perl program without stopping it

by GrandFather (Saint)
on Mar 26, 2011 at 08:31 UTC ( [id://895645]=note: print w/replies, xml ) Need Help??


in reply to Adding code to a Perl program without stopping it

Yes. Specifically it is possible to reload a module. The trick is to update %INC so that a subsequent call to require reloads the module. Consider:

# reload.pl use strict; use warnings; for (1 .. 2) { delete $INC{'ReloadModule.pm'}; require ReloadModule; for (1 .. 3) { print ReloadModule::nextCount(), " "; } print "\n"; } # ReloadModule.pm use strict; use warnings; no warnings 'redefine'; package ReloadModule; my $count = 0; sub nextCount { return ++$count; } 1;

Prints:

1 2 3 1 2 3

Note the use of no warnings 'redefine'; in the module to be reloaded to suppress "Subroutine nextCount redefined at ..." warnings.

True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://895645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found