File Coverage

lib/POSIX/1003/Module.pm
Criterion Covered Total %
statement 82 99 82.8
branch 25 42 59.5
condition 16 30 53.3
subroutine 16 19 84.2
pod 1 1 100.0
total 140 191 73.3


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution POSIX-1003. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package POSIX::1003::Module;
10 24     24   8009 use vars '$VERSION';
  24         40  
  24         1204  
11             $VERSION = '1.02';
12              
13              
14 24     24   126 use strict;
  24         43  
  24         541  
15 24     24   116 use warnings;
  24         39  
  24         1057  
16              
17             # The VERSION of the distribution is sourced from this file, because
18             # this module also loads the XS extension. Therefore, Makefile.PL
19             # extracts the version from the line below.
20             our $VERSION = '1.02';
21 24     24   141 use Carp 'croak';
  24         37  
  24         1309  
22              
23             # some other modules used by the program which uses POSIX::1003 may
24             # need POSIX.xs via POSIX.
25 24     24   12111 use POSIX ();
  24         153254  
  24         687  
26              
27 24     24   176 use XSLoader;
  24         39  
  24         1404  
28             XSLoader::load 'POSIX::1003', $VERSION;
29              
30              
31             sub import(@)
32 78     78   1670 { my $class = shift;
33 78 50       323 return if $class eq __PACKAGE__;
34              
35 24     24   132 no strict 'refs';
  24         38  
  24         736  
36 24     24   117 no warnings 'once';
  24         36  
  24         16060  
37              
38 78 50       118 my $tags = \%{"${class}::EXPORT_TAGS"} or die;
  78         416  
39              
40             # A hash-lookup is faster than an array lookup, so %EXPORT_OK
41 78         143 %{"${class}::EXPORT_OK"} = ();
  78         432  
42 78         100 my $ok = \%{"${class}::EXPORT_OK"};
  78         234  
43 78 50       234 unless(keys %$ok)
44 78         256 { @{$ok}{@{$tags->{$_}}} = () for keys %$tags;
  331         1719  
  331         529  
45             }
46              
47 78 100 100     537 my $level = @_ && $_[0] =~ m/^\+(\d+)$/ ? shift : 0;
48 78 100 100     343 return if @_==1 && $_[0] eq ':none';
49 57 100       127 @_ = ':all' if !@_;
50              
51 57         77 my %take;
52 57         101 foreach (@_)
53 99 100       271 { if( $_ eq ':all')
    100          
54 12         317 { @take{keys %$ok} = ();
55             }
56             elsif( m/^:(.*)/ )
57 3 50       16 { my $tag = $tags->{$1} or croak "$class does not export $_";
58 3         42 @take{@$tag} = ();
59             }
60             else
61 84 50 33     1080 { is_missing($_) or exists $ok->{$_}
62             or croak "$class does not export $_";
63 84         212 undef $take{$_};
64             }
65             }
66              
67 57   50     82 my $in_core = \@{$class.'::IN_CORE'} || [];
68              
69 57         400 my $pkg = (caller $level)[0];
70 57         650 foreach my $f (sort keys %take)
71 778         918 { my $export;
72 778 100 100     711 if(exists ${$class.'::'}{$f} && ($export = *{"${class}::$f"}{CODE}))
  778 100 66     5850  
  197 50 33     844  
    100 66        
    100 33        
    50          
73             { # reuse the already created function; might also be a function
74             # which is actually implemented in the $class namespace.
75             }
76             elsif( is_missing($f)
77             || (exists $ok->{$f} && $f =~ /^[A-Z_][A-Za-z0-9_]*$/))
78 522         1190 { *{$class.'::'.$f} = $export = $class->_create_constant($f);
  522         2189  
79             }
80             elsif( $f !~ m/[^A-Z0-9_]/ ) # faster than: $f =~ m!^[A-Z0-9_]+$!
81             { # other all-caps names are always from POSIX.xs
82             #XXX MO: there should be any external names left
83             #warn "$f from POSIX.pm";
84 0 0 0     0 if(exists $POSIX::{$f} && defined *{"POSIX::$f"}{CODE})
  0         0  
85             { # POSIX.xs croaks on undefined constants, we will return undef
86 0         0 my $const = eval "POSIX::$f()";
87 0         0 *{$class.'::'.$f} = $export
88 0 0   0   0 = defined $const ? sub() {$const} : sub() {undef};
  0         0  
89             }
90             else
91             { # ignore the missing value
92             # warn "missing constant in POSIX.pm $f" && next;
93 0         0 *{$class.'::'.$f} = $export = sub() {undef};
  0         0  
94             }
95             }
96             elsif($f =~ s/^%//)
97 22         56 { $export = \%{"${class}::$f"};
  22         134  
98             }
99             elsif($in_core && grep $f eq $_, @$in_core)
100             { # function is in core, simply ignore the export
101 35         2988 next;
102             }
103 28         122 elsif(exists $POSIX::{$f} && defined *{"POSIX::$f"}{CODE})
104             { # normal functions implemented in POSIX.xs
105 28         47 *{"${class}::$f"} = $export = *{"POSIX::$f"}{CODE};
  28         120  
  28         62  
106             }
107             else
108 0         0 { croak "unable to load $f from $class";
109             }
110              
111 24     24   203 no warnings 'once';
  24         46  
  24         3804  
112 743         1029 *{"${pkg}::$f"} = $export;
  743         34249  
113             }
114             }
115              
116              
117             sub exampleValue($)
118 0     0 1 0 { my ($pkg, $name) = @_;
119 24     24   222 no strict 'refs';
  24         68  
  24         3426  
120              
121 0 0       0 my $tags = \%{"$pkg\::EXPORT_TAGS"} or die;
  0         0  
122 0   0     0 my $constants = $tags->{constants} || [];
123 0 0       0 grep $_ eq $name, @$constants
124             or return undef;
125              
126 0         0 my $val = $pkg->_create_constant($name)->();
127 0 0       0 defined $val ? $val : 'undef';
128             }
129              
130             package POSIX::1003::ReadOnlyTable;
131 24     24   172 use vars '$VERSION';
  24         40  
  24         4971  
132             $VERSION = '1.02';
133              
134 88     88   21700 sub TIEHASH($) { bless $_[1], $_[0] }
135 633     633   11325 sub FETCH($) { $_[0]->{$_[1]} }
136 0     0   0 sub EXISTS($) { exists $_[0]->{$_[1]} }
137 8     8   345 sub FIRSTKEY() { scalar %{$_[0]}; scalar each %{$_[0]} }
  8         22  
  8         30  
  8         82  
138 60     60   60 sub NEXTKEY() { scalar each %{$_[0]} }
  60         149  
139              
140             1;