From: Mischa POSLAWSKY Date: Fri, 20 Nov 2009 18:31:44 +0000 (+0100) Subject: fileset: reverse search/replace parameters X-Git-Tag: v1.00~2 X-Git-Url: http://git.shiar.net/perl/file-rewrite.git/commitdiff_plain/2c5f91d126b25f072566844ec4657d8521c4b337 fileset: reverse search/replace parameters Much more readable/logical to have search before replace. --- diff --git a/lib/File/Rewrite.pm b/lib/File/Rewrite.pm index b3bf3d7..90c30f4 100644 --- a/lib/File/Rewrite.pm +++ b/lib/File/Rewrite.pm @@ -9,11 +9,11 @@ use Exporter 'import'; our @EXPORT_OK = qw(fileset); sub fileset { - my ($filename, $replace, $search) = @_; + my ($filename, $search, $replace) = @_; my $tmpname = "$filename.$$.tmp"; my $changes = 0; - $search = $replace if @_ < 3; + $replace = $search if @_ < 3; # generate a single regex to emulate smart match $search = [$search] unless ref $search eq 'ARRAY'; @@ -71,7 +71,7 @@ File::Rewrite =head1 SYNOPSIS use File::Rewrite 'fileset'; - fileset('/tmp/somefile', undef, qr/match/); + fileset('/tmp/somefile', qr/match/ => undef); =head1 DESCRIPTION diff --git a/t/10-fileset.t b/t/10-fileset.t index c405a9b..bf859de 100644 --- a/t/10-fileset.t +++ b/t/10-fileset.t @@ -47,28 +47,28 @@ sub testfileset { my $BASICBODY = "replace$/noremove$/remove$/.$/"; -testfileset('no match', $BASICBODY, [undef, 'nomatch'], 0, $BASICBODY); -testfileset('remove string', $BASICBODY, [undef, 'remove'], 1, "replace$/noremove$/.$/"); -testfileset('remove empty', "$/.$/ $/$/", [undef, ''], 2, ".$/ $/"); -testfileset('remove regex', $BASICBODY, [undef, qr/^re/], 2); -testfileset('remove all', $BASICBODY, [undef, qr/./], 4, ''); +testfileset('no match', $BASICBODY, ['nomatch' => undef], 0, $BASICBODY); +testfileset('remove string', $BASICBODY, ['remove' => undef], 1, "replace$/noremove$/.$/"); +testfileset('remove empty', "$/.$/ $/$/", ['' => undef], 2, ".$/ $/"); +testfileset('remove regex', $BASICBODY, [qr/^re/ => undef], 2); +testfileset('remove all', $BASICBODY, [qr/./ => undef], 4, ''); -testfileset('add unconditionally', "$/0$/", ['0', undef], 1, "$/0$/0$/"); +testfileset('add unconditionally', "$/0$/", [undef() => '0'], 1, "$/0$/0$/"); testfileset('add string', "foo$/", ['bar'], 1, "foo$/bar$/"); testfileset('keep string', "foo$/bar$/", ['foo'], 0, "foo$/bar$/"); testfileset('keep first', "foo$/FOO$/foo$/", ['foo'], 1, "foo$/FOO$/"); -testfileset('replace string', "$/0$/1$/2$/", [3, 0], 2, "$/1$/2$/3$/"); -testfileset('keep regex', "foo$/hi$/$/", ['hi', qr/./], 1, "hi$/$/"); +testfileset('replace string', "$/0$/1$/2$/", [0 => 3], 2, "$/1$/2$/3$/"); +testfileset('keep regex', "foo$/hi$/$/", [qr/./ => 'hi'], 1, "hi$/$/"); -testfileset('add new array', ".$/", [['foo','.'], ''], 2, ".$/foo$/.$/"); -testfileset('replace by array', "$/foo$/.$/", [['foo',''], ''], 3, "foo$/.$/foo$/$/"); -testfileset('partial add', "foo$/$/", [['foo','.'], 'foo'], 1, "foo$/$/.$/"); +testfileset('add new array', ".$/", ['' => ['foo','.']], 2, ".$/foo$/.$/"); +testfileset('replace by array', "$/foo$/.$/", ['' => ['foo','']], 3, "foo$/.$/foo$/$/"); +testfileset('partial add', "foo$/$/", ['foo' => ['foo','.']], 1, "foo$/$/.$/"); my $SAMPLE2 = "$/foo$/and$/bar$/.$/"; testfileset('keep pair', $SAMPLE2, [['foo','bar']], 0, "$/foo$/and$/bar$/.$/"); 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$/.$/"); +testfileset('replace pair', $SAMPLE2, [['bar','foo'] => ['fooo','barr']], 4, "$/and$/.$/fooo$/barr$/"); +testfileset('mixed arrays', $SAMPLE2, [['ignore',qr/a/] => ['bar','foo','.']], 3, "$/foo$/bar$/.$/foo$/.$/"); is(remove_tree($target), 19, 'no unexpected files');