--- lib/PXElator/file.pm 2009/08/20 19:00:13 277 +++ lib/PXElator/file.pm 2009/08/26 08:43:40 290 @@ -4,6 +4,9 @@ use autodie; use Carp qw/carp confess/; use File::Path qw//; +use Data::Dump qw/dump/; + +my $debug = 1; sub mkpath { my $file = shift; @@ -18,28 +21,32 @@ mkpath $file; write_file $file, $content; my $size = -s $file; - carp "append created $size bytes in $file"; + carp "## append created $size bytes in $file"; return $size; } my $on_disk = read_file $file; - my $relaxed_content =~ s{\s+}{\\s+}gs; + my $relaxed_content = $content; + $relaxed_content =~ s{\s+}{\\s+}gs; if ( $on_disk !~ m{$relaxed_content} ) { # $content =~ s{^[\n\r]+}{\n}s; # $content =~ s{[\n\r]*$}{\n}s; - open($fh, '>>', $file); - print $fh $content; - close($fh); - - my $size = length($content); + if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) { +# warn "# insert $file\n$on_disk"; + write_file $file, $on_disk; + } else { +# warn "# append $file\n$content\n"; + open($fh, '>>', $file); + print $fh $content; + close($fh); + } - carp "append $size bytes to $file"; - warn $content; - return $size; + carp "## append to $file"; + return -s $file; } } @@ -49,10 +56,10 @@ my $content = read_file $file; if ( $content =~ s{$from}{$to}s ) { write_file $file, $content; - carp "change $file $from => $to"; + carp "## change $file $from => $to" if $debug; return 1; } elsif ( $content !~ m{$to}s ) { - confess "can't find $from to change into $to in $file"; + confess "can't find $from to change into $to in $file in ",dump( $content ); } } @@ -62,4 +69,13 @@ write_file $file, $content; } +sub copy_once { + my ( $from, $to ) = @_; + $to ||= $from; + return if -e $to; + mkpath $to; + write_file $to, read_file $from; +# warn "# copy_once $to ", -s $to, " bytes"; +} + 1;