Mercurial > pub > Impl
comparison Lib/IMPL/Resources/Format.pm @ 18:818c74b038ae
DOM Schema + tests
author | Sergey |
---|---|
date | Thu, 10 Sep 2009 17:42:47 +0400 |
parents | |
children | 16ada169ca75 |
comparison
equal
deleted
inserted
replaced
17:7f88e01b58f8 | 18:818c74b038ae |
---|---|
1 package IMPL::Resources::Format; | |
2 use strict; | |
3 use warnings; | |
4 | |
5 require Exporter; | |
6 our @ISA = qw(Exporter); | |
7 our @EXPORT_OK = qw(&FormatMessage); | |
8 | |
9 sub FormatMessage { | |
10 my ($string,$args) = @_; | |
11 | |
12 $string =~ s/%(\w+(?:\.\w+)*)%/_getvalue($args,$1,"\[$1\]")/ge; | |
13 | |
14 return $string; | |
15 } | |
16 | |
17 sub _getvalue { | |
18 my ($obj,$path,$default) = @_; | |
19 | |
20 foreach my $chunk (split /\./,$path) { | |
21 if (eval { $obj->can( $chunk ) } ) { | |
22 $obj = $obj->$chunk(); | |
23 } elsif (UNIVERSAL::isa($obj,'HASH')) { | |
24 $obj = $obj->{$chunk}; | |
25 } else { | |
26 return $default; | |
27 } | |
28 } | |
29 return $obj; | |
30 } | |
31 | |
32 1; |