1 | # ------------------------------------------------------------------------------ |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # ------------------------------------------------------------------------------ |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | |
---|
22 | package FCM1::Util::ClassLoader; |
---|
23 | use base qw{Exporter}; |
---|
24 | |
---|
25 | our @EXPORT_OK = qw{load}; |
---|
26 | |
---|
27 | use Carp qw{croak}; |
---|
28 | use FCM1::Exception; |
---|
29 | |
---|
30 | sub load { |
---|
31 | my ($class, $test_method) = @_; |
---|
32 | if (!$test_method) { |
---|
33 | $test_method = 'new'; |
---|
34 | } |
---|
35 | if (!UNIVERSAL::can($class, $test_method)) { |
---|
36 | eval('require ' . $class); |
---|
37 | if ($@) { |
---|
38 | croak(FCM1::Exception->new({message => sprintf( |
---|
39 | "%s: class loading failed: %s", $class, $@, |
---|
40 | )})); |
---|
41 | } |
---|
42 | } |
---|
43 | return $class; |
---|
44 | } |
---|
45 | |
---|
46 | 1; |
---|
47 | __END__ |
---|
48 | |
---|
49 | =head1 NAME |
---|
50 | |
---|
51 | FCM1::ClassLoader |
---|
52 | |
---|
53 | =head1 SYNOPSIS |
---|
54 | |
---|
55 | use FCM1::Util::ClassLoader; |
---|
56 | $load_ok = FCM1::Util::ClassLoader::load($class); |
---|
57 | |
---|
58 | =head1 DESCRIPTION |
---|
59 | |
---|
60 | A wrapper for loading a class dynamically. |
---|
61 | |
---|
62 | =head1 FUNCTIONS |
---|
63 | |
---|
64 | =over 4 |
---|
65 | |
---|
66 | =item load($class,$test_method) |
---|
67 | |
---|
68 | If $class can call $test_method, returns $class. Otherwise, attempts to |
---|
69 | require() $class and returns it. If this fails, croak() with a |
---|
70 | L<FCM1::Exception|FCM1::Exception>. |
---|
71 | |
---|
72 | =item load($class) |
---|
73 | |
---|
74 | Shorthand for C<load($class, 'new')>. |
---|
75 | |
---|
76 | =back |
---|
77 | |
---|
78 | =head1 DIAGNOSTICS |
---|
79 | |
---|
80 | =over 4 |
---|
81 | |
---|
82 | =item L<FCM1::Exception|FCM1::Exception> |
---|
83 | |
---|
84 | The load($class,$test_method) function croak() with this exception if it fails |
---|
85 | to load the specified class. |
---|
86 | |
---|
87 | =back |
---|
88 | |
---|
89 | =head1 COPYRIGHT |
---|
90 | |
---|
91 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
92 | |
---|
93 | =cut |
---|