sub foo { my ($text, $to) = @_; $text =~ s{(\d+)}{$to}; $text; } print foo("123", 'capture 1 is $1'); #### sub bar { my ($text, $to) = @_; $text =~ s{(\d+)}{qq{qq{$to}}}ee; $text; } print bar("456", 'capture 1 is $1'); # prints capture 1 is 456