| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IO::Die; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 443 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | #NOTE: This function does not attempt to support every possible way of calling | 
| 6 |  |  |  |  |  |  | #Perl’s open() built-in, but to support the minimal syntax required to do | 
| 7 |  |  |  |  |  |  | #everything that is useful to do with open(), with preference given to those | 
| 8 |  |  |  |  |  |  | #forms that may (somewhat arbitrarily) be considered "better". | 
| 9 |  |  |  |  |  |  | # | 
| 10 |  |  |  |  |  |  | #For example, this function does NOT allow one-arg or two-arg open() except for | 
| 11 |  |  |  |  |  |  | #the more "useful" cases like when MODE is '-|' or '|-'. | 
| 12 |  |  |  |  |  |  | # | 
| 13 |  |  |  |  |  |  | #On the other hand, open($fh, '-') seems harder to understand than its 3-arg | 
| 14 |  |  |  |  |  |  | #equivalent, open($fh, '<&=', STDIN), so that two-arg form is unsupported. | 
| 15 |  |  |  |  |  |  | # | 
| 16 |  |  |  |  |  |  | #Current forms of open() that this supports are: | 
| 17 |  |  |  |  |  |  | #   - any form of 3 or more arguments | 
| 18 |  |  |  |  |  |  | #   - 2-arg when the MODE is '-|' or '|-' | 
| 19 |  |  |  |  |  |  | # | 
| 20 |  |  |  |  |  |  | #NOTE: Bareword file handles DO NOT WORK. (Auto-vivification does, though.) | 
| 21 |  |  |  |  |  |  | # | 
| 22 |  |  |  |  |  |  | sub open { | 
| 23 | 4 |  |  | 4 | 1 | 1875 | my ( $NS, $mode, $expr, @list ) = ( shift, @_[ 1 .. $#_ ] ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | #https://github.com/pjcj/Devel--Cover/issues/125 | 
| 26 |  |  |  |  |  |  | #my ( $NS, $handle_r, $mode, $expr, @list ) = ( shift, \shift, @_ ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 4 | 50 | 33 |  |  | 42 | die "Avoid bareword file handles." if !ref $_[0] && defined $_[0] && length $_[0]; | 
|  |  |  | 33 |  |  |  |  | 
| 29 | 4 | 50 |  |  |  | 11 | die "Avoid one-argument open()." if !$mode; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 4 |  |  |  |  | 20 | local ( $!, $^E ); | 
| 32 | 4 | 50 |  |  |  | 14 | if ( !defined $expr ) { | 
| 33 | 0 | 0 | 0 |  |  | 0 | if ( $mode eq '|-' or $mode eq '-|' ) { | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | #NOTE: Avoid // for compatibility with old Perl versions. | 
| 36 | 0 |  |  |  |  | 0 | my $open = CORE::open( $_[0], $mode ); | 
| 37 | 0 | 0 |  |  |  | 0 | if ( !defined $open ) { | 
| 38 | 0 |  |  |  |  | 0 | $NS->__THROW('Fork'); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 0 |  |  |  |  | 0 | return $open; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 0 |  |  |  |  | 0 | my $file = __FILE__; | 
| 45 | 0 |  |  |  |  | 0 | die "Avoid most forms of two-argument open(). (See $file and its tests for allowable forms.)"; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 4 | 50 |  |  |  | 115 | my $ok = CORE::open( $_[0], $mode, $expr, @list ) or do { | 
| 49 | 0 | 0 | 0 |  |  | 0 | if ( $mode eq '|-' || $mode eq '-|' ) { | 
| 50 | 0 |  |  |  |  | 0 | my $cmd = $expr; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | #If the EXPR (cf. perldoc -f open) has spaces and no LIST | 
| 53 |  |  |  |  |  |  | #is given, then Perl interprets EXPR as a space-delimited | 
| 54 |  |  |  |  |  |  | #shell command, the first component of which is the actual | 
| 55 |  |  |  |  |  |  | #command. | 
| 56 | 0 | 0 |  |  |  | 0 | if ( !@list ) { | 
| 57 | 0 |  |  |  |  | 0 | ($cmd) = ( $cmd =~ m<\A(\S+)> ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  | 0 | $NS->__THROW( 'Exec', path => $cmd, arguments => \@list ); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 | 0 |  |  |  | 0 | if ( 'SCALAR' eq ref $expr ) { | 
| 64 | 0 |  |  |  |  | 0 | $NS->__THROW('ScalarOpen'); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  | 0 | $NS->__THROW( 'FileOpen', mode => $mode, path => $expr ); | 
| 68 |  |  |  |  |  |  | }; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 4 |  |  |  |  | 20 | return $ok; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | 1; |