File Coverage

lib/POSIX/1003.pm
Criterion Covered Total %
statement 81 113 71.6
branch 25 42 59.5
condition 3 6 50.0
subroutine 13 16 81.2
pod 3 3 100.0
total 125 180 69.4


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             package POSIX::1003;
9 2     2   73160 use vars '$VERSION';
  2         14  
  2         114  
10             $VERSION = '1.02';
11              
12              
13 2     2   11 use warnings;
  2         4  
  2         49  
14 2     2   8 use strict;
  2         3  
  2         44  
15              
16 2     2   9 use Carp qw/croak/;
  2         2  
  2         99  
17 2     2   383 use POSIX::1003::Module (); # preload
  2         4  
  2         979  
18              
19             my %own_functions = map +($_ => 1), qw/
20             posix_1003_modules
21             posix_1003_names
22             show_posix_names
23             /;
24              
25             our (%EXPORT_TAGS, %IMPORT_FROM, %SUBSET);
26              
27              
28             my %tags =
29             ( confstr => 'POSIX::1003::Confstr'
30             , cs => 'POSIX::1003::Confstr'
31             , errno => 'POSIX::1003::Errno'
32             , errors => 'POSIX::1003::Errno'
33             , events => 'POSIX::1003::Events'
34             , ev => 'POSIX::1003::Events'
35             , fcntl => 'POSIX::1003::Fcntl'
36             , fdio => 'POSIX::1003::FdIO'
37             , fd => 'POSIX::1003::FdIO'
38             , filesystem => 'POSIX::1003::FS'
39             , fs => 'POSIX::1003::FS'
40             , glob => 'POSIX::1003::FS'
41             , limit => 'POSIX::1003::Limit'
42             , limits => 'POSIX::1003::Limit'
43             , locale => 'POSIX::1003::Locale'
44             , math => 'POSIX::1003::Math'
45             , os => 'POSIX::1003::OS'
46             , opsys => 'POSIX::1003::OS'
47             , pathconf => 'POSIX::1003::Pathconf'
48             , pc => 'POSIX::1003::Pathconf'
49             , processes => 'POSIX::1003::Proc'
50             , proc => 'POSIX::1003::Proc'
51             , properties => 'POSIX::1003::Properties'
52             , property => 'POSIX::1003::Properties'
53             , props => 'POSIX::1003::Properties'
54             , posix => 'POSIX::1003::Properties'
55             , sc => 'POSIX::1003::Sysconf'
56             , sigaction => 'POSIX::SigAction'
57             , signals => [qw/POSIX::1003::Signals POSIX::SigSet POSIX::SigAction/]
58             , sigset => 'POSIX::SigSet'
59             , socket => 'POSIX::1003::Socket'
60             , sysconf => 'POSIX::1003::Sysconf'
61             , termio => 'POSIX::1003::Termios'
62             , termios => 'POSIX::1003::Termios'
63             , time => 'POSIX::1003::Time'
64             , user => 'POSIX::1003::User'
65             );
66              
67             my %mod_tag;
68             while(my ($tag, $pkg) = each %tags)
69             { $pkg = $pkg->[0] if ref $pkg eq 'ARRAY';
70             $mod_tag{$pkg} = $tag
71             if !$mod_tag{$pkg}
72             || length $mod_tag{$pkg} < length $tag;
73             }
74              
75             { eval "require POSIX::1003::Symbols";
76             die $@ if $@;
77             }
78              
79             while(my ($pkg, $tag) = each %mod_tag) # unique modules
80             { $IMPORT_FROM{$_} = $tag for @{$EXPORT_TAGS{$tag}};
81             }
82              
83             sub _tag2mods($)
84 4     4   8 { my $tag = shift;
85 4 50       12 my $r = $tags{$tag} or croak "unknown tag '$tag'";
86 4 50       18 ref $r eq 'ARRAY' ? @$r : $r;
87             }
88              
89 0     0   0 sub _mod2tag($) { $mod_tag{$_[0]} }
90 0     0   0 sub _tags() { keys %tags}
91              
92             sub import(@)
93 4     4   39 { my $class = shift;
94 4         6 my (%mods, %modset, %from);
95              
96 4 50 33     23 my $level = @_ && $_[0] =~ /^\+(\d+)$/ ? shift : 0;
97 4 100 66     59 return if @_==1 && $_[0] eq ':none';
98 3 50       8 @_ = ':all' if !@_;
99              
100 2     2   15 no strict 'refs';
  2         4  
  2         72  
101 2     2   10 no warnings 'once';
  2         4  
  2         1672  
102 3         18 my $to = (caller $level)[0];
103              
104 3         9 foreach (@_)
105 3 50       13 { if($_ eq ':all')
    100          
    100          
106 0         0 { $mods{$_}++ for values %mod_tag;
107 0         0 *{$to.'::'.$_} = \&$_ for keys %own_functions;
  0         0  
108             }
109             elsif(m/^\:(.*)/)
110 1 50       5 { if(exists $tags{$1})
    0          
111             { # module by longest alias
112 1         2 $mods{$_}++ for map $mod_tag{$_}, _tag2mods $1;
113             }
114             elsif(my $subset = $SUBSET{$1})
115 0         0 { push @{$modset{$subset}}, $1;
  0         0  
116             }
117 0         0 else { croak "unknown tag '$_'" };
118             }
119             elsif($own_functions{$_})
120 1         3 { *{$to.'::'.$_} = \&$_;
  1         6  
121             }
122             else
123 1 50       4 { my $mod = $IMPORT_FROM{$_} or croak "unknown symbol '$_'";
124 1         2 push @{$from{$mod}}, $_;
  1         3  
125             }
126             }
127              
128             # no need for separate symbols when we need all
129 3         8 delete $from{$_} for keys %mods;
130              
131             #print "from $_ all\n" for keys %mods;
132             #print "from $_ @{$from{$_}}\n" for keys %from;
133              
134 3         7 my $up = '+' . ($level+1);
135 3         6 foreach my $tag (keys %mods) # whole tags
136 1         2 { delete $modset{$tag};
137 1         1 delete $from{$tag};
138 1         2 foreach my $pkg (_tag2mods($tag))
139 1 50       71 { eval "require $pkg"; die $@ if $@;
  1         5  
140 1         8 $pkg->import($up, ':all');
141             }
142             }
143 3         5 foreach my $tag (keys %modset)
144 0         0 { foreach my $pkg (_tag2mods($tag))
145 0 0       0 { eval "require $pkg"; die $@ if $@;
  0         0  
146 0         0 my @subsets = @{$modset{$tag}};
  0         0  
147 0         0 my $et = \%{"$pkg\::EXPORT_TAGS"};
  0         0  
148 0         0 $pkg->import($up, @{$et->{$_}})
149 0         0 for @subsets;
150             }
151             }
152 3         1735 foreach my $tag (keys %from) # separate symbols
153 1         2 { foreach my $pkg (_tag2mods($tag))
154 1 50       61 { eval "require $pkg"; die $@ if $@;
  1         6  
155 1         2 $pkg->import($up, @{$from{$tag}});
  1         7  
156             }
157             }
158             }
159              
160              
161             sub posix_1003_modules()
162 1     1 1 2 { my %mods;
163 1         7 foreach my $mods (values %tags)
164 36 100       82 { $mods{$_}++ for ref $mods eq 'ARRAY' ? @$mods : $mods;
165             }
166 1         9 keys %mods;
167             }
168              
169              
170             sub posix_1003_names(@)
171 3     3 1 1836 { my %names;
172             my @modules;
173 3 100       14 if(@_)
174 2         4 { my %mods;
175 2         3 foreach my $sel (@_)
176 2 100       15 { $mods{$_}++ for $sel =~ m/^:(\w+)/ ? _tag2mods($1) : $sel;
177             }
178 2         8 @modules = keys %mods;
179             }
180             else
181 1         4 { @modules = posix_1003_modules;
182             }
183              
184 3         7 foreach my $pkg (@modules)
185 23         1193 { eval "require $pkg";
186 23 50       105 $@ && next; # die?
187 23 100       236 $pkg->can('import') or next;
188 21         71 $pkg->import(':none'); # create %EXPORT_OK
189              
190 2     2   15 no strict 'refs';
  2         3  
  2         372  
191 21         27 my $exports = \%{"${pkg}::EXPORT_OK"};
  21         53  
192 21         1000 $names{$_} = $pkg for keys %$exports;
193             }
194              
195 3 50       464 wantarray ? keys %names : \%names;
196             }
197              
198              
199             sub show_posix_names(@)
200 0     0 1   { my $pkg_of = posix_1003_names @_;
201 0           my %order = map {(my $n = lc $_) =~ s/[^A-Za-z0-9]//g; ($n => $_)}
  0            
  0            
202             keys %$pkg_of; # Swartzian transform
203              
204 2     2   15 no strict 'refs';
  2         3  
  2         467  
205 0           foreach (sort keys %order)
206 0           { my $name = $order{$_};
207 0           my $pkg = $pkg_of->{$name};
208 0           $pkg->import($name);
209 0           my $val = $pkg->exampleValue($name);
210 0           (my $abbrev = $pkg) =~ s/^POSIX\:\:1003\:\:/::/;
211 0           my $mod = $mod_tag{$pkg};
212 0 0         if(defined $val)
213 0           { printf "%-12s :%-10s %-30s %s\n", $abbrev, $mod, $name, $val;
214             }
215             else
216 0           { printf "%-12s :%-10s %s\n", $abbrev, $mod, $name;
217             }
218             }
219 0           print "*** ".(keys %$pkg_of)." symbols in total\n";
220             }
221              
222             #------------
223              
224             1;