#!/usr/bin/perl -w use strict; my $file = "myfile"; open(FILE,$file); my $rep = &replace_text_1; foreach () { $_ =~ s/\/$rep/ if $_ =~ m/\/; print "$_"; } close(FILE); sub replace_text_1 { #some stuff #print "foobar"; #some other stuff return("foobar"); }