public
Description: My personal script stash
Homepage: http://github.com/melo/scripts
Clone URL: git://github.com/melo/scripts.git
Search Repo:
Added require_module and optional_module to the x-datetime-converter 
script

This allows a better experience if the user needs a specific perl module.

Signed-off-by: Pedro Melo <melo@simplicidade.org>
melo (author)
Sat May 03 01:50:56 -0700 2008
commit  d7e1261d9b05a79c8b221dafd1aa6dff200999bd
tree    cad1647a2815ff333956ce4dfa84b2aab11096c0
parent  f9e111a6b6d511d6123c223237ebc5f833612a5e
...
1
2
3
4
 
5
 
 
6
7
8
9
...
11
12
13
 
 
14
15
16
17
18
19
20
...
90
91
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
...
1
2
3
 
4
5
6
7
8
9
10
11
...
13
14
15
16
17
18
 
 
 
19
20
21
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
0
@@ -1,8 +1,10 @@
0
 #!/usr/bin/perl -w
0
 
0
 use strict;
0
-use DateTime;
0
+use warnings;
0
 
0
+require_module('DateTime');
0
+
0
 # Format classes to use for display
0
 my @formats = qw( DateParse MySQL HTTP Mail RSS );
0
 
0
0
@@ -11,10 +13,9 @@
0
 FORMAT_CLASS:
0
 foreach my $format (@formats) {
0
   my $class = "DateTime::Format::$format";
0
+
0
+ next FORMAT_CLASS unless optional_module($class);
0
   
0
- eval "require $class;";
0
- next FORMAT_CLASS if $@;
0
-
0
   push @available, {
0
     name => $format,
0
     class => $class,
0
@@ -90,5 +91,32 @@
0
   else {
0
     print " $t -- could not parse this\n"
0
   }
0
+}
0
+
0
+
0
+#####################################
0
+# Deal with required/optional modules
0
+
0
+sub require_module {
0
+ my $module = shift;
0
+
0
+ eval "require $module";
0
+ if (my $e = $@) {
0
+ print STDERR "FATAL: $0 requires the Perl module '$module'.\n\n";
0
+ print STDERR "You can install it with:\n\n";
0
+ print STDERR " cpan $module\n\n";
0
+ exit(1);
0
+ }
0
+ $module->import(@_);
0
+}
0
+
0
+sub optional_module {
0
+ my $module = shift;
0
+
0
+ eval "require $module";
0
+ return 0 if $@;
0
+
0
+ $module->import(@_);
0
+ return 1;
0
 }

Comments

    No one has commented yet.