[Israel.pm] What does this code do? (No. 3)

Shlomi Fish shlomif at iglu.org.il
Sun Jul 11 03:26:07 PDT 2004


Here is the next installment in the "What does this code do?" series of 
quizzes. You can post solutions to the list, but mark them with a [SPOILER] 
in the title. Other discussions are also welcome.

The script in question is:

<<<
#!/usr/bin/perl -w

use strict;

my %lines_to_extract;

open DECLS, "bash analyze-globals.sh |";
while (<DECLS>)
{
    chomp($_);
    my ($file,$line_num,$text) = /^(\w+\.c):(\d+):(.*)$/;
    push @{$lines_to_extract{$file}}, +{ 'l' => $line_num, 't' => $text, };
}
close(DECLS);

my @lines_to_put = ();
foreach my $file (keys(%lines_to_extract))
{
    my $lines = $lines_to_extract{$file};
    my $line_num = 1;
    my $extracted_line_idx = 0;
    open I, "<$file";
    open O, ">$file.new";
    while (<I>)
    {
        if (($extracted_line_idx < @$lines) && 
            ($line_num == $lines->[$extracted_line_idx]->{l})
           )
        {
            push @lines_to_put, $_;
            $extracted_line_idx++;
        }
        else
        {
            print O $_;
        }
    }
    continue
    {
        $line_num++;
    }
    close(I);
    close(O);
    rename("$file.new", "$file");
}
open GLOBALS, ">globals.c";
print GLOBALS @lines_to_put;
close(GLOBALS);
>>>

It makes use of the bash script analyze-globals.sh which is:

<<<
#!/bin/bash
grep -nP '^\S.*;' *.c | grep -v static | grep -vP '^\w+\.c:\d+:}'
>>>

Regards,

	Shlomi Fish




-- 

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://shlomif.il.eu.org/

Knuth is not God! It took him two days to build the Roman Empire.



More information about the Perl mailing list