File Coverage

blib/lib/Class/Unload.pm
Criterion Covered Total %
statement 48 50 96.0
branch 8 12 66.6
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 67 73 91.7


line stmt bran cond sub pod time code
1             package Class::Unload;
2             # ABSTRACT: Unload a class
3             $Class::Unload::VERSION = '0.12';
4 2     2   269573 use warnings;
  2     1   6  
  2         160  
  1         144371  
  1         3  
  1         63  
5 2     2   14 use strict;
  2     1   5  
  2         84  
  1         6  
  1         2  
  1         48  
6 2     2   13 no strict 'refs'; # we're fiddling with the symbol table
  2     1   4  
  2         67  
  1         7  
  1         2  
  1         38  
7              
8 2     2   1376 use Class::Inspector;
  2     1   10674  
  2         638  
  1         17  
  1         2  
  1         352  
9              
10              
11             sub unload {
12 2     2 1 243100 my ($self, $class) = @_;
  5     5   255054  
13              
14 2 50       13 return unless Class::Inspector->loaded( $class );
  5 100       24  
15              
16             # Flush inheritance caches
17 2         127 @{$class . '::ISA'} = ();
  2         41  
  4         144  
  4         76  
18              
19 2         9 my $symtab = $class.'::';
  4         11  
20             # Delete all symbols except other namespaces
21 2         10 for my $symbol (keys %$symtab) {
  4         19  
22 9 50       28 next if $symbol =~ /\A[^:]+::\z/;
  14 100       48  
23 9         60 delete $symtab->{$symbol};
  12         66  
24             }
25              
26 2         25 my $inc_file = join( '/', split /(?:'|::)/, $class ) . '.pm';
  4         39  
27 2         9 delete $INC{ $inc_file };
  4         12  
28              
29 2 50       10 if (Class::Inspector->loaded('Class::MOP')) {
  4 50       15  
30 0         0 Class::MOP::remove_metaclass_by_name($class);
  0         0  
31             }
32              
33 2         167 return 1;
  4         406  
34             }
35              
36              
37             1; # End of Class::Unload
38              
39             __END__
40              
41             =pod
42              
43             =encoding UTF-8
44              
45             =head1 NAME
46              
47             Class::Unload - Unload a class
48              
49             =head1 VERSION
50              
51             version 0.12
52              
53             =head1 SYNOPSIS
54              
55             use Class::Unload;
56             use Class::Inspector;
57              
58             use Some::Class;
59              
60             Class::Unload->unload( 'Some::Class' );
61             Class::Inspector->loaded( 'Some::Class' ); # Returns false
62              
63             require Some::Class; # Reloads the class
64              
65             =head1 METHODS
66              
67             =head2 unload $class
68              
69             Unloads the given class by clearing out its symbol table and removing it
70             from %INC. If it's a L<Moose> class, the metaclass is also removed.
71              
72             =head1 SEE ALSO
73              
74             L<Class::Inspector>
75              
76             =head1 ACKNOWLEDGEMENTS
77              
78             Thanks to Matt S. Trout, James Mastros and Uri Guttman for various tips
79             and pointers.
80              
81             =head1 AUTHOR
82              
83             Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>;
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is copyright (c) 2025 by Dagfinn Ilmari Mannsåker.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.
91              
92             =cut