open my $file, '<', $_ or die "Couldn't read file $_: $!"; while (<$file>) { # try and guess how deep we are to see when the containing block ends $braces += () = $_ =~ /\{/g; $braces -= () = $_ =~ /\}/g; if ($in_a_method) { $sub_braces += () = $_ =~ /\{/g; $sub_braces -= () = $_ =~ /\}/g; } # I hope no one uses curly braces in a string... if ($braces < 0 || $sub_braces < 0) { die "Hook finder failed in $filename, line $line."; } # check to see if any block finished if ($braces == 0) { undef $in_a_class; undef $in_a_function; } elsif ($sub_braces == 0) { undef $in_a_method; } # see if we're entering a class if (/class\s+(.+?)\s+\{/) { $in_a_class = $1; $braces = 1; } # or a function elsif (/function\s+(.+?)\s+\{/) { $in_a_function = $1; $braces = 1; } # or a method elsif ($in_a_class && /function\s+(.+?)\s+\{/) { $in_a_method = "$in_a_class::$1"; $sub_braces = 1; } # this is where the hook code is fetched (although it's not necessarily used here :/) elsif (/vBulletinHook::fetch_hook\('(.+?)'\)/) { $options{'debug'} > 1 && print "Found hook $1 in $_\n($in_a_function, $in_a_class, $in_a_method)\n"; } }