diff Lib/IMPL/Resources/Format.pm @ 18:818c74b038ae

DOM Schema + tests
author Sergey
date Thu, 10 Sep 2009 17:42:47 +0400
parents
children 16ada169ca75
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/Resources/Format.pm	Thu Sep 10 17:42:47 2009 +0400
@@ -0,0 +1,32 @@
+package IMPL::Resources::Format;
+use strict;
+use warnings;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(&FormatMessage);
+
+sub FormatMessage {
+    my ($string,$args) = @_;
+    
+    $string =~ s/%(\w+(?:\.\w+)*)%/_getvalue($args,$1,"\[$1\]")/ge;
+    
+    return $string;
+}
+
+sub _getvalue {
+    my ($obj,$path,$default) = @_;
+    
+    foreach my $chunk (split /\./,$path) {
+        if (eval { $obj->can( $chunk ) } ) {
+            $obj = $obj->$chunk();
+        } elsif (UNIVERSAL::isa($obj,'HASH')) {
+            $obj = $obj->{$chunk};
+        } else {
+            return $default;
+        }
+    }
+    return $obj;
+}
+
+1;