my %repls = qw( < < > > foo bar bar foo f baz ); my $replstr = join '|', sort {length $b <=> length $a} keys %repls; s/$replstr/$repls{$&}/g;