# file KLEIDER/web/src/localization/Replace.pm # Einfache Platzhalter in Zeichenketten ersetzen # 2015-10-16 Herbert Schiemann # GPL Version 2 oder neuer package Herbaer::Replace ; BEGIN { use Exporter; our $VERSION = 20151016; our @ISA = qw (Exporter); our @EXPORT = qw (replace); } # "intern" von replace verwendet sub _r { my ($key, $v) = @_; my $d = exists $v -> {$key} ? $v -> {$key} : $v -> {"*"}; while ( defined ($d) && ref $d eq "CODE" ) { $d = $d -> ($key); } defined $d && ! ref $d ? $d : "\$\{$key\}"; } # füllt Platzhalter sub replace { my ($str, $vals) = @_; return $str unless ref $vals eq "HASH" || ref $vals eq "ARRAY" ; if (ref $str) { if (ref $str eq "HASH") { my ($k, $v); while ( ($k, $v) = each %$str ) { $str -> {$k} = replace ($v, $vals) if $v; } } elsif (ref $str eq "ARRAY") { my $v; foreach $v (@$str) { $v = replace ($v, $vals) if $v; } } elsif (ref $str eq "SCALAR") { $$str = replace ($$str, $vals); } $str; } elsif (ref $vals eq "ARRAY") { [ map { replace ($str, $_) } @$vals ]; } elsif (ref $vals eq "HASH") { $str =~ s/\$\{([a-zA-Z0-9_.-]+)\}/_r($1, $vals)/ge if %$vals; $str; } else { $str; } } 1; # end of file KLEIDER/web/src/localization/Replace.pm