cvs commit: p5ee/App-Context/lib/App Service.pm

[email protected] (Stephen Adkins)
Newsgroups perl.cvs.p5ee
Message-ID <[email protected]>
cvsuser     05/11/07 19:45:12

  Modified:    App-Context/lib/App Service.pm
  Log:
  add substitute()
  
  Revision  Changes    Path
  1.7       +69 -1     p5ee/App-Context/lib/App/Service.pm
  
  Index: Service.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Context/lib/App/Service.pm,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Service.pm	2 Sep 2004 20:55:45 -0000	1.6
  +++ Service.pm	8 Nov 2005 03:45:12 -0000	1.7
  @@ -256,6 +256,74 @@
   }
   
   #############################################################################
  +# substitute()
  +#############################################################################
  +
  +=head2 substitute()
  +
  +    * Signature: $result = $service->substitute($target);
  +    * Signature: $result = $service->substitute($target, $values);
  +    * Param:     $target         HASH,string
  +    * Param:     $values         HASH
  +    * Return:    $result         string
  +    * Throws:    App::Exception
  +    * Since:     0.01
  +
  +    Sample Usage: 
  +
  +    $welcome_message = $service->substitute("Welcome, {default-user}");
  +
  +    my $auto_params = { user => "{default-user}", org_id => "{org_id}", };
  +    my $auto_values = { org_id => 1, };
  +    $params = $service->substitute($auto_params, $auto_values);
  +
  +The substitute() method scans the $target string (or hash of strings) for
  +instances of variables (i.e. "{varname}") and makes substitutions.
  +It makes substitutions from a hash of $values if provided or from the
  +values of SessionObjects of the same name.
  +
  +The substitute() method returns a string (or hash of strings) which is the
  +result of the substitution.
  +
  +=cut
  +
  +sub substitute {
  +    &App::sub_entry if ($App::trace);
  +    my ($self, $text, $values) = @_;
  +    my ($phrase, $var, $value, $context);
  +    $context = $self->{context};
  +    $values = {} if (! defined $values);
  +
  +    if (ref($text) eq "HASH") {
  +        my ($hash, $newhash);
  +        $hash = $text;    # oops, not text, but a hash of text values
  +        $newhash = {};    # prepare a new hash for the substituted values
  +        foreach $var (keys %$hash) {
  +            $newhash->{$var} = $self->substitute($hash->{$var}, $values);
  +        }
  +        &App::sub_exit($newhash) if ($App::trace);
  +        return($newhash); # short-circuit this whole process
  +    }
  +
  +    while ( $text =~ /\{([^\{\}]+)\}/ ) {  # vars of the form {var}
  +        $var = $1;
  +        if (defined $values->{$var}) {
  +            $value = $values->{$var};
  +            $value = join(",", @$value) if (ref($value) eq "ARRAY");
  +            $text =~ s/\{$var\}/$value/g;
  +        }
  +        else {
  +            $value = $context->so_get($var);
  +            $value = join(",", @$value) if (ref($value) eq "ARRAY");
  +        }
  +        $value = "" if (!defined $value);
  +        $text =~ s/\{$var\}/$value/g;
  +    }
  +    &App::sub_exit($text) if ($App::trace);
  +    $text;
  +}
  +
  +#############################################################################
   # PROTECTED METHODS
   #############################################################################
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.