Author: spadkins
Date: Wed Mar 29 11:18:06 2006
New Revision: 3724

Modified:
   p5ee/trunk/App-Context/lib/App/Service.pm

Log:
enhance substitute() to accept {var:default} syntax

Modified: p5ee/trunk/App-Context/lib/App/Service.pm
==============================================================================
--- p5ee/trunk/App-Context/lib/App/Service.pm   (original)
+++ p5ee/trunk/App-Context/lib/App/Service.pm   Wed Mar 29 11:18:06 2006
@@ -291,7 +291,7 @@
 sub substitute {
     &App::sub_entry if ($App::trace);
     my ($self, $text, $values) = @_;
-    my ($phrase, $var, $value, $context);
+    my ($phrase, $var, $value, $context, $default);
     $context = $self->{context};
     $values = {} if (! defined $values);
 
@@ -306,8 +306,9 @@
         return($newhash); # short-circuit this whole process
     }
 
-    while ( $text =~ /\{([^\{\}]+)\}/ ) {  # vars of the form {var}
+    while ( $text =~ /\{([^\{\}:]+)(:[^\{\}]+)?\}/ ) {  # vars of the form 
{var}
         $var = $1;
+        $default = $2;
         if (defined $values->{$var}) {
             $value = $values->{$var};
             $value = join(",", @$value) if (ref($value) eq "ARRAY");
@@ -317,8 +318,14 @@
             $value = $context->so_get($var);
             $value = join(",", @$value) if (ref($value) eq "ARRAY");
         }
-        $value = "" if (!defined $value);
-        $text =~ s/\{$var\}/$value/g;
+        if ((! defined $value || $value eq "") && $default ne "") {
+            $default =~ s/^://;
+            $value = $default;
+        }
+        elsif (!defined $value) {
+            $value = "";
+        }
+        $text =~ s/\{$var(:[^\{\}]+)?\}/$value/g;
     }
     &App::sub_exit($text) if ($App::trace);
     $text;

Reply via email to