[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Modules' page = 'Plugin_String' %] [% WRAPPER toc; PROCESS tocitem title ="SYNOPSIS" subs = []; PROCESS tocitem title ="DESCRIPTION" subs = []; PROCESS tocitem title ="METHODS" subs = [ "Construction Methods", "Inspection Methods", "Mutation Methods" ]; PROCESS tocitem title ="AUTHOR" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; PROCESS tocitem title ="SEE ALSO" subs = []; END %] [% WRAPPER section title="SYNOPSIS" -%]

        # create String objects via USE directive
        [% tt_start_tag %] USE String [% tt_end_tag %]
        [% tt_start_tag %] USE String 'initial text' [% tt_end_tag %]
        [% tt_start_tag %] USE String text => 'initial text' [% tt_end_tag %]

        # or from an existing String via new()
        [% tt_start_tag %] newstring = String.new [% tt_end_tag %]
        [% tt_start_tag %] newstring = String.new('newstring text') [% tt_end_tag %]
        [% tt_start_tag %] newstring = String.new( text => 'newstring text' ) [% tt_end_tag %]

        # or from an existing String via copy()
        [% tt_start_tag %] newstring = String.copy [% tt_end_tag %]

        # append text to string
        [% tt_start_tag %] String.append('text to append') [% tt_end_tag %]

        # format left, right or center/centre padded
        [% tt_start_tag %] String.left(20) [% tt_end_tag %]
        [% tt_start_tag %] String.right(20) [% tt_end_tag %]
        [% tt_start_tag %] String.center(20) [% tt_end_tag %]   # American spelling
        [% tt_start_tag %] String.centre(20) [% tt_end_tag %]   # European spelling

        # and various other methods...

[%- END %] [% WRAPPER section title="DESCRIPTION" -%]

This module implements a String class for doing stringy things to text in an object-oriented way.

You can create a String object via the USE directive, adding any initial text value as an argument or as the named parameter 'text'.

        [% tt_start_tag %] USE String [% tt_end_tag %]
        [% tt_start_tag %] USE String 'initial text' [% tt_end_tag %]
        [% tt_start_tag %] USE String text='initial text' [% tt_end_tag %]

The object created will be referenced as 'String' by default, but you can provide a different variable name for the object to be assigned to:

        [% tt_start_tag %] USE greeting = String 'Hello World' [% tt_end_tag %]

Once you've got a String object, you can use it as a prototype to create other String objects with the new() method.

        [% tt_start_tag %] USE String [% tt_end_tag %]
        [% tt_start_tag %] greeting = String.new('Hello World') [% tt_end_tag %]

The new() method also accepts an initial text string as an argument or the named parameter 'text'.

        [% tt_start_tag %] greeting = String.new( text => 'Hello World' ) [% tt_end_tag %]

You can also call copy() to create a new String as a copy of the original.

        [% tt_start_tag %] greet2 = greeting.copy [% tt_end_tag %]

The String object has a text() method to return the content of the string.

        [% tt_start_tag %] greeting.text [% tt_end_tag %]

However, it is sufficient to simply print the string and let the overloaded stringification operator call the text() method automatically for you.

        [% tt_start_tag %] greeting [% tt_end_tag %]

Thus, you can treat String objects pretty much like any regular piece of text, interpolating it into other strings, for example:

        [% tt_start_tag %] msg = "It printed '$greeting' and then dumped core\n" [% tt_end_tag %]

You also have the benefit of numerous other methods for manipulating the string.

        [% tt_start_tag %] msg.append("PS  Don't eat the yellow snow") [% tt_end_tag %]

Note that all methods operate on and mutate the contents of the string itself. If you want to operate on a copy of the string then simply take a copy first:

        [% tt_start_tag %] msg.copy.append("PS  Don't eat the yellow snow") [% tt_end_tag %]

These methods return a reference to the String object itself. This allows you to chain multiple methods together.

        [% tt_start_tag %] msg.copy.append('foo').right(72) [% tt_end_tag %]

It also means that in the above examples, the String is returned which causes the text() method to be called, which results in the new value of the string being printed. To suppress printing of the string, you can use the CALL directive.

        [% tt_start_tag %] foo = String.new('foo') [% tt_end_tag %]

        [% tt_start_tag %] foo.append('bar') [% tt_end_tag %]         # prints "foobar"

        [% tt_start_tag %] CALL foo.append('bar') [% tt_end_tag %]    # nothing

[%- END %] [% WRAPPER section title="METHODS" -%][% WRAPPER subsection title = "Construction Methods" -%]

The following methods are used to create new String objects.

- new()

  Creates a new string using an initial value passed as a positional argument or the named parameter 'text'.

          [% tt_start_tag %] USE String [% tt_end_tag %]
          [% tt_start_tag %] msg = String.new('Hello World') [% tt_end_tag %]
          [% tt_start_tag %] msg = String.new( text => 'Hello World' ) [% tt_end_tag %]

- copy()

  Creates a new String object which contains a copy of the original string.

          [% tt_start_tag %] msg2 = msg.copy [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = "Inspection Methods" -%]

These methods are used to inspect the string content or other parameters relevant to the string.

- text()

  Returns the internal text value of the string. The stringification operator is overloaded to call this method. Thus the following are equivalent:

          [% tt_start_tag %] msg.text [% tt_end_tag %]
          [% tt_start_tag %] msg [% tt_end_tag %]

- length()

  Returns the length of the string.

          [% tt_start_tag %] USE String("foo") [% tt_end_tag %]

          [% tt_start_tag %] String.length [% tt_end_tag %]   # => 3

- search($pattern)

  Searches the string for the regular expression specified in $pattern returning true if found or false otherwise.

          [% tt_start_tag %] item = String.new('foo bar baz wiz waz woz') [% tt_end_tag %]

          [% tt_start_tag %] item.search('wiz') ? 'WIZZY! :-)' : 'not wizzy :-(' [% tt_end_tag %]

- split($pattern, $limit)

  Splits the string based on the delimiter $pattern and optional $limit. Delegates to Perl's internal split() so the parameters are exactly the same.

          [% tt_start_tag %] FOREACH item.split [% tt_end_tag %]
               ...
          [% tt_start_tag %] END [% tt_end_tag %]

          [% tt_start_tag %] FOREACH item.split('baz|waz') [% tt_end_tag %]
               ...
          [% tt_start_tag %] END [% tt_end_tag %]

- substr($offset, $length, $replacement)

  Returns a substring starting at $offset, for $length characters.

          [% tt_start_tag %] str = String.new('foo bar baz wiz waz woz') [% tt_end_tag %]
          [% tt_start_tag %] str.substr(4, 3) [% tt_end_tag %]    # bar

  If $length is not specified then it returns everything from the $offset to the end of the string.

          [% tt_start_tag %] str.substr(12) [% tt_end_tag %]      # wiz waz woz

  If both $length and $replacement are specified, then the method replaces everything from $offset for $length characters with $replacement. The substring removed from the string is then returned.

          [% tt_start_tag %] str.substr(0, 11, 'FOO') [% tt_end_tag %]   # foo bar baz
          [% tt_start_tag %] str [% tt_end_tag %]                        # FOO wiz waz woz

[%- END %] [% WRAPPER subsection title = "Mutation Methods" -%]

These methods modify the internal value of the string. For example:

        [% tt_start_tag %] USE str=String('foobar') [% tt_end_tag %]

        [% tt_start_tag %] str.append('.html') [% tt_end_tag %]	# str => 'foobar.html'

The value of the String 'str' is now 'foobar.html'. If you don't want to modify the string then simply take a copy first.

        [% tt_start_tag %] str.copy.append('.html') [% tt_end_tag %]

These methods all return a reference to the String object itself. This has two important benefits. The first is that when used as above, the String object 'str' returned by the append() method will be stringified with a call to its text() method. This will return the newly modified string content. In other words, a directive like:

        [% tt_start_tag %] str.append('.html') [% tt_end_tag %]

will update the string and also print the new value. If you just want to update the string but not print the new value then use CALL.

        [% tt_start_tag %] CALL str.append('.html') [% tt_end_tag %]

The other benefit of these methods returning a reference to the String is that you can chain as many different method calls together as you like. For example:

        [% tt_start_tag %] String.append('.html').trim.format(href) [% tt_end_tag %]

Here are the methods:

- push($suffix, ...) / append($suffix, ...)

  Appends all arguments to the end of the string. The append() method is provided as an alias for push().

          [% tt_start_tag %] msg.push('foo', 'bar') [% tt_end_tag %]
          [% tt_start_tag %] msg.append('foo', 'bar') [% tt_end_tag %]

- pop($suffix)

  Removes the suffix passed as an argument from the end of the String.

          [% tt_start_tag %] USE String 'foo bar' [% tt_end_tag %]
          [% tt_start_tag %] String.pop(' bar')   [% tt_end_tag %]   # => 'foo'

- unshift($prefix, ...) / prepend($prefix, ...)

  Prepends all arguments to the beginning of the string. The prepend() method is provided as an alias for unshift().

          [% tt_start_tag %] msg.unshift('foo ', 'bar ') [% tt_end_tag %]
          [% tt_start_tag %] msg.prepend('foo ', 'bar ') [% tt_end_tag %]

- shift($prefix)

  Removes the prefix passed as an argument from the start of the String.

          [% tt_start_tag %] USE String 'foo bar' [% tt_end_tag %]
          [% tt_start_tag %] String.shift('foo ') [% tt_end_tag %]   # => 'bar'

- left($pad)

  If the length of the string is less than $pad then the string is left formatted and padded with spaces to $pad length.

          [% tt_start_tag %] msg.left(20) [% tt_end_tag %]

- right($pad)

  As per left() but right padding the String to a length of $pad.

          [% tt_start_tag %] msg.right(20) [% tt_end_tag %]

- center($pad) / centre($pad)

  As per left() and right() but formatting the String to be centered within a space padded string of length $pad. The centre() method is provided as an alias for center() to keep Yanks and Limeys happy.

          [% tt_start_tag %] msg.center(20) [% tt_end_tag %]    # American spelling
          [% tt_start_tag %] msg.centre(20) [% tt_end_tag %]    # European spelling

- format($format)

  Apply a format in the style of sprintf() to the string.

          [% tt_start_tag %] USE String("world") [% tt_end_tag %]
          [% tt_start_tag %] String.format("Hello %s\n") [% tt_end_tag %]  # => "Hello World\n"

- upper()

  Converts the string to upper case.

          [% tt_start_tag %] USE String("foo") [% tt_end_tag %]

          [% tt_start_tag %] String.upper [% tt_end_tag %]  # => 'FOO'

- lower()

  Converts the string to lower case

          [% tt_start_tag %] USE String("FOO") [% tt_end_tag %]

          [% tt_start_tag %] String.lower [% tt_end_tag %]  # => 'foo'

- capital()

  Converts the first character of the string to upper case.

          [% tt_start_tag %] USE String("foo") [% tt_end_tag %]

          [% tt_start_tag %] String.capital [% tt_end_tag %]  # => 'Foo'

  The remainder of the string is left untouched. To force the string to be all lower case with only the first letter capitalised, you can do something like this:

          [% tt_start_tag %] USE String("FOO") [% tt_end_tag %]

          [% tt_start_tag %] String.lower.capital [% tt_end_tag %]  # => 'Foo'

- chop()

  Removes the last character from the string.

          [% tt_start_tag %] USE String("foop") [% tt_end_tag %]

          [% tt_start_tag %] String.chop [% tt_end_tag %]	# => 'foo'

- chomp()

  Removes the trailing newline from the string.

          [% tt_start_tag %] USE String("foo\n") [% tt_end_tag %]

          [% tt_start_tag %] String.chomp [% tt_end_tag %]	# => 'foo'

- trim()

  Removes all leading and trailing whitespace from the string

          [% tt_start_tag %] USE String("   foo   \n\n ") [% tt_end_tag %]

          [% tt_start_tag %] String.trim [% tt_end_tag %]	# => 'foo'

- collapse()

  Removes all leading and trailing whitespace and collapses any sequences of multiple whitespace to a single space.

          [% tt_start_tag %] USE String(" \n\r  \t  foo   \n \n bar  \n") [% tt_end_tag %]

          [% tt_start_tag %] String.collapse [% tt_end_tag %]   # => "foo bar"

- truncate($length, $suffix)

  Truncates the string to $length characters.

          [% tt_start_tag %] USE String('long string') [% tt_end_tag %]
          [% tt_start_tag %] String.truncate(4) [% tt_end_tag %]  # => 'long'

  If $suffix is specified then it will be appended to the truncated string. In this case, the string will be further shortened by the length of the suffix to ensure that the newly constructed string complete with suffix is exactly $length characters long.

          [% tt_start_tag %] USE msg = String('Hello World') [% tt_end_tag %]
          [% tt_start_tag %] msg.truncate(8, '...') [% tt_end_tag %]   # => 'Hello...'

- replace($search, $replace)

  Replaces all occurences of $search in the string with $replace.

          [% tt_start_tag %] USE String('foo bar foo baz') [% tt_end_tag %]
          [% tt_start_tag %] String.replace('foo', 'wiz')  [% tt_end_tag %]  # => 'wiz bar wiz baz'

- remove($search)

  Remove all occurences of $search in the string.

          [% tt_start_tag %] USE String('foo bar foo baz') [% tt_end_tag %]
          [% tt_start_tag %] String.remove('foo ')  [% tt_end_tag %]  # => 'bar baz'

- repeat($count)

  Repeats the string $count times.

          [% tt_start_tag %] USE String('foo ') [% tt_end_tag %]
          [% tt_start_tag %] String.repeat(3)  [% tt_end_tag %]  # => 'foo foo foo '

[%- END %] [%- END %] [% WRAPPER section title="AUTHOR" -%]

Andy Wardley <abw@wardley.org>

[% ttlink('http://wardley.org/', 'http://wardley.org/') -%]

[%- END %] [% WRAPPER section title="VERSION" -%]

2.4, distributed as part of the Template Toolkit version 2.19, released on 27 April 2007.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]

      Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.

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

[%- END %] [% WRAPPER section title="SEE ALSO" -%]

[% ttlink('Template::Plugin', 'Template::Plugin') -%]

[%- END %]