1 package # hide from PAUSE
2 DBIx::Class::Schema::Loader::Utils;
7 use Carp::Clan qw/^DBIx::Class/;
10 use Exporter 'import';
13 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/;
15 use constant BY_CASE_TRANSITION_V7 =>
16 qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/;
18 use constant BY_NON_ALPHANUM =>
22 my $CRLF = "\x0d\x0a";
24 # Copied from String::CamelCase because of RT#123030
27 split /[_\s]+|\b|(?<![A-Z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/, $s;
33 my $is_camel_case = $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/;
35 if ((not $v) || $v >= 8) {
36 return map split(BY_NON_ALPHANUM, $_), wordsplit($name);
39 return split $is_camel_case ? BY_CASE_TRANSITION_V7 : BY_NON_ALPHANUM, $name;
45 my $dd = Data::Dumper->new([]);
46 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1);
47 return $dd->Values([ $val ])->Dump;
50 sub dumper_squashed($) {
53 my $dd = Data::Dumper->new([]);
54 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1)->Indent(0);
55 return $dd->Values([ $val ])->Dump;
58 # copied from DBIx::Class::_Util, import from there once it's released
59 sub sigwarn_silencer {
62 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
64 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
66 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
69 # Copied with stylistic adjustments from List::MoreUtils::PP
72 foreach my $i (0..$#_) {
81 grep { not $seen{$_}++ } @_;
86 $action->() foreach my @values = @_;
87 wantarray ? @values : $values[-1];
90 sub eval_package_without_redefine_warnings {
91 my ($pkg, $code) = @_;
93 local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/);
95 # This hairiness is to handle people using "use warnings FATAL => 'all';"
96 # in their custom or external content.
103 if (my ($sym) = $@ =~ /^Subroutine (\S+) redefined/) {
104 delete $INC{ +class_path($pkg) };
105 push @delete_syms, $sym;
107 foreach my $sym (@delete_syms) {
109 undef *{"${pkg}::${sym}"};
124 my $class_path = $class;
125 $class_path =~ s{::}{/}g;
126 $class_path .= '.pm';
131 sub no_warnings(&;$) {
132 my ($code, $test_name) = @_;
136 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
137 local $SIG{__WARN__} = sub {
144 ok ((not $failed), $test_name);
147 sub warnings_exist(&$$) {
148 my ($code, $re, $test_name) = @_;
152 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
153 local $SIG{__WARN__} = sub {
164 ok $matched, $test_name;
167 sub warnings_exist_silent(&$$) {
168 my ($code, $re, $test_name) = @_;
172 local $SIG{__WARN__} = sub { $matched = 1 if $_[0] =~ $re; };
176 ok $matched, $test_name;
180 my $file_name = shift;
182 open my $fh, '<:encoding(UTF-8)', $file_name,
183 or croak "Can't open '$file_name' for reading: $!";
185 my $data = do { local $/; <$fh> };
189 $data =~ s/$CRLF|$LF/\n/g;
195 my $file_name = shift;
197 open my $fh, '>:encoding(UTF-8)', $file_name,
198 or croak "Can't open '$file_name' for writing: $!";
205 no warnings 'uninitialized';
208 return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l;
212 # vim:et sts=4 sw=4 tw=0: