#!/usr/bin/env perl use Modern::Perl; sub fix { return join '|', '', ( map { sprintf('%02d',length $_), $_ } split /\./, shift ), '00', ''; } say fix 'foo.com'; say fix 'foo.foobar.com'; say fix 'foo.foobar.foo.com';