blib/lib/FFI/Raw.pm

filter: Statementstmt 100% 21 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 32 / 32 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« xs/MemPtr.xs ↑ Summary blib/lib/FFI/Raw/Callback.pm »
1 package FFI::Raw;
2 13 $FFI::Raw::VERSION = '0.32';
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 require XSLoader;
7 13 XSLoader::load('FFI::Raw', $FFI::Raw::VERSION);
8  
9 13 require FFI::Raw::Ptr;
10  
11 use overload
12 13 ▶ '&{}' => \&coderef,
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 'bool' => \&_bool;
14  
15 1 ▶sub _bool {
Subroutine _bool: called CC=1 SCAR=0.0
16 1 my $ffi = shift;
17 1 return $ffi;
18 }
19  
20 =head1 NAME
21  
22 FFI::Raw - Perl bindings to the portable FFI library (libffi)
23  
24 =head1 VERSION
25  
26 version 0.32
27  
28 =head1 SYNOPSIS
29  
30 use FFI::Raw;
31  
32 my $cos = FFI::Raw -> new(
33 'libm.so', 'cos',
34 FFI::Raw::double, # return value
35 FFI::Raw::double # arg #1
36 );
37  
38 say $cos -> call(2.0);
39  
40 =head1 DESCRIPTION
41  
42 B<FFI::Raw> provides a low-level foreign function interface (FFI) for Perl based
43 on L<libffi|http://sourceware.org/libffi/>. In essence, it can access and call
44 functions exported by shared libraries without the need to write C/XS code.
45  
46 Dynamic symbols can be automatically resolved at runtime so that the only
47 information needed to use B<FFI::Raw> is the name (or path) of the target
48 library, the name of the function to call and its signature (though it is also
49 possible to pass a function pointer obtained, for example, using L<DynaLoader>).
50  
51 Note that this module has nothing to do with L<FFI>.
52  
53 =head1 METHODS
54  
55 =head2 new( $library, $function, $return_type [, $arg_type ...] )
56  
57 Create a new C<FFI::Raw> object. It loads C<$library>, finds the function
58 C<$function> with return type C<$return_type> and creates a calling interface.
59  
60 If C<$library> is C<undef> then the function is searched in the main program.
61  
62 This method also takes a variable number of types, representing the arguments
63 of the wanted function.
64  
65 =head2 new_from_ptr( $function_ptr, $return_type [, $arg_type ...] )
66  
67 Create a new C<FFI::Raw> object from the C<$function_ptr> function pointer.
68  
69 This method also takes a variable number of types, representing the arguments
70 of the wanted function.
71  
72 =head2 call( [$arg ...] )
73  
74 Execute the C<FFI::Raw> function. This methoed also takes a variable number of
75 arguments, which are passed to the called function. The argument types must
76 match the types passed to C<new> (or C<new_from_ptr>).
77  
78 The C<FFI::Raw> object can be used as a CODE reference as well. Dereferencing
79 the object will work just like call():
80  
81 $cos -> call(2.0); # normal call() call
82 $cos -> (2.0); # dereference as CODE ref
83  
84 This works because FFI::Raw overloads the C<&{}> operator.
85  
86 =head2 coderef( )
87  
88 Return a code reference of a given C<FFI::Raw>.
89  
90 =cut
91  
92 35 ▶sub coderef {
Subroutine coderef: called CC=1 SCAR=0.0
Pod documented
93 35 my $ffi = shift;
94 35 ▶ return sub { $ffi -> call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
95 }
96  
97 =head1 SUBROUTINES
98  
99 =head2 memptr( $length )
100  
101 Create a L<FFI::Raw::MemPtr>. This is a shortcut for C<FFI::Raw::MemPtr-E<gt>new(...)>.
102  
103 =cut
104  
105 1 ▶sub memptr { FFI::Raw::MemPtr -> new(@_) }
Subroutine memptr: called CC=1 SCAR=0.0
Pod documented
106  
107 =head2 callback( $coderef, $ret_type [, $arg_type ...] )
108  
109 Create a L<FFI::Raw::Callback>. This is a shortcut for C<FFI::Raw::Callback-E<gt>new(...)>.
110  
111 =cut
112  
113 7 ▶sub callback { FFI::Raw::Callback -> new(@_) }
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
114  
115 =head1 TYPES
116  
117 =head2 FFI::Raw::void
118  
119 Return a C<FFI::Raw> void type.
120  
121 =cut
122  
123 sub void () { ord 'v' }
124  
125 =head2 FFI::Raw::int
126  
127 Return a C<FFI::Raw> integer type.
128  
129 =cut
130  
131 sub int () { ord 'i' }
132  
133 =head2 FFI::Raw::uint
134  
135 Return a C<FFI::Raw> unsigned integer type.
136  
137 =cut
138  
139 sub uint () { ord 'I' }
140  
141 =head2 FFI::Raw::short
142  
143 Return a C<FFI::Raw> short integer type.
144  
145 =cut
146  
147 sub short () { ord 'z' }
148  
149 =head2 FFI::Raw::ushort
150  
151 Return a C<FFI::Raw> unsigned short integer type.
152  
153 =cut
154  
155 sub ushort () { ord 'Z' }
156  
157 =head2 FFI::Raw::long
158  
159 Return a C<FFI::Raw> long integer type.
160  
161 =cut
162  
163 sub long () { ord 'l' }
164  
165 =head2 FFI::Raw::ulong
166  
167 Return a C<FFI::Raw> unsigned long integer type.
168  
169 =cut
170  
171 sub ulong () { ord 'L' }
172  
173 =head2 FFI::Raw::int64
174  
175 Return a C<FFI::Raw> 64 bit integer type. This requires L<Math::Int64> to work.
176  
177 =cut
178  
179 sub int64 () { ord 'x' }
180  
181 =head2 FFI::Raw::uint64
182  
183 Return a C<FFI::Raw> unsigned 64 bit integer type. This requires L<Math::Int64>
184 to work.
185  
186 =cut
187  
188 sub uint64 () { ord 'X' }
189  
190 =head2 FFI::Raw::char
191  
192 Return a C<FFI::Raw> char type.
193  
194 =cut
195  
196 sub char () { ord 'c' }
197  
198 =head2 FFI::Raw::uchar
199  
200 Return a C<FFI::Raw> unsigned char type.
201  
202 =cut
203  
204 sub uchar () { ord 'C' }
205  
206 =head2 FFI::Raw::float
207  
208 Return a C<FFI::Raw> float type.
209  
210 =cut
211  
212 sub float () { ord 'f' }
213  
214 =head2 FFI::Raw::double
215  
216 Return a C<FFI::Raw> double type.
217  
218 =cut
219  
220 sub double () { ord 'd' }
221  
222 =head2 FFI::Raw::str
223  
224 Return a C<FFI::Raw> string type.
225  
226 =cut
227  
228 sub str () { ord 's' }
229  
230 =head2 FFI::Raw::ptr
231  
232 Return a C<FFI::Raw> pointer type.
233  
234 =cut
235  
236 sub ptr () { ord 'p' }
237  
238 =head1 AUTHOR
239  
240 Alessandro Ghedini <alexbio@cpan.org>
241  
242 =head1 SEE ALSO
243  
244 L<FFI>, L<Ctypes|http://gitorious.org/perl-ctypes>
245  
246 =head1 LICENSE AND COPYRIGHT
247  
248 Copyright 2012 Alessandro Ghedini.
249  
250 This program is free software; you can redistribute it and/or modify it
251 under the terms of either: the GNU General Public License as published
252 by the Free Software Foundation; or the Artistic License.
253  
254 See http://dev.perl.org/licenses/ for more information.
255  
256 =cut
257  
258 13 1; # End of FFI::Raw
« xs/MemPtr.xs ↑ Summary blib/lib/FFI/Raw/Callback.pm »