TATMWTDI There are too many ways to do it #13

Solution #8 - Recursive

sub recursive_perl
{
    my $string = shift;

    my $recurse;

    my @chars = split(//, $string);

    $recurse = sub {
        my ($arg) = (@_);
        my ($rest_of_chars) = [ @$arg];
        if (@$rest_of_chars == 0)
        {
            return ("", 0);
        }
        my $head = shift(@$rest_of_chars);
        my $tail = $rest_of_chars;
        my ($processed_string, $was_period_found) = $recurse->($tail);
        if ($was_period_found)
        {
            return ((($head eq "." ? "" : $head) . $processed_string), 1);
        }
        else
        {
            return ($head . $processed_string, ($head eq "."));
        }
    };

    return +($recurse->([@chars]))[0];
}
Copyright © 2005 Shlomi Fish