Perlude/builtins.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 52 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Perlude::builtins;
2 use Perlude;
3 use strict;
4 use warnings;
5 our $VERSION = '0.0';
6  
7 my %builtins = (
8 abs => [
9 qw(
10 abs
11 chr
12 cos
13 defined
14 exp
15 glob
16 hex
17 int
18 lc
19 lcfirst
20 length
21 log
22 oct
23 ord
24 quotemeta
25 rand
26 ref
27 sin
28 sqrt
29 uc
30 ucfirst
31 unlink
32 )
33 ],
34 chomp => [qw( chomp chop )],
35 pack => [qw( pack )],
36 pop => [qw( pop shift )],
37 reverse => [qw( readline reverse )],
38 splice => [qw( splice )],
39 split => [qw( split )],
40 stat => [qw( lstat stat )],
41 substr => [qw( substr )],
42 unpack => [qw( unpack )],
43 );
44  
45 # the snippets of code for each builtin type
46 my %code = (
47 abs => [ '$' => 'return apply { %s } $a[0]' ],
48 chomp => [ '$' => 'return apply { %s; $_ } $a[0]' ],
49 pack => [ '$$' => 'return apply { %s $a[0], @$_ } $a[1]' ],
50 pop => [ '$' => 'return apply { %s @$_ } $a[0]' ],
51 reverse => [ '$' => 'return apply { scalar %s $_ } $a[0]' ],
52 splice => [ '$$$' => << 'CODE' ],
53 return $a[1]
54 ? apply { [ %s @$_, $a[0], $a[1] ] } $a[2]
55 : apply { [ %s @$_, $a[0] ] } $a[2];
56 CODE
57 split => [ '$$' => 'return apply { [ %s $a[0] ] } $a[1]' ],
58 stat => [ '$' => 'return apply { [ %s $_ ] } $a[0]' ],
59 substr => [ '$$$' => << 'CODE' ],
60 return $a[1]
61 ? apply { %s $_, $a[0], $a[1] } $a[2]
62 : apply { %s $_, $a[0] } $a[2];
63 CODE
64 unpack => [ '$$' => 'return apply { [ %s $a[0], $_ ] } $a[1]' ],
65 );
66  
67 # generate the functions
68 for my $type ( keys %code ) {
69 my ( $proto, $code ) = @{ $code{$type} };
70 my $count = $code =~ s/%s/%s/g;
71 for my $builtin ( @{ $builtins{$type} } ) {
72 no strict 'refs';
73 *{"f::$builtin"}
74 = eval sprintf "sub ($proto) { my \@a = \@_; $code }",
75 ($builtin) x $count;
76 die $@ if $@;
77 }
78 }
79  
80 # and a nice alias
81 *f::sub = \&Perlude::enlist;
82  
83 1;
84  
85 __END__