fileset: explicit undef search to match unconditionally
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 20 Nov 2009 15:21:50 +0000 (16:21 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 2 Dec 2009 15:09:59 +0000 (16:09 +0100)
lib/File/Rewrite.pm
t/10-fileset.t

index 2b05ded11175fc09025378089fa0fb67e8f64d6e..b3bf3d74019bc72ed127ef8015338b2464e674bf 100644 (file)
@@ -13,17 +13,18 @@ sub fileset {
        my $tmpname = "$filename.$$.tmp";
        my $changes = 0;
 
-       $search = $replace if not defined $search;
+       $search = $replace if @_ < 3;
 
        # generate a single regex to emulate smart match
        $search = [$search] unless ref $search eq 'ARRAY';
-       $search = join('|', map { ref $_ ? $_ : "^\Q$_\E\$" } @$search);
+       @$search = grep { defined } @$search;
+       $search = @$search ? join('|', map { ref $_ ? $_ : "^\Q$_\E\$" } @$search) : undef;
 
        open my $src,  '<', $filename;
        open my $dest, '>', $tmpname;
        while (readline $src) {
                chomp;
-               if ($_ =~ /$search/s) {
+               if (defined $search and $_ =~ /$search/s) {
                        if (ref $replace eq 'ARRAY') {
                                if (@$replace and $_ eq $replace->[0]) {
                                        shift @$replace;
index fca87b70ec16343af7d91853b4065852644ddefc..fd8d6d572b93aa88496fa347fdfe7204392b0b91 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 20;
+use Test::More tests => 21;
 use Test::NoWarnings;
 
 use autodie;
@@ -51,6 +51,7 @@ testfileset('remove empty',  "$/.$/ $/$/",  [undef, ''],        2, ".$/ $/");
 testfileset('remove regex',  $BASICBODY,    [undef, qr/^re/],   2);
 testfileset('remove all',    $BASICBODY,    [undef, qr/./],     4, '');
 
+testfileset('add unconditionally', "$/0$/", ['0', undef],       1, "$/0$/0$/");
 testfileset('add string',     "foo$/",      ['bar'],            1, "foo$/bar$/");
 testfileset('keep string',    "foo$/bar$/", ['foo'],            0, "foo$/bar$/");
 testfileset('replace string', "1$/2$/3$/",  [4, 1],             2, "2$/3$/4$/");
@@ -67,5 +68,5 @@ testfileset('keep order',     $SAMPLE2, [['bar','foo']], 2, "$/and$/bar$/.$/foo$
 testfileset('replace pair',   $SAMPLE2, [['fooo','barr'], ['bar','foo']], 4, "$/and$/.$/fooo$/barr$/");
 testfileset('mixed arrays',   $SAMPLE2, [['bar','foo','.'],['ignore',qr/a/]], 3, "$/foo$/bar$/.$/foo$/.$/");
 
-is(remove_tree($target), 18, 'no unexpected files');
+is(remove_tree($target), 19, 'no unexpected files');