Sidef/Types/Perl/Perl.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 218 CC 24 SCAR 51.4
CC
24
Cov
0%
to_sidef
342.0
tie
20.0
untie
12.0
CRAP 170.4
1 package Sidef::Types::Perl::Perl;
2  
3 use utf8;
4 use 5.016;
5 use Scalar::Util qw();
6  
7 use parent qw(
8 Sidef::Object::Object
9 );
10  
11 use overload q{""} => sub {
12 'Perl(' . ${Sidef::Types::String::String->new(${$_[0]})->dump} . ')';
13 };
14  
15 sub new {
16 my (undef, $code) = @_;
17  
18 if (ref($_[0]) eq __PACKAGE__) {
19 return $_[0]->eval;
20 }
21  
22 bless \(my $o = "$code"), __PACKAGE__;
23 }
24  
25 *call = \&new;
26  
27 sub code {
28 my ($self) = @_;
29 Sidef::Types::String::String->new($$self);
30 }
31  
32 sub numeric_version {
33 Sidef::Types::Number::Number->new($]);
34 }
35  
36 sub version {
37 Sidef::Types::String::String->new($^V);
38 }
39  
40 sub to_sidef {
41 my ($self, $data) = @_;
42  
43 my %refs;
44  
45 sub {
46 my ($val) = @_;
47  
48 my $ref = CORE::ref($val // return undef);
49  
50 if (index($ref, "Sidef::") == 0) {
51 return $val;
52 }
53  
54 if ($ref eq 'ARRAY') {
55 my $array = $refs{$val} //= Sidef::Types::Array::Array->new([]);
56 foreach my $item (@{$val}) {
57 push @$array,
58 (
59 ref($item) eq 'ARRAY' && $item eq $val
60 ? $array
61 : __SUB__->($item)
62 );
63 }
64 return $array;
65 }
66  
67 if ($ref eq 'HASH') {
68 my $hash = $refs{$val} //= Sidef::Types::Hash::Hash->new;
69 foreach my $key (keys %{$val}) {
70 my $value = $val->{$key};
71 $hash->{$key} = (
72 ref($value) eq 'HASH' && $value eq $val
73 ? $hash
74 : __SUB__->($value)
75 );
76 }
77 return $hash;
78 }
79  
80 if ($ref eq 'CODE') {
81 return Sidef::Types::Block::Block->new(
82 code => sub {
83 map { $self->to_sidef($_) } $val->(map { (index(ref($_), 'Sidef::') == 0) ? $_->get_value : $_ } @_);
84 }
85 );
86 }
87  
88 if ($ref eq 'Regexp') {
89 return Sidef::Types::Regex::Regex->new($val);
90 }
91  
92 if ( $ref eq 'Math::BigFloat'
93 or $ref eq 'Math::BigInt'
94 or $ref eq 'Math::BigRat'
95 or $ref eq 'Math::BigInt::Lite') {
96 return Sidef::Types::Number::Number->new($val->bstr);
97 }
98  
99 if ($ref eq 'Math::Complex') {
100 return Sidef::Types::Number::Complex->new($val->Re, $val->Im);
101 }
102  
103 if ( $ref eq 'Math::MPFR'
104 or $ref eq 'Math::GMPz'
105 or $ref eq 'Math::GMPq'
106 or $ref eq 'Math::MPC') {
107 return Sidef::Types::Number::Number->new($val);
108 }
109  
110 if ($ref eq '') {
111  
112 if (Scalar::Util::looks_like_number($val) and $val !~ /^0[0-9]/) {
113  
114 if ($val =~ tr/e.//) { # parse as float
115 return Sidef::Types::Number::Number::_set_str('float', "$val");
116 }
117  
118 return Sidef::Types::Number::Number->new("$val");
119 }
120  
121 return Sidef::Types::String::String->new($val);
122 }
123  
124 if ($ref eq 'JSON::PP::Boolean') {
125 return (
126 $val
127 ? Sidef::Types::Bool::Bool::TRUE
128 : Sidef::Types::Bool::Bool::FALSE
129 );
130 }
131  
132 # Return an OO object when $val is blessed
133 if (defined Scalar::Util::blessed($val)) {
134 return Sidef::Module::OO->__NEW__($val);
135 }
136  
137 $val;
138 }
139 ->($data);
140 }
141  
142 sub execute {
143 ref($_[0]) || shift(@_);
144 my ($self) = @_;
145 __PACKAGE__->to_sidef(CORE::eval($$self));
146 }
147  
148 *run = \&execute;
149 *eval = \&execute;
150  
151 sub tie {
152 my ($self, $variable, $class_name, @args) = @_;
153 my $type = Scalar::Util::reftype($variable);
154 __PACKAGE__->to_sidef(
155 CORE::tie(($type eq 'ARRAY' ? (@$variable) : $type eq 'HASH' ? %$variable : $variable), "$class_name", map { defined($_) ? $_->get_value : $_ } @args));
156 }
157  
158 sub untie {
159 my ($self, $variable) = @_;
160 my $type = Scalar::Util::reftype($variable);
161 __PACKAGE__->to_sidef(CORE::untie($type eq 'ARRAY' ? (@$variable) : $type eq 'HASH' ? %$variable : $variable));
162 }
163  
164 sub dump {
165 my ($self) = @_;
166 Sidef::Types::String::String->new("$self");
167 }
168  
169 *to_s = \&dump;
170 *to_str = \&dump;
171  
172 1