#!/usr/bin/perl use strict; use warnings; while () { my $search = 'foo'; if (/foo/g) { print "Line $. Col ", pos() - length($search), ": $search found.\n"; } } __DATA__ This is a test. This is foo. foo is good. # Results #Line 2 Col 8: foo found. #Line 3 Col 0: foo found.