source:
branches/UKMO/antarctic_partial_slip/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Util/ClassLoader.pm
@
5958
Last change on this file since 5958 was 5958, checked in by davestorkey, 8 years ago | |
---|---|
File size: 1.6 KB |
Rev | Line | |
---|---|---|
[1980] | 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 | # ------------------------------------------------------------------------------ | |
6 | use strict; | |
7 | use warnings; | |
8 | ||
9 | package Fcm::Util::ClassLoader; | |
10 | use base qw{Exporter}; | |
11 | ||
12 | our @EXPORT_OK = qw{load}; | |
13 | ||
14 | use Carp qw{croak}; | |
15 | use Fcm::Exception; | |
16 | ||
17 | sub 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 | ||
33 | 1; | |
34 | __END__ | |
35 | ||
36 | =head1 NAME | |
37 | ||
38 | Fcm::ClassLoader | |
39 | ||
40 | =head1 SYNOPSIS | |
41 | ||
42 | use Fcm::Util::ClassLoader; | |
43 | $load_ok = Fcm::Util::ClassLoader::load($class); | |
44 | ||
45 | =head1 DESCRIPTION | |
46 | ||
47 | A wrapper for loading a class dynamically. | |
48 | ||
49 | =head1 FUNCTIONS | |
50 | ||
51 | =over 4 | |
52 | ||
53 | =item load($class,$test_method) | |
54 | ||
55 | If $class can call $test_method, returns $class. Otherwise, attempts to | |
56 | require() $class and returns it. If this fails, croak() with a | |
57 | L<Fcm::Exception|Fcm::Exception>. | |
58 | ||
59 | =item load($class) | |
60 | ||
61 | Shorthand 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 | ||
71 | The load($class,$test_method) function croak() with this exception if it fails | |
72 | to load the specified class. | |
73 | ||
74 | =back | |
75 | ||
76 | =head1 COPYRIGHT | |
77 | ||
78 | E<169> Crown copyright Met Office. All rights reserved. | |
79 | ||
80 | =cut |
Note: See TracBrowser
for help on using the repository browser.