| 1 | package IO::Die; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 11 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | #not in production | ||||||||||||||||||||
| 7 | #use warnings; | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | IO::Die - Namespaced, error-checked I/O | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | Version 0.057 | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 2 | our $VERSION = '0.057'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 22 | #PROTECTED | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | #Override in subclasses as needed | ||||||||||||||||||||
| 25 | 249 | ▶ | sub _CREATE_ERROR { | ||||||||||||||||||
|
Subroutine
_CREATE_ERROR: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 26 | 249 | shift; | |||||||||||||||||||
| 27 | 249 | ▶ | return shift() . ": " . join( ' ', map { defined() ? $_ : q<> } @_ ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 259 | ▶ | sub _DO_WITH_ERROR { die $_[1] } | ||||||||||||||||||
|
Subroutine
_DO_WITH_ERROR: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 33 | #PRIVATES | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 259 | ▶ | sub __THROW { | ||||||||||||||||||
|
Subroutine
__THROW: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 259 | my ( $NS, $type, @args ) = @_; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 259 | $NS->_DO_WITH_ERROR( | |||||||||||||||||||
| 39 | $NS->_CREATE_ERROR( | ||||||||||||||||||||
| 40 | $type, | ||||||||||||||||||||
| 41 | @args, | ||||||||||||||||||||
| 42 | OS_ERROR => $!, | ||||||||||||||||||||
| 43 | EXTENDED_OS_ERROR => $^E, | ||||||||||||||||||||
| 44 | ) | ||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 79 | ▶ | sub __is_a_fh { | ||||||||||||||||||
|
Subroutine
__is_a_fh: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 49 | 79 | my ($thing) = @_; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 79 | my $is_fh; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | #Every file handle is a GLOB reference. This would be sufficient, except | ||||||||||||||||||||
| 54 | #GLOBs can also be symbol table references. | ||||||||||||||||||||
| 55 | 79 | ▶ | if ( UNIVERSAL::isa( $thing, 'GLOB' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # You can’t tie() a symbol table reference, so if we’re tied(), | ||||||||||||||||||||
| 58 | # then this is a filehandle. | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # If we’re not tied(), then we have to check fileno(). | ||||||||||||||||||||
| 61 | 44 | ▶ | $is_fh = ( tied *{$thing} ) || defined( CORE::fileno($thing) ); | ||||||||||||||||||
|
Condition66%2/3
tied *{$thing;} || defined fileno $thing
MC/DC50%1/2
tied *{$thing;} || defined fileno $thing
tied *{$thing;}
defined fileno $thing
Truth table66%2/3
A || B
A= tied *{$thing;}B= defined fileno $thing
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 79 | return $is_fh; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | our $AUTOLOAD; | |||||||||||||||||||
| 70 | 236 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 71 | 236 | $AUTOLOAD =~ s<.*::><>; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 236 | local ($!, $^E); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 236 | my $reldir = __PACKAGE__; | |||||||||||||||||||
| 76 | 236 | $reldir =~ s<::></>g; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 236 | require "$reldir/$AUTOLOAD.pm"; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 236 | return __PACKAGE__->can($AUTOLOAD)->(@_); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 1 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=15.4
| |||||||||||||||||||||
| 84 | 1 | my ($class, @tags) = @_; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 1 | for my $t (@tags) { | |||||||||||||||||||
| 87 | 0 | ▶ | if ($t eq ':preload') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | $class->_preload(); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | return; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub _preload { | ||||||||||||||||||
|
Subroutine
_preload: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 96 | 0 | my ($class) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | my $path = __FILE__; | |||||||||||||||||||
| 99 | 0 | $path =~ s<\.pm\z><>; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my $reldir = __PACKAGE__; | |||||||||||||||||||
| 102 | 0 | $reldir =~ s<::></>g; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | $class->opendir( my $dh, $path ); | |||||||||||||||||||
| 105 | 0 | local ($!, $^E); | |||||||||||||||||||
| 106 | 0 | ▶ | while ( my $node = readdir $dh ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 0 | ▶ | next if substr( $node, -3 ) ne '.pm'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | require "$reldir/$node"; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | return; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 2 | 1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||