Author image Edmund von der Burg


Text::Variations - create many variations of the same message


    use Text::Variations;

    # Simple variables that change each time
    my $mood     = Text::Variations->new( [ 'happy',  'sad' ] );
    my $activity = Text::Variations->new( [ 'shopping', 'surfing' ] );
    my $facebook_status = "I'm feeling $mood - going $activity now\n";

    # build up complex strings with interpolations
    my $announcement = Text::Variations->new(
        "The train at platform {{platform}} has been ",
        [   'delayed',
        " due to ",
        [   "engineering works",
            "maintenance issues",
            "operating difficulties",
            "a passenger incident",
            "leaves on the tracks",
            "the wrong kind of snow",
        " - we apologise for any ",
        [   "inconvenience",
            "disruption to your journey",
            "missed onward connections",
        " this may have caused\n"

    print $announcement->generate( { platform => 4 } );


Often you have a simple message that you want to get across, but you don't want it to be the same format each time. This module helps you do that.

You can specify several alternatives and a random one will be picked each time.

This module was written to generate the tweets for every time someone signed up or donated. To keep the tweets interesting and feel more human they all had to be different, but all generated from code.



    my $tv = Text::Variations->new(
        "just a simple string",
        [ 'or', 'an', 'arrayref', 'of', 'alternatives' ],
        "can have {{placeholders}} to interpolate",

Create a new Text::Variations object.

The arguments are an array of strings, arrayrefs of alternatives, or other T::V objects.

You can include placeholders for variables by using '{{key}}' in the strings. These placeholders will then be replaced by the value you specify in the arguments to generate.


    my $string = $tv->generate();
    my $string = "$tv";
    my $string = $tv->generate( { name => 'Joe', } );

Generates and returns a string. The arguments are used to fill in the placeholders if there are any. The various parts are chosen at random. If there are any embedded T::V objects then the arguments are passed on to them so as well.

Stringification is overloaded so that it is identical to calling generate with no arguments.


Catalyst::Plugin::Twitter - used to send the tweets that this module was created to generate.


If you're hoping to generate different looking messages make sure that there is plenty of variation in the first part. Also think about creating several different forms as T::V objects and then combining all of those into a single final T::V object.


... the British rail companies, for delaying my journey and providing so much material for the example code. This module was entirely written on the late running service between London Paddington and Newport.


Edmund von der Burg <>.


Contributions welcome:

TRavis build tests:


Copyright (c) 2009, Edmund von der Burg <>. All rights reserved.

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