#!/usr/bin/perl/ use strict; use warnings; my $key = 'foo'; my $value = 'bar'; my $str = sprintf('%-50s%s', $key, $value); my $l = length($str)-50; die "wrong pattern: $str" unless $str =~ /^\w+\s+\w{$l}$/;