File: c.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 (61 lines) | stat: -rw-r--r-- 1,200 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use strict;
use warnings;
use FFI::C;

package ColorValue {
  FFI::C->struct([
    red   => 'uint8',
    green => 'uint8',
    blue  => 'uint8',
  ]);
}

package NamedColor {
  FFI::C->struct([
    name  => 'string(22)',
    value => 'color_value_t',
  ]);
}

package ArrayNamedColor {
  FFI::C->array(['named_color_t' => 4]);
};

my $array = ArrayNamedColor->new([
  { name => "red",    value => { red   => 255 } },
  { name => "green",  value => { green => 255 } },
  { name => "blue",   value => { blue  => 255 } },
  { name => "purple", value => { red   => 255,
                                 blue  => 255 } },
]);

# dim each color by 1/2
foreach my $color (@$array)
{
  $color->value->red  ( $color->value->red   / 2 );
  $color->value->green( $color->value->green / 2 );
  $color->value->blue ( $color->value->blue  / 2 );
}

# print out the colors
foreach my $color (@$array)
{
  printf "%s [%02x %02x %02x]\n",
    $color->name,
    $color->value->red,
    $color->value->green,
    $color->value->blue;
}

package AnyInt {
  FFI::C->union([
    u8  => 'uint8',
    u16 => 'uint16',
    u32 => 'uint32',
    u64 => 'uint64',
  ]);
}

my $int = AnyInt->new({ u8 => 42 });
print $int->u32;