Data-Munge-0.095/0000755000175000017500000000000012523073007012436 5ustar maukemaukeData-Munge-0.095/README0000644000175000017500000000171712477067445013345 0ustar maukemaukeData-Munge Various utility functions. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc Data::Munge You can also look for information at: RT, CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Munge AnnoCPAN, Annotated CPAN documentation http://annocpan.org/dist/Data-Munge CPAN Ratings http://cpanratings.perl.org/d/Data-Munge MetaCPAN https://metacpan.org/module/Data::Munge COPYRIGHT AND LICENCE Copyright (C) 2009-2011, 2013-2015 Lukas Mai This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. Data-Munge-0.095/Changes0000644000175000017500000000253212523072633013737 0ustar maukemaukeRevision history for Data-Munge 0.095 2015-05-08 * Extend documentation for slurp, submatches, eval_string. 0.094 2015-03-08 * elem("$ref", [$ref]) is now false. (elem($ref, ["$ref"]) had been false before.) 0.093 2014-12-23 * Fix typo in synopsis (thanks, Ivan Wills). 0.092 2014-11-25 * Work around some parser bug in perl 5.6. 0.091 2014-11-19 * Work around regex bug (#115242) in perls < 5.18 that causes spurious test failures. 0.09 2014-11-18 * Add slurp. * Don't leave $VERSION and @EXPORT in scope for eval_string. 0.08 2014-09-15 * Make trim(undef) return undef without warnings. 0.07 2013-10-22 * eval_string() now sets __FILE__ and __LINE__ for the eval'd code. This may or may not make error locations more useful. 0.06 2013-03-07 * add elem 0.05 2013-03-05 * add trim, eval_string, and rec 0.04 2011-08-04 * Fix buggy behavior for list2re('') and list2re() 0.032 2010-01-23 * Fix overly strict test that fails on < 5.10 0.031 2010-01-20 * Also remove Defaults::Mauke from tests (sigh). 0.03 2010-01-19 * Dropped dependency on Defaults::Mauke. * Added submatches/replace. 0.02 2009-12-14 First version, released on an unsuspecting world. Data-Munge-0.095/lib/0000755000175000017500000000000012523073007013204 5ustar maukemaukeData-Munge-0.095/lib/Data/0000755000175000017500000000000012523073007014055 5ustar maukemaukeData-Munge-0.095/lib/Data/Munge.pm0000644000175000017500000002414112523072640015472 0ustar maukemaukepackage Data::Munge; use strict; use warnings; use base qw(Exporter); sub _eval { eval $_[0] } # empty lexical scope our $VERSION = '0.095'; our @EXPORT = qw( byval elem eval_string list2re mapval rec replace slurp submatches trim ); sub byval (&$) { my ($f, $x) = @_; local *_ = \$x; $f->($_); $x } sub elem { my ($k, $xs) = @_; if (ref $k) { for my $x (@$xs) { return 1 if ref $x && $k == $x; } } elsif (defined $k) { for my $x (@$xs) { return 1 if defined $x && !ref $x && $k eq $x; } } else { for my $x (@$xs) { return 1 if !defined $x; } } !1 } sub eval_string { my ($code) = @_; my ($package, $file, $line) = caller; $code = qq{package $package; # eval_string()\n#line $line "$file"\n$code}; my @r = wantarray ? _eval $code : scalar _eval $code; die $@ if $@; wantarray ? @r : $r[0] } sub list2re { @_ or return qr/(?!)/; my $re = join '|', map quotemeta, sort {length $b <=> length $a || $a cmp $b } @_; $re eq '' and $re = '(?#)'; qr/$re/ } sub mapval (&@) { my $f = shift; my @xs = @_; map { $f->($_); $_ } @xs } if ($] >= 5.016) { eval_string <<'EOT'; use v5.16; sub rec (&) { my ($f) = @_; sub { $f->(__SUB__, @_) } } EOT } elsif (eval { require Scalar::Util } && defined &Scalar::Util::weaken) { *rec = sub (&) { my ($f) = @_; my $w; my $r = $w = sub { $f->($w, @_) }; Scalar::Util::weaken($w); $r }; } else { # slow but always works *rec = sub (&) { my ($f) = @_; sub { $f->(&rec($f), @_) } }; } sub replace { my ($str, $re, $x, $g) = @_; my $f = ref $x ? $x : sub { my $r = $x; $r =~ s{\$([\$&`'0-9]|\{([0-9]+)\})}{ $+ eq '$' ? '$' : $+ eq '&' ? $_[0] : $+ eq '`' ? substr($_[-1], 0, $_[-2]) : $+ eq "'" ? substr($_[-1], $_[-2] + length $_[0]) : $_[$+] }eg; $r }; if ($g) { $str =~ s{$re}{ $f->(substr($str, $-[0], $+[0] - $-[0]), submatches(), $-[0], $str) }eg; } else { $str =~ s{$re}{ $f->(substr($str, $-[0], $+[0] - $-[0]), submatches(), $-[0], $str) }e; } $str } sub slurp { local $/; scalar readline $_[0] } sub submatches { no strict 'refs'; map $$_, 1 .. $#+ } sub trim { my ($s) = @_; return undef if !defined $s; $s =~ s/^\s+//; $s =~ s/\s+\z//; $s } 'ok' __END__ =head1 NAME Data::Munge - various utility functions =head1 SYNOPSIS use Data::Munge; my $re = list2re qw/f ba foo bar baz/; # $re = qr/bar|baz|foo|ba|f/; print byval { s/foo/bar/ } $text; # print do { my $tmp = $text; $tmp =~ s/foo/bar/; $tmp }; foo(mapval { chomp } @lines); # foo(map { my $tmp = $_; chomp $tmp; $tmp } @lines); print replace('Apples are round, and apples are juicy.', qr/apples/i, 'oranges', 'g'); # "oranges are round, and oranges are juicy." print replace('John Smith', qr/(\w+)\s+(\w+)/, '$2, $1'); # "Smith, John" my $trimmed = trim " a b c "; # "a b c" my $x = 'bar'; if (elem $x, [qw(foo bar baz)]) { ... } my $contents = slurp $fh; # or: slurp *STDIN eval_string('print "hello world\\n"'); # says hello eval_string('die'); # dies eval_string('{'); # throws a syntax error my $fac = rec { my ($rec, $n) = @_; $n < 2 ? 1 : $n * $rec->($n - 1) }; print $fac->(5); # 120 if ("hello, world!" =~ /(\w+), (\w+)/) { my @captured = submatches; # @captured = ("hello", "world") } =head1 DESCRIPTION This module defines a few generally useful utility functions. I got tired of redefining or working around them, so I wrote this module. =head2 Functions =over =item list2re LIST Converts a list of strings to a regex that matches any of the strings. Especially useful in combination with C. Example: my $re = list2re keys %hash; $str =~ s/($re)/$hash{$1}/g; This function takes special care to get several edge cases right: =over =item * Empty list: An empty argument list results in a regex that doesn't match anything. =item * Empty string: An argument list consisting of a single empty string results in a regex that matches the empty string (and nothing else). =item * Prefixes: The input strings are sorted by descending length to ensure longer matches are tried before shorter matches. Otherwise C would generate C, which (on its own) can never match C (because C is tried first, and it always succeeds where C could). =back =item byval BLOCK SCALAR Takes a code block and a value, runs the block with C<$_> set to that value, and returns the final value of C<$_>. The global value of C<$_> is not affected. C<$_> isn't aliased to the input value either, so modifying C<$_> in the block will not affect the passed in value. Example: foo(byval { s/!/?/g } $str); # Calls foo() with the value of $str, but all '!' have been replaced by '?'. # $str itself is not modified. Since perl 5.14 you can also use the C flag: foo($str =~ s/!/?/gr); But C works on all versions of perl and is not limited to C. =item mapval BLOCK LIST Works like a combination of C and C; i.e. it behaves like C, but C<$_> is a copy, not aliased to the current element, and the return value is taken from C<$_> again (it ignores the value returned by the block). Example: my @foo = mapval { chomp } @bar; # @foo contains a copy of @bar where all elements have been chomp'd. # This could also be written as chomp(my @foo = @bar); but that's not # always possible. =item submatches Returns a list of the strings captured by the last successful pattern match. Normally you don't need this function because this is exactly what C returns in list context. However, C also works in other contexts such as the RHS of C. =item replace STRING, REGEX, REPLACEMENT, FLAG =item replace STRING, REGEX, REPLACEMENT A clone of javascript's C. It works almost the same as C, but with a few important differences. REGEX can be a string or a compiled C object. REPLACEMENT can be a string or a subroutine reference. If it's a string, it can contain the following replacement patterns: =over =item $$ Inserts a '$'. =item $& Inserts the matched substring. =item $` Inserts the substring preceding the match. =item $' Inserts the substring following the match. =item $N (where N is a digit) Inserts the substring matched by the Nth capturing group. =item ${N} (where N is one or more digits) Inserts the substring matched by the Nth capturing group. =back Note that these aren't variables; they're character sequences interpreted by C. If REPLACEMENT is a subroutine reference, it's called with the following arguments: First the matched substring (like C<$&> above), then the contents of the capture buffers (as returned by C), then the offset where the pattern matched (like C<$-[0]>, see L), then the STRING. The return value will be inserted in place of the matched substring. Normally only the first occurrence of REGEX is replaced. If FLAG is present, it must be C<'g'> and causes all occurrences to be replaced. =item trim STRING Returns I with all leading and trailing whitespace removed. Like L|perlfunc/length-EXPR> it returns C if the input is C. =item elem SCALAR, ARRAYREF Returns a boolean value telling you whether I is an element of I or not. Two scalars are considered equal if they're both C, if they're both references to the same thing, or if they're both not references and C to each other. This is implemented as a linear search through I that terminates early if a match is found (i.e. C won't even look at elements C<1 .. 9999>). =item eval_string STRING Evals I just like C but doesn't catch exceptions. Caveat: Unlike with C the code runs in an empty lexical scope: my $foo = "Hello, world!\n"; eval_string 'print $foo'; # Dies: Global symbol "$foo" requires explicit package name That is, the eval'd code can't see variables from the scope of the C call. =item slurp FILEHANDLE Reads and returns all remaining data from I as a string, or C if it hits end-of-file. (Interaction with non-blocking filehandles is currently not well defined.) C is equivalent to C. =item rec BLOCK Creates an anonymous sub as C would, but supplies the called sub with an extra argument that can be used to recurse: my $code = rec { my ($rec, $n) = @_; $rec->($n - 1) if $n > 0; print $n, "\n"; }; $code->(4); That is, when the sub is called, an implicit first argument is passed in C<$_[0]> (all normal arguments are moved one up). This first argument is a reference to the sub itself. This reference could be used to recurse directly or to register the sub as a handler in an event system, for example. A note on defining recursive anonymous functions: Doing this right is more complicated than it may at first appear. The most straightforward solution using a lexical variable and a closure leaks memory because it creates a reference cycle. Starting with perl 5.16 there is a C<__SUB__> constant that is equivalent to C<$rec> above, and this is indeed what this module uses (if available). However, this module works even on older perls by falling back to either weak references (if available) or a "fake recursion" scheme that dynamically instantiates a new sub for each call instead of creating a cycle. This last resort is slower than weak references but works everywhere. =back =head1 AUTHOR Lukas Mai, C<< >> =head1 COPYRIGHT & LICENSE Copyright 2009-2011, 2013-2015 Lukas Mai. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. =cut Data-Munge-0.095/META.yml0000644000175000017500000000117312523073007013711 0ustar maukemauke--- abstract: 'various utility functions' author: - 'Lukas Mai ' build_requires: Test::More: '0' Test::Warnings: '0' configure_requires: ExtUtils::MakeMaker: '6.48' strict: '0' warnings: '0' dynamic_config: 0 generated_by: 'ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Data-Munge no_index: directory: - t - inc requires: Exporter: '0' base: '0' strict: '0' warnings: '0' resources: repository: git://github.com/mauke/Data-Munge.git version: '0.095' Data-Munge-0.095/META.json0000644000175000017500000000237712523073007014070 0ustar maukemauke{ "abstract" : "various utility functions", "author" : [ "Lukas Mai " ], "dynamic_config" : 0, "generated_by" : "ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.150001", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Data-Munge", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : {} }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.48", "strict" : "0", "warnings" : "0" } }, "runtime" : { "requires" : { "Exporter" : "0", "base" : "0", "strict" : "0", "warnings" : "0" } }, "test" : { "requires" : { "Test::More" : "0", "Test::Warnings" : "0" } } }, "release_status" : "stable", "resources" : { "repository" : { "type" : "git", "url" : "git://github.com/mauke/Data-Munge.git", "web" : "https://github.com/mauke/Data-Munge" } }, "version" : "0.095" } Data-Munge-0.095/Makefile.PL0000644000175000017500000000374712523072472014430 0ustar maukemaukeuse strict; use warnings; use ExtUtils::MakeMaker; sub merge_key_into { my ($href, $target, $source) = @_; %{$href->{$target}} = (%{$href->{$target}}, %{delete $href->{$source}}); } my %opt = ( NAME => 'Data::Munge', AUTHOR => q{Lukas Mai }, VERSION_FROM => 'lib/Data/Munge.pm', ABSTRACT_FROM => 'lib/Data/Munge.pm', LICENSE => 'perl', PL_FILES => {}, CONFIGURE_REQUIRES => { 'strict' => 0, 'warnings' => 0, 'ExtUtils::MakeMaker' => '6.48', }, BUILD_REQUIRES => {}, TEST_REQUIRES => { 'Test::More' => 0, 'Test::Warnings' => 0, }, PREREQ_PM => { 'Exporter' => 0, 'base' => 0, 'strict' => 0, 'warnings' => 0, }, depend => { Makefile => '$(VERSION_FROM)', }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Data-Munge-*' }, META_MERGE => { 'meta-spec' => { version => 2 }, dynamic_config => 0, resources => { repository => { url => 'git://github.com/mauke/Data-Munge.git', web => 'https://github.com/mauke/Data-Munge', type => 'git', }, }, }, ); (do 'maint/eumm-fixup.pl' || die $@ || $!)->(\%opt) if !-f 'META.yml'; (my $mm_version = ExtUtils::MakeMaker->VERSION($opt{CONFIGURE_REQUIRES}{'ExtUtils::MakeMaker'})) =~ tr/_//d; if ($mm_version < 6.67_04) { # Why? For the glory of satan, of course! no warnings qw(redefine); *ExtUtils::MM_Any::_add_requirements_to_meta_v1_4 = \&ExtUtils::MM_Any::_add_requirements_to_meta_v2; } if ($mm_version < 6.63_03) { merge_key_into \%opt, 'BUILD_REQUIRES', 'TEST_REQUIRES'; } if ($mm_version < 6.55_01) { merge_key_into \%opt, 'CONFIGURE_REQUIRES', 'BUILD_REQUIRES'; } if ($mm_version < 6.51_03) { merge_key_into \%opt, 'PREREQ_PM', 'CONFIGURE_REQUIRES'; } WriteMakefile %opt; Data-Munge-0.095/MANIFEST0000644000175000017500000000042212523073007013565 0ustar maukemaukeChanges MANIFEST MANIFEST.SKIP Makefile.PL README lib/Data/Munge.pm t/00-load.t t/01-compile.t t/torgox.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Data-Munge-0.095/t/0000755000175000017500000000000012523073007012701 5ustar maukemaukeData-Munge-0.095/t/torgox.t0000644000175000017500000000130112405527063014410 0ustar maukemauke#!perl use warnings; use strict; use Test::More tests => 8; use Data::Munge qw(list2re replace); my $orig = '[acabbdcacab]'; my $re1 = list2re 'a', 'b'; my $good1 = '[XYcXYXYXYdcXYcXYXY]'; is replace($orig, $re1, 'XY', 'g'), $good1; $_ = $orig; s/$re1/XY/g; is $_, $good1; my $re2 = list2re 'a'; my $good2 = '[XYcXYbbdcXYcXYb]'; is replace($orig, $re2, 'XY', 'g'), $good2; $_ = $orig; s/$re2/XY/g; is $_, $good2; my $re3 = list2re; my $good3 = $orig; is replace($orig, $re3, 'XY', 'g'), $good3; $_ = $orig; s/$re3/XY/g; is $_, $good3; my $re4 = list2re ''; my $good4 = 'XY[XYaXYcXYaXYbXYbXYdXYcXYaXYcXYaXYbXY]XY'; is replace($orig, $re4, 'XY', 'g'), $good4; $_ = $orig; s/$re4/XY/g; is $_, $good4; Data-Munge-0.095/t/00-load.t0000644000175000017500000000022212405531212014212 0ustar maukemauke#!perl -T use Test::More tests => 1; BEGIN { use_ok( 'Data::Munge' ); } diag( "Testing Data::Munge $Data::Munge::VERSION, Perl $], $^X" ); Data-Munge-0.095/t/01-compile.t0000644000175000017500000000412112477066766014760 0ustar maukemauke#!perl use Test::More tests => 64; use Test::Warnings; use warnings FATAL => 'all'; use strict; use Data::Munge; { my $str = "abc|bar|baz|foo|\\*\\*|ab|\\!|\\*|a"; is list2re(qw[! a abc ab foo bar baz ** *]), qr/$str/, 'list2re'; } is +(byval { s/foo/bar/ } 'foo-foo'), 'bar-foo', 'byval'; is_deeply [mapval { tr[a-d][1-4] } 'foo', 'bar', 'baz'], [qw[foo 21r 21z]], 'mapval'; is replace('Apples are round, and apples are juicy.', qr/apples/i, 'oranges', 'g'), 'oranges are round, and oranges are juicy.', 'replace g'; is replace('John Smith', qr/(\w+)\s+(\w+)/, '$2, $1'), 'Smith, John', 'replace'; is replace('97653 foo bar 42', qr/(\d)(\d)/, sub { $_[1] + $_[2] }, 'g'), '16113 foo bar 6', 'replace fun g'; "foo bar" =~ /(\w+) (\w+)/ or die; is_deeply [submatches], [qw(foo bar)]; "" =~ /^/ or die; is_deeply [submatches], []; is trim(" a b "), "a b"; is trim(""), ""; is trim(","), ","; is trim(" "), ""; is trim(" "), ""; is trim("\na"), "a"; is trim("b\t"), "b"; is trim("X\nY \n "), "X\nY"; is trim(undef), undef; { my $fac = rec { my ($rec, $n) = @_; $n < 2 ? 1 : $n * $rec->($n - 1) }; is $fac->(5), 120; is $fac->(6), 720; } is eval_string('"ab" . "cd"'), 'abcd'; is eval { eval_string('{') }, undef; like $@, qr/Missing right curly/; is eval { eval_string '$VERSION' }, undef; like $@, qr/Global symbol "\$VERSION"/; ok !elem 42, []; ok elem 42, [42]; ok elem "A", [undef, [], "A", "B"]; ok elem "B", [undef, [], "A", "B"]; ok elem undef, [undef, [], "A", "B"]; ok !elem [], [undef, [], "A", "B"]; ok !elem "C", [undef, [], "A", "B"]; for my $ref ([], {}, sub {}) { ok !elem $ref, []; ok !elem $ref, [undef]; ok !elem $ref, ["$ref"]; ok !elem "$ref", [$ref]; ok !elem $ref, [[], {}]; ok elem $ref, [$ref]; ok elem $ref, ["A", "B", $ref]; ok elem $ref, ["A", $ref, "B"]; ok elem $ref, [$ref, "A", $ref, $ref]; ok elem $ref, [undef, $ref]; } my $source = slurp \*DATA; like $source, qr/\AThis is the beginning\.\n/; like $source, qr/\nThis is the end\.\Z/; __DATA__ This is the beginning. stuff etc. This is the end. Data-Munge-0.095/MANIFEST.SKIP0000644000175000017500000000023112523072410014325 0ustar maukemauke\.tar\.gz$ ^Build$ ^Data-Munge- ^GNUmakefile$ ^MANIFEST\.(?!SKIP$) ^MYMETA\. ^Makefile$ ^Makefile\.old$ ^\. ^_build ^blib ^cover_db$ ^pm_to_blib ^maint/