| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | Chj::BuiltinTypePredicates | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | Predicates that are useful/needed outside of the `FP` namespace | ||||||||||||||||||||||||||||||||
| 19 | tree. Also, to avoid circular dependency on Chj::TEST. | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | L<FP::Predicates> | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head1 NOTE | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||||||
| 28 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | package Chj::BuiltinTypePredicates; | ||||||||||||||||||||||||||||||||
| 33 | 81 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 81 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 81 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 81 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 81 | our @EXPORT = qw(is_filehandle); | |||||||||||||||||||||||||||||||
| 39 | 81 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||||||
| 40 | 81 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 81 | ▶ | use Scalar::Util 'reftype'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | 81 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | # for tests, see FP::Predicates | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 304 | ▶ | sub is_filehandle { | ||||||||||||||||||||||||||||||
|
Subroutine
is_filehandle: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 48 | 304 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 304 | my ($v) = @_; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | # NOTE: never returns true for strings, even though plain strings | ||||||||||||||||||||||||||||||||
| 52 | # naming globals containing filehandles in their IO slot will work | ||||||||||||||||||||||||||||||||
| 53 | # for IO, too! Let's just leave that depreciated and | ||||||||||||||||||||||||||||||||
| 54 | # 'non-working', ok? | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | # NOTE 2: also this only returns true for *references* to globs, | ||||||||||||||||||||||||||||||||
| 57 | # not globs themselves (which could also be used as in | ||||||||||||||||||||||||||||||||
| 58 | # `(*STDOUT)->print( "Huh\n")`). Let's just leave bare globs as | ||||||||||||||||||||||||||||||||
| 59 | # buckets for any of the variable types perl has, and not assume | ||||||||||||||||||||||||||||||||
| 60 | # it's meant to be a filehandle, ok? (Or is that inconsistent with | ||||||||||||||||||||||||||||||||
| 61 | # treating `\*STDOUT` as filehandle? But there's no way around | ||||||||||||||||||||||||||||||||
| 62 | # this one, as that's what `open my $out, ..` gives, and we do | ||||||||||||||||||||||||||||||||
| 63 | # check that the IO slot is actually set in this case.) (hm could | ||||||||||||||||||||||||||||||||
| 64 | # take reference to the bare glob and treat it the same then, | ||||||||||||||||||||||||||||||||
| 65 | # though; but still.) | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 304 | ▶ | if (defined(my $rt = reftype($v))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 295 | ▶ | (($rt eq "GLOB" and *{$v}{IO}) or $rt eq "IO") ? 1 : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$rt eq "GLOB" and *{$v;}{"IO"} or $rt eq "IO"
$rt eq "GLOB" and *{$v;}{"IO"}
MC/DC0%0/2
$rt eq "GLOB" and *{$v;}{"IO"} or $rt eq "IO"
$rt eq "GLOB"
$rt eq "IO"
Truth table66%2/3
A or B
A= $rt eq "GLOB"B= $rt eq "IO"
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | # explicitely return '' instead of undef | ||||||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||||||
| 72 | 9 | '' | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 81 | 1 | |||||||||||||||||||||||||||||||