in reply to determine the name of a function that contains a specified string

#!/usr/bin/perl use warnings; use strict; use 5.10.0; my $search_term = 'ipconfig'; my %subs; my $name; while ( my $line = <DATA> ){ if ( $line =~ /^sub\s+/ ){ $name = (split /\s+/, $line)[1]; $subs{ $name } = 0; next; } $subs{ $name } = 1 if $line =~ /$search_term/; } while ( my ($k,$v) = each %subs ){ say "sub $k calls $search_term" if $v; } __DATA__ sub one { #one #two #three #ipconfig #four } sub two { #hello #there #wonderful #world } sub three { #ifconfig #ipconfig #nmap -sS -P0 localhost #tcpdump -n -i re0 -s 0 -w a.file

EDIT: made it a bit more generic and descriptive

  • Comment on Re: determine the name of a function that contains a specified string
  • Download Code

Replies are listed 'Best First'.
Re^2: determine the name of a function that contains a specified string
by pixxyman (Initiate) on Apr 03, 2012 at 10:03 UTC
    Thanks for the perl module stevieb, unfortunately we are locked down as to what perl versions we can use. However you snipped of code above is exactly what I need :-D I just have to read up on hashes now to understand what you are doing :)