local $/; # undef $/ so we glob data in $data = ; # get all our data as a string $data =~ s|//.*\n|\n|g; # strip single line comments $data =~ s|/\*.*?\*/||gs; # strip multi-line comments $data =~ s|^\s*\n||gm; # strip blank lines $data =~ s|^\s+||gm; # strip leading whitespace $data =~ s|\s+$||gm; # strip trailing whitespace # do the magic and split into functions @functions = split /(?=\bfunction\s)/, $data; # pudding proof print "$_ ---\n" for @functions; __DATA__ var answer = 42; // just because function 1 { blah // comment; } /* this function is no good function 2 { blah } */ function 3 { if (functionvar){ // var name not function keyword // do the blah stuff } } /* this is another comment */ function 4 { just another perl hacker }