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 branches/UKMO/r5936_restart_datestamp/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Util – NEMO

source: branches/UKMO/r5936_restart_datestamp/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Util/ClassLoader.pm @ 7114

Last change on this file since 7114 was 7114, checked in by jcastill, 7 years ago

Changes as in UKMO/restart_datestamp@6336

File size: 1.6 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) Crown copyright Met Office. All rights reserved.
3# For further details please refer to the file COPYRIGHT.txt
4# which you should have received as part of this distribution.
5# ------------------------------------------------------------------------------
6use strict;
7use warnings;
8
9package Fcm::Util::ClassLoader;
10use base qw{Exporter};
11
12our @EXPORT_OK = qw{load};
13
14use Carp qw{croak};
15use Fcm::Exception;
16
17sub load {
18    my ($class, $test_method) = @_;
19    if (!$test_method) {
20        $test_method = 'new';
21    }
22    if (!UNIVERSAL::can($class, $test_method)) {
23        eval('require ' . $class);
24        if ($@) {
25            croak(Fcm::Exception->new({message => sprintf(
26                "%s: class loading failed: %s", $class, $@,
27            )}));
28        }
29    }
30    return $class;
31}
32
331;
34__END__
35
36=head1 NAME
37
38Fcm::ClassLoader
39
40=head1 SYNOPSIS
41
42    use Fcm::Util::ClassLoader;
43    $load_ok = Fcm::Util::ClassLoader::load($class);
44
45=head1 DESCRIPTION
46
47A wrapper for loading a class dynamically.
48
49=head1 FUNCTIONS
50
51=over 4
52
53=item load($class,$test_method)
54
55If $class can call $test_method, returns $class. Otherwise, attempts to
56require() $class and returns it. If this fails, croak() with a
57L<Fcm::Exception|Fcm::Exception>.
58
59=item load($class)
60
61Shorthand for C<load($class, 'new')>.
62
63=back
64
65=head1 DIAGNOSTICS
66
67=over 4
68
69=item L<Fcm::Exception|Fcm::Exception>
70
71The load($class,$test_method) function croak() with this exception if it fails
72to load the specified class.
73
74=back
75
76=head1 COPYRIGHT
77
78E<169> Crown copyright Met Office. All rights reserved.
79
80=cut
Note: See TracBrowser for help on using the repository browser.