File: arraydef.pl

package info (click to toggle)
libffi-c-perl 0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 484 kB
  • sloc: perl: 1,517; ansic: 57; sh: 19; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use strict;
use warnings;
use FFI::Platypus 1.00;
use FFI::C::ArrayDef;
use FFI::C::StructDef;

my $ffi = FFI::Platypus->new( api => 1 );
# See FFI::Platypus::Bundle for how bundle works.
$ffi->bundle;

my $point_def = FFI::C::StructDef->new(
  $ffi,
  name  => 'point_t',
  class => 'Point',
  members => [
    x => 'double',
    y => 'double',
  ],
);

my $rect_def = FFI::C::ArrayDef->new(
  $ffi,
  name    => 'rectangle_t',
  class   => 'Rectangle',
  members => [
    $point_def, 2,
  ]
);

$ffi->attach( print_rectangle => ['rectangle_t'] );

my $rect = Rectangle->new([
  { x => 1.5,  y => 2.0  },
  { x => 3.14, y => 11.0 },
]);

print_rectangle($rect);  # [[1.5 2] [3.14 11]]

# move rectangle on the y axis
$rect->[$_]->y( $rect->[$_]->y + 1.0 ) for 0..1;

print_rectangle($rect);  # [[1.5 3] [3.14 12]]