1
eErrorT ChainCtrlInitChains(ChainCtrlT* pChainCtrl,
    char* name,
    int instance)
{
    ....
}
eErrorT ChainCtrlInit(ChainCtrlT* pChainCtrl, void* pOwner)
{
    ....
}

MY CODE

open(my $FILE, "< a.c") or die $!;
my @arr = <$FILE>;
foreach(@arr){  
   if ($_ =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg) {
      my $arg = $1;
      my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
      print my @temp = map "$_\n", @arr
            unless $_ =~ /;\s*$/;
     }
} 

Basically I am extracting function arguments from the function definitions.

But in this case I am only able to extract arguments for 2nd function(ChainCtrlInit) and not for 1st function(ChainCtrlinitchains).

DESIRED OUTPUT

pChainCtrl
name
instance

pChainCtrl
pOwner

OUTPUT I AM GETTING

pChainCtrl
pOwner

3 Answers 3

4

The problem you have is when you do "foreach", $_ in the block inherits each element in the array. For example,

foreach(@arr).. the first iteration of the loop assign $_ with "eErrorT ChainCtrlInitChains(ChainCtrlT* pChainCtrl,\n", so your regex will fail.

And you also use temp variables that don't have too much use. I have improved the code as the following:

my $arr = do { local $/; <$FILE> }; #Copy one of the comments above.

#note there's a slight difference in the 'while' regex to your code
while ($arr =~ /^ \S+ \s+ \S+ \s* (\( .+? \)) /xsmg) { 
    my @args = $1 =~ /(\w+)[,)]/g; #This assumes what you want always ends in 
    #a ',' or a ')', hence the gentle modification in the 'while' regex.
    local $" = "\n";
    say "@args";
}
2

The problem is that you are reading the file line by line and so the regex can never extend across multiple lines - if you load the file then make it a single string with embedded new lines it will work

eg a quick hack gives

open(my $FILE, "< a.c") or die $!;
my @arr = <$FILE>;
my $file = join('',@arr);

 my @matches = $file =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg;
 foreach (@matches) {
      my $arg = $_;
      my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
      print my @temp = map "$_\n", @arr
            unless $_ =~ /;\s*$/;
      print "\n";
  }
2
  • thanks.. works fine now. I am new to Perl . thought it will check starting from "(" till it sees ")" even if its in separate lines.
    – Ad-vic
    Oct 8, 2013 at 14:08
  • Save a lot of work with my $file = do { local $/; <FILE> } instead of breaking it into lines just to join them again.
    – cjm
    Oct 8, 2013 at 14:56
1

@Ad-vic, in addition to @atleypnorth's solution to slurp the whole file into a string, there could be a problem within your split-map statement.

this
my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
should be this
my @arr = map /(\w+)\W*$/, split /\W*?,\W*/, $arg;
because the last element from split gets residual characters.

Once you get the hang of Perl, you could streamline it into this -

 $/ = "";
 open(my $FILE, "< a.c") or die $!;
 my $str = <$FILE>;

 foreach ( $str =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg ) {
     print map "$_\n", /(\w+) [^\w,]* (?:,|$)/xg ;
     print "\n";
 }
1
  • Just a note that this is not recommended for to parse c/c++.
    – user557597
    Oct 8, 2013 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.