blib/lib/Seis/BuiltinFunctions.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 145 CC 9 SCAR 15.1
CC
9
Cov
0%
get
12.0
slurp
6.0
open
6.0
CRAP 4.5
1 package Seis::BuiltinFunctions;
2 use strict;
3 use warnings;
4 use utf8;
5 use 5.010_001;
6 use Time::HiRes ();
7 use Socket;
8  
9 sub end { @{$_[0]}-1 }
10 sub end_list { @_-1 }
11  
12 sub slurp {
13 if (@_==1) {
14 my $fname = shift;
15 open my $fh, '<', $fname
16 or Carp::croak("Can't open '$fname' for reading: '$!'");
17 scalar(do { local $/; <$fh> })
18 } else {
19 ...
20 }
21 }
22  
23 sub open :method {
24 my $mode = @_==2 && $_[1]->key eq 'w' ? '>' : '<';
25 CORE::open my $fh, $mode, $_[0];
26 return Seis::IO->_new_with_fh($_[0], $fh);
27 }
28  
29 sub get :method {
30 my $stuff = shift;
31 return $stuff->get() if Scalar::Util::blessed($stuff);
32 my $line = <$stuff>;
33 if (defined $line) {
34 $line =~ s/\n//;
35 }
36 $line;
37 }
38  
39 sub close:method {
40 my $stuff = shift;
41 if (UNIVERSAL::isa($stuff, 'Seis::IO')) {
42 $stuff->close;
43 } else {
44 CORE::close($stuff);
45 }
46 }
47  
48 sub getc:method {
49 my $stuff = shift;
50 if (UNIVERSAL::isa($stuff, 'Seis::IO')) {
51 $stuff->getc;
52 } else {
53 CORE::getc($stuff);
54 }
55 }
56  
57 sub now:method {
58 Seis::Instant->_new(Time::HiRes::time())
59 }
60  
61 sub gcd:method {
62 int(Math::BigInt::bgcd(@_) - 0)
63 }
64  
65 sub any:method {
66 Seis::Any->_new(@_);
67 }
68  
69 sub is_prime {
70 require Math::Prime::Util;
71 Math::Prime::Util::is_prime($_[0])
72 }
73  
74 sub ords {
75 my @ret = map { ord($_) } split //, $_[0];
76 wantarray ? @ret : \@ret;
77 }
78  
79 sub connect:method {
80 my ($host, $port) = @_;
81  
82 my $sock;
83 socket($sock, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
84 or die "Cannot create socket: $!";
85 my $address = sockaddr_in($port, inet_aton($host));
86 CORE::connect($sock, $address)
87 or die "Cannot connect $host:$port: $!";
88 return bless $sock, 'Seis::Socket';
89 }
90  
91 sub reduce:method {
92 my $code = shift;
93 return shift unless @_ > 1;
94  
95 my $a = shift;
96 for (@_) {
97 $a = &{$code}($a, $_);
98 }
99 $a;
100 }
101  
102 sub copy:method {
103 require File::Copy;
104 File::Copy::copy($_[0], $_[1])
105 or die Seis::Exception::IO->throw("$!");
106 }
107  
108 1;
109