-1

I'm trying to analyse a code base (many .c files) by writing Perl script using (Scitools) Understand's Perl API.

I'm aware that list of variables, functions etc. are easy to obtain for a given .c file.

Probably someone who has worked with Understand Perl API could point me in the right direction to search for a specific string that is present in many lines of a file? and possible return the line number?

serenesat
  • 4,611
  • 10
  • 37
  • 53
EnsieT
  • 91
  • 5
  • Did you try `grep -n`? – cdarke Dec 06 '17 at 11:47
  • Dear @cdarke , Thanks for the quick response. Once I get the lexeme, then I can look for the substring using grep etc.. Thing is , I'm not sure , if there is a lexer that I can loop in the entire .c file and even if it is possible, I believe it would not be efficient. Have you already used Understand for c/c++? – EnsieT Dec 06 '17 at 11:52
  • I know Perl and I know C and C++, but I don't know Understand, sorry. – cdarke Dec 06 '17 at 11:55
  • no problem. Thanks anyways @cdarke. – EnsieT Dec 06 '17 at 11:58
  • **cdarke** is referring to the *command line* `grep` tool. There is no need to involve Understand just to find a string in some files. – Borodin Dec 06 '17 at 13:57
  • Thanks @Borodin. Well, I understand what you say but thing is, the perl script is just one small part of another process. So, lot of other things are involved inside and outside of the script including how it is being executed. That's the reason why I would like to insert a small functionality inside the existing script. Hope you got my point. – EnsieT Dec 06 '17 at 14:23
  • Especially with something obscure, you'll need to do better than that. Add a link to Understand's docs, code that does something similar, etc – ikegami Dec 06 '17 at 17:50
  • Sorry @ikegami , added link to online manual including sample codes in the description. – EnsieT Dec 06 '17 at 22:48
  • What problem are you having? Getting the entity, or searching a string for another string? – ikegami Dec 07 '17 at 21:04

1 Answers1

0

Sounds like you simply want

  use Understand qw( );

  my $db = Understand::open("test.udb");

  # Find all 'File' entities that match test*.cpp
  for my $file_ent ($db->lookup("test*.cpp","File")) {
       my $file_contents = $file_ent->contents();
       printf "File %s matches", $file_ent->name
          if $file_contents =~ /pattern/;
  }
ikegami
  • 367,544
  • 15
  • 269
  • 518