sub convert { ### This subroutine accepts a single parameter (a scalar which it treats as a string) ### and sequentially substitutes out "invalid" characters with their "valid" counterparts ### based on a fixed list of characters to replace. my $input_stream = shift; return undef unless $input_stream; my %char_swap_hash = ( 1 => {OLD => '&[^(amp;)|(lt;)|(gt;)]', NEW => '&'}, # don't match legit '&' codes 2 => {OLD => '<', NEW => '<'}, 3 => {OLD => '>', NEW => '>'} ); $input_stream =~ s/$char_swap_hash{$_}{OLD}/$char_swap_hash{$_}{NEW}/g for keys %char_swap_hash; return $input_stream; }