openhandle
[gitmo/Mouse.git] / lib / Mouse / Util.pm
index 5b34083..51aa3f9 100644 (file)
@@ -69,6 +69,23 @@ our %dependencies = (
             }
             : $t
         },
+        'openhandle' => sub {
+            my $fh = shift;
+            my $rt = reftype($fh) || '';
+
+            return defined(fileno($fh)) ? $fh : undef
+                if $rt eq 'IO';
+
+            if (reftype(\$fh) eq 'GLOB') { # handle  openhandle(*DATA)
+                $fh = \(my $tmp=$fh);
+            }
+            elsif ($rt ne 'GLOB') {
+                return undef;
+            }
+
+            (tied(*$fh) or defined(fileno($fh)))
+                ? $fh : undef;
+        },
     },
     'MRO::Compat' => {
         'get_linear_isa' => {