Class::Refresh(3pm) | User Contributed Perl Documentation | Class::Refresh(3pm) |
Class::Refresh - refresh your classes during runtime
version 0.07
use Class::Refresh; use Foo; Class::Refresh->refresh; # edit Foo.pm Class::Refresh->refresh; # changes in Foo.pm are applied
During development, it is fairly common to cycle between writing code and testing that code. Generally the testing happens within the test suite, but frequently it is more convenient to test things by hand when tracking down a bug, or when doing some exploratory coding. In many situations, however, this becomes inconvenient - for instance, in a REPL, or in a stateful web application, restarting from the beginning after every code change can get pretty tedious. This module allows you to reload your application classes on the fly, so that the code/test cycle becomes a lot easier.
This module takes a hash of import arguments, which can include:
use Class::Refresh track_require => 1;
If set, a "require()" hook will be installed to track modules which are loaded. This will make the list of modules to reload when "refresh" is called more accurate, but may cause issues with other modules which hook into "require" (since the hook is global).
This module has several limitations, due to reloading modules in this way being an inherently fragile operation. Therefore, this module is recommended for use only in development environments - it should not be used for reloading things in production.
It makes several assumptions about how code is structured that simplify the logic involved quite a bit, and make it more reliable when those assumptions hold, but do make it inappropriate for use in certain cases. For instance, this module is named "Class::Refresh" for a reason: it is only intended for refreshing classes, where each file contains a single namespace, and each namespace corresponds to a single file, and all function calls happen through method dispatch. Unlike Module::Refresh, which makes an effort to track the files where subs were defined, this module assumes that refreshing a class means wiping out everything in the class's namespace, and reloading the file corresponding to that class. If your code includes multiple files that all load things into a common namespace, or defines multiple classes in a single file, this will likely not work.
The main entry point to the module. The first call to "refresh" populates a cache of modification times for currently loaded modules, and subsequent calls will refresh any classes which have changed since the previous call.
Returns a list of modules which have changed since the last call to "refresh".
This method calls "unload_module" and "load_module" on $mod, as well as on any classes that depend on $mod (for instance, subclasses if $mod is a class, or classes that consume $mod if $mod is a role). This ensures that all of your classes are consistent, even when dealing with things like immutable Moose classes.
Unloads $mod, using Class::Unload.
Loads $mod, using Class::Load.
Also, files which define multiple classes cause problems since we can't always determine which classes are defined in the file, and so reloading the file may cause class definitions to be run more than once.
Please report any bugs to GitHub Issues at <https://github.com/doy/class-refresh/issues>.
Module::Refresh
You can find this documentation for this module with the perldoc command.
perldoc Class::Refresh
You can also look for information at:
<https://metacpan.org/release/Class-Refresh>
<https://github.com/doy/class-refresh>
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Refresh>
<http://cpanratings.perl.org/d/Class-Refresh>
This module was based in large part on Module::Refresh by Jesse Vincent.
Jesse Luehrs <doy@tozt.net>
This software is copyright (c) 2014 by Jesse Luehrs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2018-03-31 | perl v5.26.1 |