New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
ClassLoader.pm in vendors/lib/FCM1/Util – NEMO

source: vendors/lib/FCM1/Util/ClassLoader.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 2.2 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
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# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22package FCM1::Util::ClassLoader;
23use base qw{Exporter};
24
25our @EXPORT_OK = qw{load};
26
27use Carp qw{croak};
28use FCM1::Exception;
29
30sub 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
461;
47__END__
48
49=head1 NAME
50
51FCM1::ClassLoader
52
53=head1 SYNOPSIS
54
55    use FCM1::Util::ClassLoader;
56    $load_ok = FCM1::Util::ClassLoader::load($class);
57
58=head1 DESCRIPTION
59
60A wrapper for loading a class dynamically.
61
62=head1 FUNCTIONS
63
64=over 4
65
66=item load($class,$test_method)
67
68If $class can call $test_method, returns $class. Otherwise, attempts to
69require() $class and returns it. If this fails, croak() with a
70L<FCM1::Exception|FCM1::Exception>.
71
72=item load($class)
73
74Shorthand 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
84The load($class,$test_method) function croak() with this exception if it fails
85to load the specified class.
86
87=back
88
89=head1 COPYRIGHT
90
91E<169> Crown copyright Met Office. All rights reserved.
92
93=cut
Note: See TracBrowser for help on using the repository browser.