tags:

views:

56

answers:

2

Hello All: I have a file with the following syntax in some_1.xyz

module some_1 {
INPUT PINS
OUTPUT PINS
}

and I want to insert APPLY DELAYS xx and APPLY LOADS ld after line module some_1 {

The following code works fine for just one file i.e., if I replace some_1.xyz to *.xyz then the script doesn't work. I tried introducing sleep(xx) but the code doesn't work for multiple files and I could not figure out why it isn't working. Any pointers is appreciated. Thanks

@modulename_array = `grep "module " some_1.xyz | cut -f 2 -d ' '`;
@line = `grep "module " some_1.xyz`;

chomp(@line);
chomp(@kfarray);

$i = 0;
foreach (@modulename_array) {
  print "Applying delay and load to $_.xyz $line[$i] \n";

  `perl -ni -le 'print; print "\tAPPLY DELAY xx \n \tAPPLY LOADS  ld\n" if/$line[$i]/' $_.xyz`;
  $i++;
  #sleep(3);

}
A: 

I have no idea why your code isn't working, but I have trouble following your use of Perl inside backticks inside Perl. This is untested, but should work. I suggest you also "use strict;" and "use warnings;".

my @files = ("some_1.xyz", "some_2.xyz", ... );
for my $file in ( @files )
{
    my $outfile = $file + ".tmp";
    open( my $ins, "<", $file ) or die("can't open " . $file . " for reading: " . $!);
    open( my $outs, ">", $outfile ) 
        or die("can't open " . $outfile . " for writing: " . $!);
    while ( my $line = <$ins> )
    {
        print { $outs } $line;
        if ( $line =~ m/^module\s+/ )
        {
             print { $outs } "\tAPPLY DELAY xx\n\tAPPLY LOADS ld\n";
        }
    }
    rename( $outfile, $file );
}
robert
`use warnings` and `use strict` are missing, `/module /` -> `/^module\s+/`, `die("...")` -> `die "can't open $filenane: $?"` and it would be about right, considering that no OS/platform was mentioned in the question. PS line with `my $outfile = ...` is missing the semicolon.
Dummy00001
PS line with open($outfile) is missing the semicolon
vol7ron
The open outfile line has a semicolon, it's just on the next line.
robert
A: 

And what's wrong with the easy solution?:

$data=`cat /the/input/file`;
$data=~s/some_1 {\n/some_1 {\nAPPLY DELAYS xx\nAPPLY LOADS ld\n/gm;
print $data;
hlynur