X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FUtils.pm;h=66a92c739938ba3c9292fe5da92b2424cca7e962;hb=4a0dee31ab22653d1249298776935d7be8e66f3b;hp=77a82598f2d4f53daafbbfcb1067b7a4723c4617;hpb=112415f1a0c30d7fb77412b91da7890e54b43393;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/Utils.pm b/lib/DBIx/Class/Schema/Loader/Utils.pm index 77a8259..66a92c7 100644 --- a/lib/DBIx/Class/Schema/Loader/Utils.pm +++ b/lib/DBIx/Class/Schema/Loader/Utils.pm @@ -4,13 +4,13 @@ package # hide from PAUSE use strict; use warnings; use Test::More; -use String::CamelCase 'wordsplit'; use Carp::Clan qw/^DBIx::Class/; +use List::Util 'all'; use namespace::clean; use Exporter 'import'; use Data::Dumper (); -our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file write_file/; +our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file write_file array_eq sigwarn_silencer apply firstidx uniq/; use constant BY_CASE_TRANSITION_V7 => qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/; @@ -21,6 +21,12 @@ use constant BY_NON_ALPHANUM => my $LF = "\x0a"; my $CRLF = "\x0d\x0a"; +# Copied from String::CamelCase because of RT#123030 +sub wordsplit { + my $s = shift; + split /[_\s]+|\b|(?Values([ $val ])->Dump; } +# copied from DBIx::Class::_Util, import from there once it's released +sub sigwarn_silencer { + my $pattern = shift; + + croak "Expecting a regexp" if ref $pattern ne 'Regexp'; + + my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) }; + + return sub { &$orig_sig_warn unless $_[0] =~ $pattern }; +} + +# Copied with stylistic adjustments from List::MoreUtils::PP +sub firstidx (&@) { + my $f = shift; + foreach my $i (0..$#_) { + local *_ = \$_[$i]; + return $i if $f->(); + } + return -1; +} + +sub uniq (@) { + my %seen = (); + grep { not $seen{$_}++ } @_; +} + +sub apply (&@) { + my $action = shift; + $action->() foreach my @values = @_; + wantarray ? @values : $values[-1]; +} + sub eval_package_without_redefine_warnings { my ($pkg, $code) = @_; - my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; - - local $SIG{__WARN__} = sub { - $warn_handler->(@_) - unless $_[0] =~ /^Subroutine \S+ redefined/; - }; + local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/); # This hairiness is to handle people using "use warnings FATAL => 'all';" # in their custom or external content. @@ -168,5 +201,12 @@ sub write_file($$) { close $fh; } +sub array_eq($$) { + no warnings 'uninitialized'; + my ($l, $r) = @_; + + return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l; +} + 1; # vim:et sts=4 sw=4 tw=0: