#!/usr/local/bin/perl -l -w use strict; my $str = "SAdBoy"; $str =~ s/($str)/fix_case($1, 'badgirl')/eig; print $str; sub fix_case { my ($match_word, $replace_word) = @_; my @rep = split //, $replace_word; my $i = 0; for (split '', $match_word) { $rep[$i] = $_ eq lc($_) ? lc($rep[$i++]) : uc($rep[$i++]); } return join '', @rep; }