Newcomen

Description A static content generator.
Newcomen > Perl Modules > Newcomen::Util::String
Source

Index


NAME ^

Newcomen::Util::String - String related utility functions.

SYNOPSIS ^

use Newcomen::Util::String;

# Replace <stuff> in the string:
my $result = Newcomen::Util::String::replace ($string, $hashref);

# Split strings on comma or slash:
my @parts_csv = split_csv ($string);
my @parts_ssv = split_ssv ($string);

DESCRIPTION ^

This package provides a few utility functions dealing with strings. Note that all functions may be imported into the package using Newcomen::Util::String:

use Newcomen::Util::String qw( replace split_csv split_ssv );

No function is exported by default.

FUNCTIONS ^

replace

Toggle source:   replace

sub replace {
   my $string = shift;
   my $values = shift;
   my $spvals = Data::SimplePath -> new ($values);
   while (my @vars = $string =~ /<([^>]+)>/g) {
      for my $var (@vars) {
         my $replacement = replace ($spvals -> get ($var) // '', $values);
         $string =~ s/<\Q$var\E>/$replacement/g;
      }
   }
   return $string;
}
my $result = replace ($string, $hashref);

This function replaces all occurrences of <key> in the string (first parameter) by their values specified in the hashref (second parameter) and returns the resulting string.

For example, the (sub)string '<foo>' will be replaced by $hashref -> {'foo'}. The function will process the string until no more m/<.+>/ are found. If a key does not exist in the hashref, '<key>' will be replaced by an empty string. If a replacement value itself contains variable placeholders, these will be replaced recursively, too. There are no checks to prevent infinite recursion, so be careful in that case. The key must not contain a '>' character.

Note that the hashref may be a complex data structure, e.g. a hash of hashes. Data::SimplePath is used to access the values. For example, the string '<foo/bar>' would be replaced by the value of $hashref -> {'foo'} {'bar'} (as returned by Data::SimplePath's get() method).

split_csv

Toggle source:   split_csv

sub split_csv { my @parts = split /\s*,\s*/, shift; return grep { $_ ne '' } @parts; }
my @parts = split_csv ($string);

Splits the specified string at commas and returns the resulting list, excluding any empty strings. Spaces around the commas will be stripped. E.g. splitting the string 'x, y, , z' would return the list ('x', 'y', 'z').

split_ssv

Toggle source:   split_ssv

sub split_ssv { my @parts = split /\/+/,     shift; return grep { $_ ne '' } @parts; }
my @parts = split_ssv ($string);

Splits the specified string at slashes and returns the resulting list, excluding any empty strings, e.g. splitting the string '/x/y//z/' would return the list ('x', 'y', 'z').

SEE ALSO ^

Data::SimplePath

VERSION ^

This is version 2014052501.

AUTHOR ^

Stefan Goebel - newcomen {at} subtype {dot} de

COPYRIGHT AND LICENSE ^

Copyright 2013-2014 Stefan Goebel.

This file is part of Newcomen.

Newcomen is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version.

Newcomen is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Newcomen. If not, see <http://www.gnu.org/licenses/>.