%templates = ( 1 => "template one: data = #DATA#", 2 => "template two: data = #DATA#", 3 => "template three: data = #DATA#", ERR => "bad template reference, but the data = #DATA#", ); %data = ( 1 => "data value 1", 2 => "data value 2", 3 => "data value 3", ERR => "bad data reference" ); for (1..20) { $t = $templates{ int rand(5) } || $templates{'ERR'}; ## assertion: we always get a template that will be usable ## in the substitution below. $d = $data{ int rand(5) } || $data{'ERR'}; ## assertion: we always get a value that will be usable ## in the substitution below. $t =~ s/#DATA#/$d/; print $t, "\n"; }