Re: Need to extract only the function name no brackets reqd.



Nath, Alok (STSD) wrote:
Hi,
I have this program which extract function names from the files.
My only problem is I want to extract only the function names{no
brackets () needed } .
Currently its extract all words after word sub but it all
returns the parentheses after that.


my $fileLoc = "//opt//sva//perl//lib//" ;
my $funcCount = 0 ;

opendir (DIR, $fileLoc) || die "Cannot opendir $!" ;
my @libs = readdir(DIR) ;

open (RES, ">out.txt") || die "Cannot open out.txt : $!" ;

foreach (@libs){
if(/pm$/){
open(FILE, $fileLoc.$_) or die("Unable to open $!") ;

print RES "Inside file >> $_ \n" ;
while (<FILE>){
chomp ;
if (/sub/){
my $sub ;
my $subName ;
($sub, $subName) = split /\s+/ ;
# print RES $_ ."\n" ;
print RES $subName."\n" ;
$funcCount ++ ;
}
}
print "\n\n" ;
close(FILE) ;
}

}

print RES "Total Functions : $funcCount \n" ;
close RES ;
closedir DIR ;

Hi Alok

If you're going to sell Perl you should program to its strengths. I hope you
find the program below clearer. I wasn't sure whny you doubled the slashes in
the path, but I left them in case there was a good reason. Were you getting
confused about escaping backslashes?

Rob


use strict;
use warnings;

my $fileLoc = "//opt//sva//perl//lib//";

open RES, '>out.txt' or die "Cannot open out.txt: $!";

my $funcCount = 0;

chdir $fileLoc or die $!;
opendir DIR, '.' or die "Cannot open directory: $!";

while (my $file = readdir DIR) {

next unless $file =~ /\.pm$/;

open FILE, $file or die "Unable to open file: $!";

print RES "Inside file >> $file\n" ;

while (<FILE>) {
chomp ;

if (/sub\s+(\w+)/) {
print RES $1, "\n" ;
$funcCount++;
}
}
close FILE;

print RES "\n\n" ;
}

closedir DIR ;

print RES "Total Functions: $funcCount\n" ;
close RES;

.



Relevant Pages

  • Need to extract only the function name no brackets reqd.
    ... I have this program which extract function names from the files. ... Currently its extract all words after word sub but it all ... print RES $subName."\n"; ...
    (perl.beginners)
  • Varying Results with Extract Function
    ... I have a problem when using the extract function: ... If I use a multiple dimension nonemptycrossjoin to extract the Customer ...
    (microsoft.public.sqlserver.olap)
  • Re: $_SESSION Array
    ... I put var_dumps for both arrays right before and after the extract ... It's the extract function that's doing it. ... Maybe if I used one of the extract_type or prefix parameters, ...
    (comp.lang.php)