Peter’s blog ✴ Week 369 ✴ 13 April 2026

THE WEEKLY CHALLENGE
Fun with strings

The Perl Camel

Task 1

Valid tag

You are given a given a string caption for a video. Write a script to generate tag for the given string caption in three steps as mentioned below:

  • Format as camelCase: Starting with a lower-case letter and capitalising the first letter of each subsequent word.
  • Merge all words in the caption into a single string starting with
    a #.
  • Sanitise the String: Strip out all characters that are not English letters (a-z or A-Z).
  • Enforce Length: If the resulting string exceeds 100 characters, truncate it so it is exactly 100 characters long.

Examples


Example 1
Input: $caption = 'Cooking with 5 ingredients!'
Output: '#cookingWithIngredients'

Example 2
Input: $caption = 'the-last-of-the-mohicans'
Output: '#thelastofthemohicans'

Example 3
Input: $caption = '  extra spaces here'
Output: '#extraSpacesHere'

Example 4
Input: $caption = 'iPhone 15 Pro Max Review'
Output: '#iphoneProMaxReview'

Example 5
Input: $caption = 'Ultimate 24-Hour Challenge: Living in 
   a Smart Home controlled entirely by Artificial 
   Intelligence and Voice Commands in the year 2026!'
Output: '#ultimateHourChallengeLivingInASmartHomeControll
edEntirelyByArtificialIntelligenceAndVoiceCommandsIn'

Analysis

This is all achievable using simple regexes and built-in functions, but the order is important. Applying the steps in the order listed in the challenge doesn't quite achieve the desired output, but I think this does:

  • lower-case the whole string
  • remove anything that isn't a letter or a space
  • remove initial spaces or initial non-letters
  • replace space+letter with upper-case letter
  • Remove any remaining spaces
  • Prefix with '#'
  • Truncate to max 100 chars

I have altered some of the supplied examples to test some edge cases.

Perl Weekly’s review

from PW issue 769

In his article, Peter presents a practical and polished approach to developing an order of operations for the sanitisation of strings. By organising the procedure so that lower case, regular expression character removal, and space & character combination are completed before the creation of camelCase, the end product meets the requirements for both camel case formatting as well as length requirements while still producing clean, effective code.

Try it 

Try running the script with any input:



example: (1) Today's test!

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-04-13
use utf8;     # Week 369 - task 1 - Valid tag
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

valid_tag('Cooking with 5 ingredients!');
valid_tag('the-last-of-the-mohicans');
valid_tag('  extra  spaces  here  ');
valid_tag('!! iPhone 15 Pro Max Review !!');
valid_tag('Ultimate 24-Hour Challenge: Living in a Smart Home controlled entirely by Artificial Intelligence and Voice Commands in the year 2026!');

sub valid_tag {
    
    my ($text);
    
    # initialise
    $text = $_[0];
    say qq[\nInput:  '$text'];
    
    $text = lc($text);             # lower case all
    $text =~ s|[^a-z\s]||g;        # remove non-letters
    $text =~ s|^\s+||g;            # no initial spaces
    $text =~ s| ([a-z])|uc($1)|ge; # ucase space+letter
    $text =~ s|\s+||g;             # no spaces
    $text = substr('#' . $text, 0, 100); # prefix '#'
                                   # and max 100 chars  
    say qq[Output: '$text'];
}

11 lines of code

Output from script


Input:  'Cooking with 5 ingredients!'
Output: '#cookingWithIngredients'

Input:  'the-last-of-the-mohicans'
Output: '#thelastofthemohicans'

Input:  '  extra  spaces  here  '
Output: '#extraSpacesHere'

Input:  '!! iPhone 15 Pro Max Review !!'
Output: '#iphoneProMaxReview'

Input:  'Ultimate 24-Hour Challenge: Living in a Smart Home controlled
   entirely by Artificial Intelligence and Voice Commands in the year
   2026!'
Output:
   '#ultimateHourChallengeLivingInASmartHomeControlledEntirelyByArtifi
   cialIntelligenceAndVoiceCommandsIn'

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain