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   1867 use strict;
  278         541  
  278         7787  
4 278     278   1464 use warnings;
  278         559  
  278         6924  
5 278     278   1401 use Carp 'confess';
  278         613  
  278         13067  
6              
7 278     278   1837 use base 'SPVM::BlessedObject';
  278         642  
  278         61146  
8              
9             use overload '@{}' => sub {
10 15601     15601   32569 my ($array) = @_;
11            
12 15601         38433 my $elems = $array->to_elems;
13            
14 15601         205762 return $elems;
15 278     278   345108 };
  278         279975  
  278         2565  
16              
17 278     278   19713 use overload fallback => 1;
  278         861  
  278         996  
18              
19 278     278   196672 use SPVM::ExchangeAPI;
  278         811  
  278         91777  
20              
21 20 50   20 0 136 sub length { my $ret; eval { $ret = shift->_xs_length(@_) }; if ($@) { confess $@ } $ret; }
  20         43  
  20         312  
  20         87  
  0         0  
  20         111  
22              
23 15809 50   15809 1 23935 sub to_elems { my $ret; eval { $ret = shift->_xs_to_elems(@_) }; if ($@) { confess $@ } $ret; }
  15809         28544  
  15809         270180  
  15809         40984  
  0         0  
  15809         27993  
24              
25             sub to_strings {
26 6     6 1 74 my $self = shift;
27            
28 6         23 my $elems = $self->to_elems;
29            
30 6         25 my $strings = [map { $_->to_string } @$elems];
  14         74  
31            
32 6         150 return $strings;
33             }
34              
35             sub to_bins {
36 2     2 1 20 my $self = shift;
37            
38 2         9 my $elems = $self->to_elems;
39            
40 2         17 my $binaries = [map { $_->to_bin } @$elems];
  4         14  
41            
42 2         44 return $binaries;
43             }
44              
45 46 50   46 1 172 sub to_bin { my $ret; eval { $ret = shift->_xs_to_bin(@_) }; if ($@) { confess $@ } $ret; }
  46         67  
  46         649  
  46         153  
  0         0  
  46         118  
46              
47 12 50   12 1 61 sub set { my $ret; eval { $ret = shift->_xs_set(@_) }; if ($@) { confess $@ } $ret; }
  12         20  
  12         158  
  12         31  
  0         0  
  12         22  
48              
49 12 50   12 1 22 sub get { my $ret; eval { $ret = shift->_xs_get(@_) }; if ($@) { confess $@ } $ret; }
  12         18  
  12         290  
  12         39  
  0         0  
  12         27  
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