#!/usr/bin/perl -w $data = { key1 => 'value1', key2 => 'value2', }; sub macro_replace { $_ = shift; my $helper = sub { $_ = shift; return $data->{$_} if $data->{$_}; return undef; }; chomp (my $pwd = `pwd`); s/\[\[(pwd)\]\]/$pwd/g; s/\[\[(\w+)\]\]/@{[$helper->($1)]}/g; return $_; } print macro_replace ("blah [[key1]] [[key2]] [[pwd]] blah blah\n");