1 package # hide from PAUSE
2 DBIx::Class::Schema::Loader::Utils;
7 use String::CamelCase 'wordsplit';
8 use Carp::Clan qw/^DBIx::Class/;
15 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/;
17 use constant BY_CASE_TRANSITION_V7 =>
18 qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/;
20 use constant BY_NON_ALPHANUM =>
24 my $CRLF = "\x0d\x0a";
29 my $is_camel_case = $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/;
31 if ((not $v) || $v >= 8) {
32 return map split(BY_NON_ALPHANUM, $_), wordsplit($name);
35 return split $is_camel_case ? BY_CASE_TRANSITION_V7 : BY_NON_ALPHANUM, $name;
41 my $dd = Data::Dumper->new([]);
42 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1);
43 return $dd->Values([ $val ])->Dump;
46 sub dumper_squashed($) {
49 my $dd = Data::Dumper->new([]);
50 $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1)->Indent(0);
51 return $dd->Values([ $val ])->Dump;
54 # copied from DBIx::Class::_Util, import from there once it's released
55 sub sigwarn_silencer {
58 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
60 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
62 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
65 # Copied with stylistic adjustments from List::MoreUtils::PP
68 foreach my $i (0..$#_) {
77 grep { not $seen{$_}++ } @_;
82 $action->() foreach my @values = @_;
83 wantarray ? @values : $values[-1];
86 sub eval_package_without_redefine_warnings {
87 my ($pkg, $code) = @_;
89 local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/);
91 # This hairiness is to handle people using "use warnings FATAL => 'all';"
92 # in their custom or external content.
99 if (my ($sym) = $@ =~ /^Subroutine (\S+) redefined/) {
100 delete $INC{ +class_path($pkg) };
101 push @delete_syms, $sym;
103 foreach my $sym (@delete_syms) {
105 undef *{"${pkg}::${sym}"};
120 my $class_path = $class;
121 $class_path =~ s{::}{/}g;
122 $class_path .= '.pm';
127 sub no_warnings(&;$) {
128 my ($code, $test_name) = @_;
132 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
133 local $SIG{__WARN__} = sub {
140 ok ((not $failed), $test_name);
143 sub warnings_exist(&$$) {
144 my ($code, $re, $test_name) = @_;
148 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
149 local $SIG{__WARN__} = sub {
160 ok $matched, $test_name;
163 sub warnings_exist_silent(&$$) {
164 my ($code, $re, $test_name) = @_;
168 local $SIG{__WARN__} = sub { $matched = 1 if $_[0] =~ $re; };
172 ok $matched, $test_name;
176 my $file_name = shift;
178 open my $fh, '<:encoding(UTF-8)', $file_name,
179 or croak "Can't open '$file_name' for reading: $!";
181 my $data = do { local $/; <$fh> };
185 $data =~ s/$CRLF|$LF/\n/g;
191 my $file_name = shift;
193 open my $fh, '>:encoding(UTF-8)', $file_name,
194 or croak "Can't open '$file_name' for writing: $!";
201 no warnings 'uninitialized';
204 return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l;
208 # vim:et sts=4 sw=4 tw=0: