#!/usr/bin/perl -w use strict; my $file = shift; my $marker = "GEN_INCLUDE"; sub expand { my ($work, $ofh) = @_; printf($ofh "\t# Section autogenerated using %s on %s\n", $0, scalar localtime) || die "Error writing to $work: $!"; for my $file (@ARGV) { next if $file eq $0 || $file eq "gen_include"; print($ofh "\t'$file' => <<'$marker',\n") || die "Error writing to $work: $!"; open(my $fh, "<", $file) || die "Could not open $file: $!"; local $_; while (<$fh>) { $_ .= "\n" unless /\n\z/; print($ofh $_) || die "Error writing to $work: $!"; } close($fh) || die "Error closing $file: $!"; print($ofh "$marker\n") || die "Error writing to $work: $!"; } } sub change_file { my $work = $file; $work =~ s/\.\w+\z/.tmp/ || die "No extension on file $file"; my $expanded = 0; open(my $ifh, "<", $file) || die "Could not open $file: $!"; { open(my $ofh, ">", $work) || die "Could not create $work: $!"; eval { local $_; NORMAL: while (<$ifh>) { print($ofh $_) || die "Error writing to $work: $!"; if (/^\s*#\s*START\s+GEN_INCLUDE\b/) { expand($work, $ofh); $expanded++; while (<$ifh>) { if (/^\s*#\s*STOP\s+GEN_INCLUDE\b/) { print($ofh $_) || die "Error writing to $work: $!"; next NORMAL; } } die "START GEN_INCLUDE without STOP in $file\n"; } } close($ifh) || die "Error reading from $file: $!"; close($ofh) || die "Error closing $work: $!"; die "No expansion done, so why did you call $0 ?\n" if !$expanded; rename($work, $file) || die "Could not rename $work to $file: $!"; }; } if ($@) { unlink($work) || die "Could not unlink $work: $! after $@"; die $@; } } change_file($file);