Colin Newell
and 1 contributors


OpusVL::Text::Util - Simple text utilities


version 0.10


This provides a couple of simple methods for playing with text.

    use OpusVL::Text::Util qw/truncate_text wrap_text/;

    my $truncated = truncate_text('a long string really', 10);
    # 'a long...'
    my $wrapped = truncate_text('a long string really', 10);
    $wrapped = "a long\nstring really";



This truncates a string close to the limit provided. It tries to break it on a word break if possible. It then appends a '...' to the string. This isn't included in the calculation of the length, so you may end up with 3 more characters than you specified.

    my $truncated = truncate_text('a long string really', 10);
    # 'a long...'


This method has a go at wrapping a line of text. Note that it isn't designed to work on multiple lines of text. It will attempt to split at convenient points within the required width and if that fails it will simply display what is there. All the text should be displayed with this method.

You can also specify the linefeed characters as the last parameter.

    my $wrapped = wrap_text('a long string really', 10);
    # "a long\nstring\nreally"
    my $wrapped = wrap_text('a long string really', 10, "\r\n");
    # "a long\r\nstring\r\nreally"


Makes a string safe to use as an HTML id. Converts all non safe characters to _.

    string_to_id('thuds-!this') # 'thuds__this'


Splits a string on line breaks. Accounts for all 3 types of line break, DOS, MAC and Unix.

    line_split("a\nb\r\nc") # qw/a b c/


Returns the list of items missing.

    $mandatory = [qw/a b c/];
    $cols = [qw/a b d e f/];
    missing_array_items($mandatory, $cols); # ['c']


Returns true if the string provided is not blank.

    not_blank('0') # 0
    not_blank('')  # 1


Splits a list of words in a string. Looks for commas to split the list and strips whitespace.

    split_words('veh1,veh2,veh3') #  qw/veh1 veh2 veh3/


Mask text field contents using a simple regex.

    mask_text('*', '(\d{4}).*(\d{3})', '456456564654654');
    # '4564********654'

Specify a fill character, a regex (as a string), and the text to mask out.

This does not guard against rogue regexes. Capture the parts you expect to be retained.


Split a camel case word into an arrayref. This assumes you're giving it a word, behaviour when passed something like a sentence is undefined.

    # ['Template', 'Not', 'Matched', 'Exception']


Colin Newell <>


This software is copyright (c) 2012 - 2016 by OpusVL.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.