aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Utils.pm
diff options
context:
space:
mode:
authorFlorent Villard <warly@mandriva.com>2006-08-04 16:45:06 +0000
committerFlorent Villard <warly@mandriva.com>2006-08-04 16:45:06 +0000
commit0a7ef4aa1b338a6c23dccd0db15086596f05a22a (patch)
tree620105c88261aa086535f04d1ca5fba94cb4cbbf /lib/Youri/Utils.pm
parent1fec4f0cac5732229070c4ad2e24c01ba2bab51b (diff)
downloadmga-youri-core-0a7ef4aa1b338a6c23dccd0db15086596f05a22a.tar
mga-youri-core-0a7ef4aa1b338a6c23dccd0db15086596f05a22a.tar.gz
mga-youri-core-0a7ef4aa1b338a6c23dccd0db15086596f05a22a.tar.bz2
mga-youri-core-0a7ef4aa1b338a6c23dccd0db15086596f05a22a.tar.xz
mga-youri-core-0a7ef4aa1b338a6c23dccd0db15086596f05a22a.zip
imported initial version of youri svn
Diffstat (limited to 'lib/Youri/Utils.pm')
-rw-r--r--lib/Youri/Utils.pm90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/Youri/Utils.pm b/lib/Youri/Utils.pm
new file mode 100644
index 0000000..76359ed
--- /dev/null
+++ b/lib/Youri/Utils.pm
@@ -0,0 +1,90 @@
+# $Id: Utils.pm 697 2006-02-26 16:44:54Z guillomovitch $
+package Youri::Utils;
+
+=head1 NAME
+
+Youri::Utils - Youri shared functions
+
+=head1 DESCRIPTION
+
+This module implement some helper functions for all youri applications.
+
+=cut
+
+use base qw(Exporter);
+use Carp;
+use strict;
+use warnings;
+
+our @EXPORT = qw(
+ create_instance
+ load
+ add2hash
+ add2hash_
+);
+
+=head2 create_instance(class => I<$class>, I<%options>)
+
+Create an instance of a class at runtime.
+I<$class> is the class name.
+I<%options> are passed to the class constructor.
+Returns the class instance.
+
+=cut
+
+sub create_instance {
+ my ($expected_class, %options) = @_;
+
+ die 'No expected class given' unless $expected_class;
+ die "No class given, expected derivated class from '$expected_class'" unless $options{class};
+
+ # extract class from options
+ my $class = $options{class};
+ delete $options{class};
+
+ # ensure loaded
+ load($class);
+
+ # check interface
+ die "$class is not a $expected_class" unless $class->isa($expected_class);
+
+ # instantiate
+ no strict 'refs';
+ return $class->new(%options);
+}
+
+sub load {
+ my ($class) = @_;
+
+ $class .= '.pm';
+ $class =~ s/::/\//g;
+ require $class;
+}
+
+# structure helpers
+
+sub add2hash {
+ my ($a, $b) = @_;
+ while (my ($k, $v) = each %{$b || {}}) {
+ $a->{$k} ||= $v;
+ }
+ return $a;
+}
+
+sub add2hash_ {
+ my ($a, $b) = @_;
+ while (my ($k, $v) = each %{$b || {}}) {
+ exists $a->{$k} or $a->{$k} = $v;
+ }
+ return $a;
+}
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2002-2006, YOURI project
+
+This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
+
+1;