#!/usr/bin/env perl use strict; use warnings; my $text; my $match = 0; my $regex = qr/int\s+main\s*\(/; { local $/; # enable "slurp" mode $text = ; # read whole file } while ($text =~ /$regex/gms) { print "Match found on line ", scalar ( split /\n/, substr $text, 0, pos($text) ) - scalar ( split /\n/, ${^MATCH} ) + 1, "\n"; $match = 1; } print "No matches found\n" unless $match; __DATA__ /* Dummy multi-main C code */ int main(int argc, char** argv) int main(int argc, char** argv) /* END */