Ad-vic
Ad-vic

Reputation: 529

perl - need to add set of lines into a file

pChainCtrl 
pChainName 
pDef 

pChainCtrl 
pArgs 

pChainCtrl
pChainCtrl 
name 
pChainTable

These are arguments of few functions,Need to add them to a function template in front of "@param[in ]"

TEMPLATE

/**
********************************************************************************
*  @fn                                                            
*  @brief
*  @param[in ]                                                         
*  @return
********************************************************************************
*/

MY CODE

use strict;
use warnings;


open(FILE3,"< functions2.txt")or die $!;
my @array1 = <FILE3>;

foreach my $arg (@array1){
    open(my $FILE4,"+< function_template.txt")or die $!;;
    seek( $FILE4, 197, 0);     // takes pointer infront of @param[in ]
    chomp $arg;
    print $FILE4 "$arg";
close($FILE4);
}
close(FILE3);

It adds arguments one by one.

I need to add the each set of arguments to file template which will be copied elsewhere(I have code for that) and then move on to next set of arguments

OUTPUT REQUIRED

/**
********************************************************************************
*  @fn                                                            
*  @brief
*  @param[in ]   pChainCtrl 
                 pChainName 
                 pDef                                                        
*  @return
********************************************************************************
*/

Upvotes: 2

Views: 65

Answers (1)

mpapec
mpapec

Reputation: 50637

You can read template once and use it for each group of parameters,

use strict;
use warnings;

open(my $FILE4, "<", "function_template.txt") or die $!;
my $tl = do { local $/; <$FILE4> };
$tl =~ s|\s+$||mg;

open (my $FILE3, "<", "functions2.txt") or die $!;
my @array1 = map [ split ],
  do { local $/ = ""; <$FILE3> };

for my $arg (@array1) {
  my $s = $tl;
  $s =~ s|(param.+)|"$1   ". join "\n                 ", @$arg |e;
  print $s;
}

Upvotes: 1

Related Questions