File Coverage

blib/lib/SPVM/BlessedObject/Array.pm
Criterion Covered Total %
statement 59 64 92.1
branch 5 10 50.0
condition n/a
subroutine 15 15 100.0
pod 6 7 85.7
total 85 96 88.5


line stmt bran cond sub pod time code
1             package SPVM::BlessedObject::Array;
2              
3 278     278   1922 use strict;
  278         583  
  278         8052  
4 278     278   1432 use warnings;
  278         547  
  278         7026  
5 278     278   1394 use Carp 'confess';
  278         587  
  278         13158  
6              
7 278     278   1793 use base 'SPVM::BlessedObject';
  278         629  
  278         62511  
8              
9             use overload '@{}' => sub {
10 15601     15601   33712 my ($array) = @_;
11            
12 15601         39460 my $elems = $array->to_elems;
13            
14 15601         212631 return $elems;
15 278     278   349950 };
  278         291496  
  278         2580  
16              
17 278     278   19569 use overload fallback => 1;
  278         822  
  278         1069  
18              
19 278     278   200084 use SPVM::ExchangeAPI;
  278         858  
  278         93259  
20              
21 20 50   20 0 153 sub length { my $ret; eval { $ret = shift->_xs_length(@_) }; if ($@) { confess $@ } $ret; }
  20         53  
  20         343  
  20         86  
  0         0  
  20         134  
22              
23 15809 50   15809 1 25260 sub to_elems { my $ret; eval { $ret = shift->_xs_to_elems(@_) }; if ($@) { confess $@ } $ret; }
  15809         28817  
  15809         284327  
  15809         41398  
  0         0  
  15809         28531  
24              
25             sub to_strings {
26 6     6 1 82 my $self = shift;
27            
28 6         22 my $elems = $self->to_elems;
29            
30 6         27 my $strings = [map { $_->to_string } @$elems];
  14         74  
31            
32 6         157 return $strings;
33             }
34              
35             sub to_bins {
36 2     2 1 21 my $self = shift;
37            
38 2         8 my $elems = $self->to_elems;
39            
40 2         9 my $binaries = [map { $_->to_bin } @$elems];
  4         19  
41            
42 2         53 return $binaries;
43             }
44              
45 46 50   46 1 245 sub to_bin { my $ret; eval { $ret = shift->_xs_to_bin(@_) }; if ($@) { confess $@ } $ret; }
  46         80  
  46         711  
  46         146  
  0         0  
  46         141  
46              
47 12 50   12 1 83 sub set { my $ret; eval { $ret = shift->_xs_set(@_) }; if ($@) { confess $@ } $ret; }
  12         22  
  12         166  
  12         36  
  0         0  
  12         27  
48              
49 12 50   12 1 22 sub get { my $ret; eval { $ret = shift->_xs_get(@_) }; if ($@) { confess $@ } $ret; }
  12         24  
  12         261  
  12         45  
  0         0  
  12         32  
50              
51             1;
52              
53             =head1 Name
54              
55             SPVM::BlessedObject::Array - SPVM Array
56              
57             =head2 DESCRIPTION
58              
59             The object of the C class holds a SPVM array.
60              
61             =head1 Usage
62              
63             # Gets an element of the array
64             my $elem = $blessed_object_array->get(2);
65            
66             # Sets an element of the array
67             $blessed_object_array->set(2 => 5);
68            
69             # Converts a SPVM array to a Perl array reference
70             my $elems = $blessed_object_array->to_elems;
71            
72             # Converts a SPVM array to a binary
73             my $binary = $blessed_object_array->to_bin;
74              
75             =head1 Methods
76              
77             =head2 get
78              
79             my $elem = $blessed_object_array->get($index);
80              
81             Returns an element of the array with the index.
82              
83             =head2 set
84              
85             $blessed_object_array->set($index, $elem);
86              
87             Sets an element of the array with the index.
88              
89             If $elem cannnot be assigned to the element of the array, an exception is thrown.
90              
91             =head2 to_elems
92              
93             my $elems = $blessed_object_array->to_elems;
94              
95             Converts a SPVM array to a Perl array reference and returns it.
96              
97             =head2 to_bin
98              
99             my $binary = $blessed_object_array->to_bin;
100              
101             Converts a SPVM array to a binary and returns it.
102              
103             This binary is unpacked by L function.
104              
105             If the array is an object array, an excetion is thrown.
106              
107             Examples:
108              
109             # byte[]
110             my @elems = unpack 'c*', $binary;
111            
112             # short[]
113             my @elems = unpack 's*', $binary;
114            
115             # int[]
116             my @elems = unpack 'l*', $binary;
117              
118             # long[]
119             my @elems = unpack 'q*', $binary;
120              
121             # float[]
122             my @elems = unpack 'f*', $binary;
123              
124             # double[]
125             my @elems = unpack 'd*', $binary;
126              
127             =head2 to_strings
128              
129             my $elems = $blessed_object_array->to_strings;
130              
131             Converts a SPVM string array to a Perl array reference and returns it.
132              
133             Each element calls L method.
134              
135             =head2 to_bins
136              
137             my $elems = $blessed_object_array->to_bins;
138              
139             Converts a SPVM string array to Perl array reference and returns it.
140              
141             Each element calls L method.
142              
143             =head1 Operators
144              
145             Overloads the following operators.
146              
147             =head2 array dereference
148            
149             my @elems = @$array;
150              
151             This is the same as the following operation.
152              
153             my @elems = @{$array->to_elems};
154              
155             =head1 Copyright & License
156              
157             Copyright (c) 2023 Yuki Kimoto
158              
159             MIT License