Peter’s blog ✴ Week 364 ✴ 9 March 2026

THE WEEKLY CHALLENGE
Weird encodings

The Perl Camel

Task 2

Goal parser

You are given a string, $str. Write a script to interpret the given string using Goal Parser. The Goal Parser interprets “G” as the string “G”, “()” as the string “o”, and “(al)” as the string “al”. The interpreted strings are then concatenated in the original order.

Examples


Example 1
Input: $str = "G()(al)"
Output: "Goal"
G    -> "G"
()   -> "o"
(al) -> "al"

Example 2
Input: $str = "G()()()()(al)"
Output: "Gooooal"
G       -> "G"
four () -> "oooo"
(al)    -> "al"

Example 3
Input: $str = "(al)G(al)()()"
Output: "alGaloo"
(al) -> "al"
G    -> "G"
(al) -> "al"
()   -> "o"
()   -> "o"

Example 4
Input: $str = "()G()G"
Output: "oGoG"
() -> "o"
G  -> "G"
() -> "o"
G  -> "G"

Example 5
Input: $str = "(al)(al)G()()"
Output: "alalGoo"
(al) -> "al"
(al) -> "al"
G    -> "G"
()   -> "o"
()   -> "o"

Analysis

This is even simpler than task 1 in that no executed s||| is required.

I'm struggling to think of a use case for this!

Perl Weekly’s review

from Perl Weekly issue 764

This post shares Peter's solutions to Perl Weekly Challenge 364, presenting clear and well-structured Perl implementations for both tasks. It explains the reasoning behind the approach and walks the reader through the logic step by step, making the solutions easy to follow. Overall, it is a solid and educational write-up that demonstrates practical Perl problem-solving and clean coding style.

Try it 

Try running the script with any input:



example: G()(al)G()(al)G()(al)

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-03-09
use utf8;     # Week 364 - task 2 - Goal parser
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

goal_parser('G()(al)');
goal_parser('G()()()()(al)');
goal_parser('(al)G(al)()()');
goal_parser('()G()G');
goal_parser('al)(al)G()()');

sub goal_parser {
    
    my ($string, $base);
    
    # initialise
    $string = $_[0];
    say qq[\nInput:  '$string'];
    $base = ord('a') - 1;
    
    # apply rules
    $string =~ s|\(\)|o|g;
    $string =~ s|[\(\)]||g;
    
    say qq[Output: '$string'];
}

8 lines of code

Output from script


Input:  'G()(al)'
Output: 'Goal'

Input:  'G()()()()(al)'
Output: 'Gooooal'

Input:  '(al)G(al)()()'
Output: 'alGaloo'

Input:  '()G()G'
Output: 'oGoG'

Input:  'al)(al)G()()'
Output: 'alalGoo'

 

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