You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that binding was used to C<@titles>, I<not> assignment! If you assign, you
@@ -261,11 +261,11 @@ subroutine, otherwise the C function may stomp all over Perl's memory
261
261
leading to possibly unpredictable behaviour:
262
262
263
263
=begincode :skip-test
264
-
my $ints = CArray[int32].new;
265
-
my $number_of_ints = 10;
266
-
$ints[$number_of_ints - 1] = 0; # extend the array to 10 items
264
+
my $ints = CArray[int32].new;
265
+
my $number_of_ints = 10;
266
+
$ints[$number_of_ints - 1] = 0; # extend the array to 10 items
267
267
268
-
my $n = get_n_ints($ints, $number_of_ints);
268
+
my $n = get_n_ints($ints, $number_of_ints);
269
269
=endcode
270
270
271
271
The memory management of arrays is important to understand. When you create an
@@ -315,20 +315,20 @@ to them (with =) doesn't work. Instead, you have to bind new values to the priva
315
315
members:
316
316
317
317
=begincode :skip-test
318
-
class MyStruct is repr('CStruct') {
319
-
has CArray[num64] $!arr;
320
-
has Str $!str;
321
-
has Point $!point; # Point is a user-defined class
322
-
323
-
submethod TWEAK {
324
-
my $arr := CArray[num64].new;
325
-
$arr[0] = 0.9e0;
326
-
$arr[1] = 0.2e0;
327
-
$!arr := $arr;
328
-
$!str := 'Perl 6 is fun';
329
-
$!point := Point.new;
330
-
}
318
+
class MyStruct is repr('CStruct') {
319
+
has CArray[num64] $!arr;
320
+
has Str $!str;
321
+
has Point $!point; # Point is a user-defined class
322
+
323
+
submethod TWEAK {
324
+
my $arr := CArray[num64].new;
325
+
$arr[0] = 0.9e0;
326
+
$arr[1] = 0.2e0;
327
+
$!arr := $arr;
328
+
$!str := 'Perl 6 is fun';
329
+
$!point := Point.new;
331
330
}
331
+
}
332
332
=endcode
333
333
334
334
As you may have predicted by now, a null is represented by the type object of the
@@ -341,14 +341,14 @@ attributes the same way a C compiler would lay them out in a similar
341
341
C<union> definition; using the C<CUnion> representation:
342
342
343
343
=begincode
344
-
use NativeCall;
344
+
use NativeCall;
345
345
346
-
class MyUnion is repr('CUnion') {
347
-
has int32 $.flags32;
348
-
has int64 $.flags64;
349
-
}
346
+
class MyUnion is repr('CUnion') {
347
+
has int32 $.flags32;
348
+
has int64 $.flags64;
349
+
}
350
350
351
-
say nativesizeof(MyUnion.new); # 8, ie. max(sizeof(MyUnion.flags32), sizeof(MyUnion.flags64))
351
+
say nativesizeof(MyUnion.new); # 8, ie. max(sizeof(MyUnion.flags32), sizeof(MyUnion.flags64))
352
352
=endcode
353
353
354
354
=head2Embedding CStructs and CUnions
@@ -359,17 +359,17 @@ as usual, and to do the latter we use the C<HAS> declarator
359
359
instead:
360
360
361
361
=begincode :skip-test
362
-
class MyStruct is repr('CStruct') {
363
-
has Point $.point; # referenced
364
-
has int32 $.flags;
365
-
}
362
+
class MyStruct is repr('CStruct') {
363
+
has Point $.point; # referenced
364
+
has int32 $.flags;
365
+
}
366
366
367
-
say nativesizeof(MyStruct.new); # 16, ie. sizeof(struct Point *) + sizeof(int32_t)
367
+
say nativesizeof(MyStruct.new); # 16, ie. sizeof(struct Point *) + sizeof(int32_t)
368
368
369
-
class MyStruct2 is repr('CStruct') {
370
-
HAS Point $.point; # embedded
371
-
has int32 $.flags;
372
-
}
369
+
class MyStruct2 is repr('CStruct') {
370
+
HAS Point $.point; # embedded
371
+
has int32 $.flags;
372
+
}
373
373
374
374
say nativesizeof(MyStruct2.new); # 24, ie. sizeof(struct Point) + sizeof(int32_t)
375
375
=endcode
@@ -389,39 +389,39 @@ embedded in a C<CStruct>.
389
389
You have to call C<.deref> on it to access the embedded type.
390
390
391
391
=begincode :skip-test
392
-
my Pointer[int32] $p; #For a pointer on int32;
393
-
my Pointer[MyCstruct] $p2 = some_c_routine();
394
-
my MyCstruct $mc = $p2.deref;
395
-
say $mc.field1;
392
+
my Pointer[int32] $p; #For a pointer on int32;
393
+
my Pointer[MyCstruct] $p2 = some_c_routine();
394
+
my MyCstruct $mc = $p2.deref;
395
+
say $mc.field1;
396
396
=endcode
397
397
398
398
It's quite common for a native function to return a pointer to an array of elements. Typed pointers can be dereferenced as an array to obtain individual elements.
399
399
400
400
=begincode :skip-test
401
-
my $n = 5;
402
-
# returns a pointer to an array of length $n
403
-
my Pointer[Point] $plot = some_other_c_routine($n);
404
-
# display the 5 elements in the array
405
-
for 1 .. $n -> $i {
406
-
my $x = $plot[$i - 1].x;
407
-
my $y = $plot[$i - 1].y;
408
-
say "$i: ($x, $y)";
409
-
}
401
+
my $n = 5;
402
+
# returns a pointer to an array of length $n
403
+
my Pointer[Point] $plot = some_other_c_routine($n);
404
+
# display the 5 elements in the array
405
+
for 1 .. $n -> $i {
406
+
my $x = $plot[$i - 1].x;
407
+
my $y = $plot[$i - 1].y;
408
+
say "$i: ($x, $y)";
409
+
}
410
410
=endcode
411
411
412
412
Pointers can also be updated to reference successive elements in the array:
413
413
414
414
=begincode :skip-test
415
-
my Pointer[Point] $elem = $plot;
416
-
# show differences between successive points
417
-
for 1 ..^ $n {
418
-
my Point $lo = $elem.deref;
419
-
++$elem; # equivalent to $elem = $elem.add(1);
420
-
my Point $hi = (++$elem).deref;
421
-
my $dx = $hi.x = $lo.x;
422
-
my $dy = $hi.y = $lo.y;
423
-
say "$_: delta ($dx, $dy)";
424
-
}
415
+
my Pointer[Point] $elem = $plot;
416
+
# show differences between successive points
417
+
for 1 ..^ $n {
418
+
my Point $lo = $elem.deref;
419
+
++$elem; # equivalent to $elem = $elem.add(1);
420
+
my Point $hi = (++$elem).deref;
421
+
my $dx = $hi.x = $lo.x;
422
+
my $dy = $hi.y = $lo.y;
423
+
say "$_: delta ($dx, $dy)";
424
+
}
425
425
=endcode
426
426
427
427
=head1Buffers and Blobs
@@ -533,7 +533,7 @@ Variables exported by a library – also named "global" or "extern"
533
533
variables – can be accessed using C<cglobal>. For example:
534
534
535
535
=forcode :skip-test
536
-
my $var := cglobal('libc.so.6', 'errno', int32)
536
+
my $var := cglobal('libc.so.6', 'errno', int32)
537
537
538
538
This code binds to C<$var> a new L<Proxy|/type/Proxy> object that
539
539
redirects all its accesses to the integer variable named "errno" as
@@ -595,7 +595,7 @@ You'll need to install MySQL server locally; on Debian-esque systems
595
595
it can be installed with something like:
596
596
597
597
=forcode :lang<shell>
598
-
sudo apt-get install mysql-server
598
+
sudo apt-get install mysql-server
599
599
600
600
Prepare your system along these lines before trying out the examples:
601
601
@@ -609,14 +609,14 @@ Prepare your system along these lines before trying out the examples:
0 commit comments