fileset: array replace
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 20 Nov 2009 00:01:26 +0000 (01:01 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 2 Dec 2009 15:09:58 +0000 (16:09 +0100)
lib/File/Rewrite.pm
t/10-fileset.t

index 5a30a32989c80df3b8e91dca1ed9596333bef71e..784542d9f47074854c9789811429148a98f20087 100644 (file)
@@ -19,7 +19,16 @@ sub fileset {
        while (readline $src) {
                chomp;
                if ($_ =~ (ref $search ? $search : qr/^\Q$search\E$/)) {
-                       if (defined $replace and $_ eq $replace) {
+                       if (ref $replace eq 'ARRAY') {
+                               if (@$replace and $_ eq $replace->[0]) {
+                                       shift @$replace;
+                               }
+                               else {
+                                       $changes++;
+                                       next;
+                               }
+                       }
+                       elsif (defined $replace and $_ eq $replace) {
                                undef $replace;
                        }
                        else {
@@ -31,8 +40,10 @@ sub fileset {
        }
        close $src;
        if (defined $replace) {
-               $changes++;
-               print {$dest} $replace, $/;
+               for (ref $replace eq 'ARRAY' ? @$replace : $replace) {
+                       $changes++;
+                       print {$dest} $_, $/;
+               }
        }
        close $dest;
 
index dc800efa2a424d06c1605730c998e9771cfabfd0..db1575e9e51daf277ed0404fe7c16ac32a730ae0 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 13;
+use Test::More tests => 16;
 use Test::NoWarnings;
 
 use autodie;
@@ -57,5 +57,9 @@ testfileset('replace string', "1$/2$/3$/",  [4, 1],             2, "2$/3$/4$/");
 testfileset('replace all',    $BASICBODY,   ['', qr/^re/],      3, "noremove$/.$/$/");
 testfileset('keep first',     "foo$/bar$/foo$/", ['foo'],       1, "foo$/bar$/");
 
-is(remove_tree($target), 11, 'no unexpected files');
+testfileset('add new array',  ".$/",        [['foo','.'], ''],  2, ".$/foo$/.$/");
+testfileset('replace by array', "$/foo$/.$/", [['foo',''], ''], 3, "foo$/.$/foo$/$/");
+testfileset('partial add',    "foo$/$/",   [['foo','.'], 'foo'], 1, "foo$/$/.$/");
+
+is(remove_tree($target), 14, 'no unexpected files');