Centralize handling of refcount in DBIC::_Util
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / _Util.pm
index cf7443f..0afe2ea 100644 (file)
@@ -53,7 +53,7 @@ use Carp;
 use Scalar::Util qw(refaddr weaken);
 
 use base 'Exporter';
-our @EXPORT_OK = qw(sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray);
+our @EXPORT_OK = qw(sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray refcount);
 
 sub sigwarn_silencer {
   my $pattern = shift;
@@ -65,6 +65,15 @@ sub sigwarn_silencer {
   return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
 }
 
+sub refcount {
+  croak "Expecting a reference" if ! length ref $_[0];
+
+  require B;
+  # No tempvars - must operate on $_[0], otherwise the pad
+  # will count as an extra ref
+  B::svref_2object($_[0])->REFCNT;
+}
+
 sub modver_gt_or_eq {
   my ($mod, $ver) = @_;