blib/lib/List/Gen/Lazy/Builtins.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 153 SCAR 40.3
CC
7
Cov
0%
wrap
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 56.0
1 package List::Gen::Lazy::Builtins;
2 use warnings;
3 use strict;
4 use Carp;
5 use List::Gen::Lazy 'fn';
6
7 BEGIN {
8 no strict 'refs';
9 *import = *List::Gen::import;
10 *{uc $_} = *{'List::Gen::'.uc} for 'version'
11 }
12
13 our (@EXPORT_OK, @EXPORT, %EXPORT_TAGS) = 'wrap';
14
15 our $WARN;
16 $WARN = 1 unless defined $WARN;
17
18 my %styles = (
19 '$' => sub {"\$_[@_]"},
20 '*' => sub {"\$_[@_]"},
21 '\$' => sub {'${$_['."@_]}"},
22 '\@' => sub {'@{$_['."@_]}"},
23 '\%' => sub {'%{$_['."@_]}"},
24 '\&' => sub {'&{$_['."@_]}"},
25 '&' => sub {'\&{$_['."@_]}"},
26 '\*' => sub {'*{$_['."@_]}"},
27 '@' => sub {$_[0] == 0 ? '@_' : "\@_[@_ .. \$#_]"},
28 '%' => sub {"\@_[@_ .. \$#_]"},
29 '_' => sub {"\$#_ >= @_ ? \$_[@_] : \$_"},
30 );
31
32 sub wrap {
33 my $name = shift;
34 my %cfg = (
35 arity => 1,
36 returns => 1,
37 proto => eval {prototype($name) or defined prototype("CORE::$name") ? prototype("CORE::$name")||' ' : '@' } || '@',
38 styles => {},
39 @_
40 );
41
42 unless (keys %{$cfg{styles}}) {
43 my ($head, $tail) = $cfg{proto} =~ /^([^;]*);?(.*)/;
44 my @head = $List::Gen::proto_split->($head);
45 my @tail = $List::Gen::proto_split->($tail);
46
47 my $need = @head;
48
49 $cfg{styles}{$need} = \@head;
50 for my $i (0 .. $#tail) {
51 $cfg{styles}{$need + $i + 1} = [@head, @tail[0 .. $i]]
52 }
53 }
54
55 my (@pre, @post);
56 my $add = sub {push @pre, shift; unshift @post, @_};
57
58 my $wrap_code;
59 if (ref $name eq 'CODE') {
60 $wrap_code = $name;
61 $name = '$wrap_code->';
62 } else {
63 $name =~ /^\w+$/ or croak "invalid name for wrap: $name";
64 }
65
66 $add->("sub ($cfg{proto}) {", '}');
67 for my $num ((sort {$a <=> $b} grep {/^\d+$/} keys %{$cfg{styles}}), '@') {
68 my $styles = $cfg{styles}{$num} or next;
69 my $pred;
70 if ($num eq '@') {
71 $pred = 'if (1 ) {';
72 } elsif (@$styles and $$styles[$#$styles] eq '@') {
73 $pred = "if (\@_ >= $num) {";
74 } else {
75 $pred = "if (\@_ == $num) {";
76 }
77 my $n = 0;
78 $add->(" $pred return $name(".join(', ' => map {($styles{$_} or die "$name: no style: $_")->($n++)} @$styles).'); }');
79 }
80
81 unless (keys %{$cfg{styles}}) {
82 $add->("return $name(\@_)")
83 }
84 no warnings;
85 eval "fn @pre @post" or die $@;
86 }
87
88 my @builtin = qw(
89 abs accept alarm atan2 bind binmode bless caller chdir chmod chomp
90 chop chown chr chroot close closedir connect cos crypt dbmclose dbmopen
91 default defined die do endgrent endhostent endnetent endprotoent
92 endpwent endservent eof eval exec exit exp fcntl fileno flock fork
93 formline getc getgrent getgrgid getgrnam gethostbyaddr gethostbyname
94 gethostent getlogin getnetbyaddr getnetbyname getnetent getpeername
95 getpgrp getppid getpriority getprotobyname getprotobynumber getprotoent
96 getpwent getpwnam getpwuid getservbyname getservbyport getservent
97 getsockname getsockopt glob gmtime goto hex index int ioctl join
98 kill lc lcfirst length link listen localtime lock log lstat mkdir
99 msgctl msgget msgrcv msgsnd oct open opendir ord pack pipe pop pos
100 print printf prototype push quotemeta rand read readdir readline
101 readlink readpipe recv ref rename require reset reverse rewinddir
102 rindex rmdir say scalar seek seekdir select semctl semget semop send
103 setgrent sethostent setnetent setpgrp setpriority setprotoent setpwent
104 setservent setsockopt shift shmctl shmget shmread shmwrite shutdown sin
105 sleep socket socketpair splice split sprintf sqrt srand stat study
106 substr symlink syscall sysopen sysread sysseek system syswrite tell
107 telldir tied time times truncate uc ucfirst umask undef unlink unpack
108 unshift untie utime vec wait waitpid wantarray warn when write
109 );
110
111 for my $fn (@builtin) {
112 no strict 'refs';
113 my $code = eval {wrap $fn};
114 unless ($code) {
115 warn "could not wrap '$fn': $@\n" if $WARN;
116 next;
117 }
118 *$_ = $code for $fn, "lazy_$fn", ucfirst $fn, "_$fn";
119
120 push @EXPORT, ucfirst $fn;
121
122 push @EXPORT_OK, "lazy_$fn";
123 push @{$EXPORT_TAGS{lazy}}, "lazy_$fn";
124
125 push @EXPORT_OK, "_$fn";
126 push @{$EXPORT_TAGS{_}}, "_$fn";
127
128 push @EXPORT_OK, $fn;
129 push @{$EXPORT_TAGS{userspace}}, ucfirst $fn;
130 }
131
132 @EXPORT_OK = keys %{{map {$_ => 1} @EXPORT_OK, @EXPORT}};
133 $EXPORT_TAGS{':all'} = \@EXPORT_OK;
134 $EXPORT_TAGS{':base'} = \@EXPORT;
135
136
137 =head1 NAME
138
139 List::Gen::Builtins - perl builtin functions with partial + lazy application
140
141 =head1 SYNOPSIS
142
143 this module implements most of the perl functions with C< fn() > from
144 L<List::Gen::Lazy>. you can import functions from this module as follows:
145
146 =head1 EXPORT
147
148 builtin's with prototypes have the same prototype when exported from this module.
149
150 to export the builtins in ucfirst:
151
152 use List::Gen::Builtins; # ucfirst is default
153
154 my $int = Int my $float;
155
156 $float = 4.333;
157
158 say $int; # 4
159
160 or to export prefixed with C< '_' >:
161
162 use List::Gen::Builtins ':_';
163
164 my $int = _int my $float;
165
166 or to export prefixed with C< 'lazy_' >:
167
168 use List::Gen::Builtins ':lazy';
169
170 my $int = lazy_int my $float;
171
172 to export the builtins as like named userspace functions:
173
174 use List::Gen::Builtins ':userspace';
175
176 my $int = &int(my $float);
177
178 note that as always, when a user function is called with C< & >, prototypes are
179 disabled, this means that you must call functions like C< &shift > as:
180
181 my $x = &shift(\@array); # must use parens and manually take the reference
182
183 the implemented functions are:
184
185 abs accept alarm atan2 bind binmode bless caller chdir chmod chomp chop
186 chown chr chroot close closedir connect cos crypt dbmclose dbmopen default
187 defined die do endgrent endhostent endnetent endprotoent endpwent
188 endservent eof eval exec exit exp fcntl fileno flock fork formline getc
189 getgrent getgrgid getgrnam gethostbyaddr gethostbyname gethostent getlogin
190 getnetbyaddr getnetbyname getnetent getpeername getpgrp getppid getpriority
191 getprotobyname getprotobynumber getprotoent getpwent getpwnam getpwuid
192 getservbyname getservbyport getservent getsockname getsockopt glob gmtime
193 goto hex index int ioctl join keys kill lc lcfirst length link listen
194 localtime lock log lstat mkdir msgctl msgget msgrcv msgsnd oct open opendir
195 ord pack pipe pop pos print printf prototype push quotemeta rand read
196 readdir readline readlink readpipe recv ref rename require reset reverse
197 rewinddir rindex rmdir say scalar seek seekdir select semctl semget semop
198 send setgrent sethostent setnetent setpgrp setpriority setprotoent setpwent
199 setservent setsockopt shift shmctl shmget shmread shmwrite shutdown sin
200 sleep socket socketpair splice split sprintf sqrt srand stat study substr
201 symlink syscall sysopen sysread sysseek system syswrite tell telldir tied
202 time times truncate uc ucfirst umask undef unlink unpack unshift untie utime
203 values vec wait waitpid wantarray warn when write
204
205 just because they have been implemented, that says nothing about their
206 usefulness as a lazy function.
207
208 =head1 FUNCTIONS
209
210 =over 4
211
212 =item C< wrap NAME OPTIONS >
213
214 =item C< wrap CODE OPTIONS >
215
216 C< wrap > is used to automatically wrap the builtin functions in a C< fn(...) >
217 function that calls the builtin with the arguments passed in.
218
219 *lazy_open = wrap 'open',
220 styles => {
221 1 => ['\@'],
222 2 => ['\@', '$',],
223 3 => ['\@', '$', '$'],
224 '@' => ['\@', '$', '$', '@']
225 },
226 proto => '\@;$$@';
227 arity => 1,
228 returns => 1;
229
230 all of the options are optional, in fact, with a proper prototype, C< wrap >
231 will determine everything itself:
232
233 *lazy_open = wrap 'open'; # same as with the options above
234
235 C< wrap > does not install anything, it returns the lazy coderef.
236
237 =back
238
239 =head1 AUTHOR
240
241 Eric Strom, C<< <asg at cpan.org> >>
242
243 =head1 BUGS
244
245 this module has barely been tested, ymmv
246
247 several functions are not available in 5.13+ and a warning will be generated.
248 to silence this:
249
250 BEGIN {$List::Gen::Lazy::Builtins::WARN = 0} # before calling 'use'
251 use List::Gen::Lazy::Builtins;
252
253 report any bugs / feature requests to C<bug-list-gen at rt.cpan.org>, or through
254 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Gen>.
255
256 comments / feedback / patches are also welcome.
257
258 =head1 COPYRIGHT & LICENSE
259
260 copyright 2009-2011 Eric Strom.
261
262 this program is free software; you can redistribute it and/or modify it under
263 the terms of either: the GNU General Public License as published by the Free
264 Software Foundation; or the Artistic License.
265
266 see http://dev.perl.org/licenses/ for more information.
267
268 =cut
269
270
271 1;