Attempt to fix 'Attempt to free unreferenced scalar' on 5.8
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Utils.pm
CommitLineData
cc4f11a2 1package # hide from PAUSE
2 DBIx::Class::Schema::Loader::Utils;
3
4use strict;
5use warnings;
1ad8e8c3 6use Test::More;
ea3b8f03 7use String::CamelCase 'wordsplit';
112415f1 8use Carp::Clan qw/^DBIx::Class/;
d3a09821 9use List::Util 'all';
ea3b8f03 10use Data::Dumper ();
dbe5c904 11use base 'Exporter';
12use namespace::clean;
13
cc4f11a2 14
ecf22f0a 15our @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/;
cc4f11a2 16
ea3b8f03 17use constant BY_CASE_TRANSITION_V7 =>
cc4f11a2 18 qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/;
19
20use constant BY_NON_ALPHANUM =>
21 qr/[\W_]+/;
22
fcf328c7 23my $LF = "\x0a";
24my $CRLF = "\x0d\x0a";
25
ea3b8f03 26sub split_name($;$) {
27 my ($name, $v) = @_;
28
29 my $is_camel_case = $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/;
30
31 if ((not $v) || $v >= 8) {
32 return map split(BY_NON_ALPHANUM, $_), wordsplit($name);
33 }
cc4f11a2 34
ea3b8f03 35 return split $is_camel_case ? BY_CASE_TRANSITION_V7 : BY_NON_ALPHANUM, $name;
cc4f11a2 36}
37
15efd63a 38sub dumper($) {
39 my $val = shift;
40
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;
44}
45
46sub dumper_squashed($) {
47 my $val = shift;
48
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;
52}
53
cfaae7fc 54# copied from DBIx::Class::_Util, import from there once it's released
55sub sigwarn_silencer {
83bce685 56 my $pattern = shift;
cfaae7fc 57
83bce685 58 croak "Expecting a regexp" if ref $pattern ne 'Regexp';
cfaae7fc 59
83bce685 60 my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
cfaae7fc 61
83bce685 62 return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
cfaae7fc 63}
64
ecf22f0a 65# Copied with stylistic adjustments from List::MoreUtils::PP
66sub firstidx (&@) {
67 my $f = shift;
68 foreach my $i (0..$#_) {
69 local *_ = \$_[$i];
70 return $i if $f->();
71 }
72 return -1;
73}
74
75sub uniq (@) {
76 my %seen = ();
77 grep { not $seen{$_}++ } @_;
78}
79
80sub apply (&@) {
81 my $action = shift;
82 $action->() foreach my @values = @_;
83 wantarray ? @values : $values[-1];
84}
85
0f21885a 86sub eval_package_without_redefine_warnings {
87 my ($pkg, $code) = @_;
c38ec663 88
cfaae7fc 89 local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/);
0f21885a 90
91 # This hairiness is to handle people using "use warnings FATAL => 'all';"
92 # in their custom or external content.
93 my @delete_syms;
94 my $try_again = 1;
95
96 while ($try_again) {
97 eval $code;
98
99 if (my ($sym) = $@ =~ /^Subroutine (\S+) redefined/) {
100 delete $INC{ +class_path($pkg) };
101 push @delete_syms, $sym;
102
103 foreach my $sym (@delete_syms) {
104 no strict 'refs';
105 undef *{"${pkg}::${sym}"};
106 }
107 }
108 elsif ($@) {
109 die $@ if $@;
110 }
111 else {
112 $try_again = 0;
113 }
114 }
115}
116
117sub class_path {
118 my $class = shift;
119
120 my $class_path = $class;
121 $class_path =~ s{::}{/}g;
122 $class_path .= '.pm';
123
124 return $class_path;
c38ec663 125}
126
12b86f07 127sub no_warnings(&;$) {
128 my ($code, $test_name) = @_;
129
130 my $failed = 0;
131
132 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
133 local $SIG{__WARN__} = sub {
134 $failed = 1;
135 $warn_handler->(@_);
136 };
137
138 $code->();
139
140 ok ((not $failed), $test_name);
141}
142
1ad8e8c3 143sub warnings_exist(&$$) {
144 my ($code, $re, $test_name) = @_;
145
146 my $matched = 0;
147
148 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
149 local $SIG{__WARN__} = sub {
150 if ($_[0] =~ $re) {
151 $matched = 1;
152 }
153 else {
154 $warn_handler->(@_)
155 }
156 };
157
158 $code->();
159
160 ok $matched, $test_name;
161}
162
163sub warnings_exist_silent(&$$) {
164 my ($code, $re, $test_name) = @_;
165
166 my $matched = 0;
167
168 local $SIG{__WARN__} = sub { $matched = 1 if $_[0] =~ $re; };
169
170 $code->();
171
172 ok $matched, $test_name;
173}
174
fcf328c7 175sub slurp_file($) {
112415f1 176 my $file_name = shift;
177
178 open my $fh, '<:encoding(UTF-8)', $file_name,
179 or croak "Can't open '$file_name' for reading: $!";
180
a79e1189 181 my $data = do { local $/; <$fh> };
112415f1 182
a79e1189 183 close $fh;
fcf328c7 184
185 $data =~ s/$CRLF|$LF/\n/g;
186
187 return $data;
188}
1ad8e8c3 189
b564fc4b 190sub write_file($$) {
112415f1 191 my $file_name = shift;
192
193 open my $fh, '>:encoding(UTF-8)', $file_name,
194 or croak "Can't open '$file_name' for writing: $!";
195
b564fc4b 196 print $fh shift;
197 close $fh;
198}
199
50b95db6 200sub array_eq($$) {
201 no warnings 'uninitialized';
d3a09821 202 my ($l, $r) = @_;
50b95db6 203
d3a09821 204 return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l;
50b95db6 205}
206
cc4f11a2 2071;
208# vim:et sts=4 sw=4 tw=0: