File: C-Cookbook.pod

package info (click to toggle)
libinline-perl 0.50-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 632 kB
  • sloc: perl: 3,950; makefile: 2
file content (1514 lines) | stat: -rw-r--r-- 41,052 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
=head1 NAME

Inline::C-Cookbook - A Cornucopia of Inline C Recipes

=cut --------------------------------------------------------------------------

=head1 DESCRIPTION

It's a lot easier for most of us to cook a meal from a recipe, rather
than just throwing things into a pot until something edible forms. So it
is with programming as well. C<Inline.pm> makes C programming for Perl
as easy as possible. Having a set of easy to understand samples, makes
it simpler yet.

This Cookbook is intended to be an evergrowing repository of small yet
complete coding examples; each showing how to accomplish a particular
task with Inline. Each example is followed by a short discussion,
explaining in detail the particular features that are being
demonstrated.

Many of these recipes are apdapted from email discussions I have had
with Inline users around the world. It has been my experience so far,
that Inline provides an elegant solution to almost all problems
involving Perl and C.

Bon Appetit!

=cut --------------------------------------------------------------------------

=head1 Appetizers

=cut --------------------------------------------------------------------------

=head2 Hello, world

=over 4

=item Problem

It seems that the first thing any programmer wants to do when he learns
a new programming technique is to use it to greet the Earth. How can I
do this using Inline?

=item Solution

    use Inline C => <<'END_C';

    void greet() {
        printf("Hello, world\n");
    }
    END_C

    greet;

=item Discussion

Nothing too fancy here. We define a single C function C<greet()> which
prints a message to STDOUT. One thing to note is that since the Inline
code comes before the function call to C<greet>, we can call it as a
bareword (no parentheses).

=item See Also

See L<Inline> and L<Inline::C> for basic info about C<Inline.pm>.

=item Credits

Brian Kernigan

Dennis Ritchie

=back

=cut --------------------------------------------------------------------------

=head2 One Liner

=over 4

=item Problem

A concept is valid in Perl only if it can be shown to work in one line.
Can Inline reduce the complexities of Perl/C interaction to a one-liner?

=item Solution

    perl -e 'use Inline C=>q{void greet(){printf("Hello, world\n");}};greet'

=item Discussion

Try doing that in XS :-)

=item See Also

My email signature of late is:

    perl -le 'use Inline C=>q{SV*JAxH(char*x){return newSVpvf("Just Another %s Hacker",x);}};print JAxH+Perl'

A bit fancier but a few bytes too long to qualify as a true one liner :-(

=item Credits

"Eli the Bearded" <elijah@workspot.net> gave me the idea that I should
have an Inline one-liner as a signature.

=back

=cut --------------------------------------------------------------------------

=head1 Meat & Potatoes

=cut --------------------------------------------------------------------------

=head2 Data Types

=over 4

=item Problem

How do I pass different types of data to and from Inline C functions;
like strings, numbers and integers?

=item Solution

    # vowels.pl
    use Inline C;

    $filename = $ARGV[0];
    die "Usage: perl vowels.pl filename\n" unless -f $filename;

    $text = join '', <>;           # slurp input file
    $vp = vowel_scan($text);       # call our function
    $vp = sprintf("%03.1f", $vp * 100);  # format for printing
    print "The letters in $filename are $vp% vowels.\n";

    __END__
    __C__

    /* Find percentage of vowels to letters */
    double vowel_scan(char* str) {
        int letters = 0;
        int vowels = 0;
        int i = 0;
        char c;
        char normalize = 'a' ^ 'A';
        /* normalize forces lower case in ASCII; upper in EBCDIC */
        char A = normalize | 'a';
        char E = normalize | 'e';
        char I = normalize | 'i';
        char O = normalize | 'o';
        char U = normalize | 'u';
        char Z = normalize | 'z';

        while(c = str[i++]) {
            c |= normalize;
            if (c >= A && c <= Z) {
                 letters++;
                 if (c == A || c == E || c == I || c == O || c == U)
                     vowels++;
            }
        }

        return letters ? ((double) vowels / letters) : 0.0;
    }

=item Discussion

This script takes a file name from the command line and prints the ratio
of vowels to letters in that file. C<vowels.pl> uses an Inline C
function called C<vowel_scan>, that takes a string argument, and returns
the percentage of vowels as a floating point number between 0 and 1. It
handles upper and lower case letters, and works with ASCII and EBCDIC.
It is also quite fast.

Running this script produces:

    > perl vowels.pl /usr/share/dict/words
    The letters in /usr/share/dict/words are 37.5% vowels.

=item See Also

The Perl Journal vol #19 has an article about Inline which uses this example.

=item Credits

This example was reprinted by permission of The Perl Journal. It was
edited to work with Inline v0.30 and higher.

=back

=cut --------------------------------------------------------------------------

=head2 Variable Argument Lists

=over 4

=item Problem

How do I pass a variable-sized list of arguments to an Inline C function?

=item Solution

    greet(qw(Sarathy Jan Sparky Murray Mike));

    use Inline C => <<'END_OF_C_CODE';

    void greet(SV* name1, ...) {
        Inline_Stack_Vars;
        int i;

        for (i = 0; i < Inline_Stack_Items; i++)
            printf("Hello %s!\n", SvPV(Inline_Stack_Item(i), PL_na));

        Inline_Stack_Void;
    }

    END_OF_C_CODE


=item Discussion

This little program greets a group of people, such as my
coworkers. We use the C<C> ellipsis syntax: "C<...>", since the
list can be of any size.

Since there are no types or names associated with each argument, we
can't expect XS to handle the conversions for us. We'll need to pop them
off the B<Stack> ourselves. Luckily there are two functions (macros)
that make this a very easy task.

First, we need to begin our function with a "C<Inline_Stack_Vars>"
statement. This defines a few internal variables that we need to access
the B<Stack>. Now we can use "C<Inline_Stack_Items>", which returns an
integer containing the number of arguments passed to us from Perl.

B<NOTE:> It is important to I<only> use "C<Inline_Stack_>" macros when
there is an ellipsis (C<...>) in the argument list, I<or> the function
has a return type of void.

Second, we use the C<Inline_Stack_Item(x)> function to access each
argument where "0 <= x < items".

B<NOTE:> When using a variable length argument list, you have to
specify at least one argument before the ellipsis. (On my compiler,
anyway.) When XS does it's argument checking, it will complain if you
pass in less than the number of I<defined> arguments. Therefore, there
is currently no way to pass an empty list when a variable length list
is expected.

=item See Also

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Multiple Return Values

=over 4

=item Problem

How do I return a list of values from a C function?

=item Solution

    print map {"$_\n"} get_localtime(time);

    use Inline C => <<'END_OF_C_CODE';

    #include <time.h>

    void get_localtime(int utc) {
      struct tm *ltime = localtime(&utc);
      Inline_Stack_Vars;

      Inline_Stack_Reset;
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_year)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_mon)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_mday)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_hour)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_min)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_sec)));
      Inline_Stack_Push(sv_2mortal(newSViv(ltime->tm_isdst)));
      Inline_Stack_Done;
    }
    END_OF_C_CODE

=item Discussion

Perl is a language where it is common to return a list of values
from a subroutine call instead of just a single value. C is not such
a language. In order to accomplish this in C we need to manipulate
the Perl call stack by hand. Luckily, Inline provides macros to make
this easy.

This example calls the system C<localtime>, and returns each of the
parts of the time struct; much like the perl builtin C<localtime()>. On
each stack push, we are creating a new Perl integer (SVIV) and
mortalizing it. The sv_2mortal() call makes sure that the reference
count is set properly. Without it, the program would leak memory.

NOTE:
The C<#include> statement is not really needed, because Inline
automatically includes the Perl headers which include almost all
standard system calls.

=item See Also

For more information on the Inline stack macros, see L<Inline::C>.

=item Credits

Richard Anderson <starfire@zipcon.net> contributed the original idea for
this snippet.

=back

=cut --------------------------------------------------------------------------

=head2 Multiple Return Values (Another Way)

=over 4

=item Problem

How can I pass back more than one value without using the Perl Stack?

=item Solution

    use Inline::Files;
    use Inline C;

    my ($foo, $bar);
    change($foo, $bar);

    print "\$foo = $foo\n";
    print "\$bar = $bar\n";

    __C__

    int change(SV* var1, SV* var2) {
        sv_setpvn(var1, "Perl Rocks!", 11);
        sv_setpvn(var2, "Inline Rules!", 13);
        return 1;
    }

=item Discussion

Most perl function interfaces return values as a list of one or more
scalars. Very few like C<chomp>, will modify an input scalar in place.
On the other hand, in C you do this quite often. Values are passed in by
reference and modified in place by the called function.

It turns out that we can do that with Inline as well. The secret is to
use a type of 'C<SV*>' for each argument that is to be modified. This
ensures passing by reference, because no typemapping is needed.

The function can then use the Perl5 API to operate on that argument.
When control returns to Perl, the argument will retain the value set by
the C function. In this example we passed in 2 empty scalars and
assigned values directly to them.

=item See Also

=item Credits

Ned Konz <ned@bike-nomad.com> brought this behavior to my attention. He
also pointed out that he is not the world famous computer cyclist Steve
Roberts (http://www.microship.com), but he is close
(http://bike-nomad.com). Thanks Ned.

=back

=cut --------------------------------------------------------------------------

=head2 Using Memory

=over 4

=item Problem

How should I allocate buffers in my Inline C code?

=item Solution

    print greeting('Ingy');

    use Inline C => <<'END_OF_C_CODE';

    SV* greeting(SV* sv_name) {
        return (newSVpvf("Hello %s!\n", SvPV(sv_name, PL_na)));
    }

    END_OF_C_CODE

=item Discussion

In this example we will return the greeting to the caller, rather than
printing it. This would seem mighty easy, except for the fact that we
need to allocate a small buffer to create the greeting.

I would urge you to stay away from C<malloc>ing your own buffer. Just
use Perl's built in memory management. In other words, just create a new
Perl string scalar. The function C<newSVpv> does just that. And
C<newSVpvf> includes C<sprintf> functionality.

The other problem is getting rid of this new scalar. How will the ref
count get decremented after we pass the scalar back? Perl also provides
a function called C<sv_2mortal>. Mortal variables die when the context
goes out of scope. In other words, Perl will wait until the new scalar
gets passed back and then decrement the ref count for you, thereby
making it eligible for garbage collection. See C<perldoc perlguts>.

In this example the C<sv_2mortal> call gets done under the hood by XS,
because we declared the return type to be C<SV*>.

To view the generated XS code, run the command "C<perl
-MInline=INFO,FORCE,NOCLEAN example004.pl>". This will leave the build
directory intact and tell you where to find it.

=item See Also

=item Credits

=back

=cut --------------------------------------------------------------------------

=head1 Fast Food

=cut --------------------------------------------------------------------------

=head2 Inline CGI

=over 4

=item Problem

How do I use Inline securely in a CGI environment?

=item Solution

    #!/usr/bin/perl

    use CGI qw(:standard);
    use Inline (Config =>
                DIRECTORY => '/usr/local/apache/Inline',
               );

    print (header,
           start_html('Inline CGI Example'),
           h1(JAxH('Inline')),
           end_html
          );

    use Inline C => <<END;
    SV* JAxH(char* x) {
        return newSVpvf("Just Another %s Hacker", x);
    }
    END

=item Discussion

The problem with running Inline code from a CGI script is that Inline
B<writes> to a build area on your disk whenever it compiles code. Most
CGI scripts don't (and shouldn't) be able to create a directory and
write into it.

The solution is to explicitly tell Inline which directory to use with
the 'use Inline Config => DIRECTORY => ...' line. Then you need to give
write access to that directory from the web server (CGI script).

If you see this as a security hole, then there is another option.
Give write access to yourself, but read-only access to the CGI
script. Then run the script once by hand (from the command line).
This will cause Inline to precompile the C code. That way the CGI
will only need read access to the build directory (to load in the
shared library from there).

Just remember that whenever you change the C code, you need to
precompile it again.

=item See Also

See L<CGI> for more information on using the C<CGI.pm> module.

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 mod_perl

=over 4

=item Problem

How do I use Inline with mod_perl?

=item Solution

    package Factorial;
    use strict;
    use Inline Config =>
               DIRECTORY => '/usr/local/apache/Inline',
               ENABLE => 'UNTAINT';
    use Inline 'C';
    Inline->init;

    sub handler {
        my $r = shift;
        $r->send_http_header('text/plain');
        printf "%3d! = %10d\n", $_, factorial($_) for 1..100;
        return Apache::Constants::OK;
    }

    1;
    __DATA__
    __C__
    double factorial(double x) {
        if (x < 2)  return 1;
        return x * factorial(x - 1)
    }

=item Discussion

This is a fully functional mod_perl handler that prints out the
factorial values for the numbers 1 to 100. Since we are using Inline
under mod_perl, there are a few considerations to , um, consider.

First, mod_perl handlers are usually run with C<-T> taint detection.
Therefore, we need to enable the UNTAINT option. The next thing to deal
with is the fact that this handler will most likely be loaded after
Perl's compile time. Since we are using the DATA section, we need to
use the special C<init()> call. And of course we need to specify a
DIRECTORY that mod_perl can compile into. I<See the above CGI example
for more info.>

Other than that, this is a pretty straightforward mod_perl handler,
tuned for even more speed!

=item See Also

See Stas Bekman's upcoming O'Reilly book on mod_perl to which this
example was contributed.

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Object Oriented Inline

=over 4

=item Problem

How do I implement Object Oriented programming in Perl using C objects?

=item Solution

    my $obj1 = Soldier->new('Benjamin', 'Private', 11111);
    my $obj2 = Soldier->new('Sanders', 'Colonel', 22222);
    my $obj3 = Soldier->new('Matt', 'Sergeant', 33333);

    for my $obj ($obj1, $obj2, $obj3) {
        print ($obj->get_serial, ") ",
               $obj->get_name, " is a ",
               $obj->get_rank, "\n");
    }

    #---------------------------------------------------------

    use Inline C => <<'END';

    typedef struct {
      char* name;
      char* rank;
      long  serial;
      } Soldier;



    SV* new(char* class, char* name, char* rank, long serial) {
        Soldier* soldier;
        SV*      obj_ref = newSViv(0);
        SV*      obj = newSVrv(obj_ref, class);

        New(42, soldier, 1, Soldier);

        soldier->name = savepv(name);
        soldier->rank = savepv(rank);
        soldier->serial = serial;

        sv_setiv(obj, (IV)soldier);
        SvREADONLY_on(obj);
        return obj_ref;
     }

     char* get_name(SV* obj) {
           return ((Soldier*)SvIV(SvRV(obj)))->name;
     }

     char* get_rank(SV* obj) {
           return ((Soldier*)SvIV(SvRV(obj)))->rank;
     }

     long get_serial(SV* obj) {
          return ((Soldier*)SvIV(SvRV(obj)))->serial;
     }

     void DESTROY(SV* obj) {
          Soldier* soldier = (Soldier*)SvIV(SvRV(obj));
          Safefree(soldier->name);
          Safefree(soldier->rank);
          Safefree(soldier);
    }

    END

=item Discussion

Damian Conway has given us myriad ways of implementing OOP in Perl. This
is one he might not have thought of.

The interesting thing about this example is that it uses Perl for all
the OO bindings while using C for the attributes and methods.

If you examine the Perl code everything looks exactly like a regular OO
example. There is a C<new> method and several accessor methods. The
familiar 'arrow syntax' is used to invoke them.

In the class definition (second part) the Perl C<package> statement is
used to name the object class or namespace. But that's where the
similarities end Inline takes over.

The idea is that we call a C subroutine called C<new()> which returns a
blessed scalar. The scalar contains a readonly integer which is a C
pointer to a Soldier struct. This is our object.

The C<new()> function needs to malloc the memory for the struct and then
copy the initial values into it using C<savepv()>. This also allocates
more memory (which we have to keep track of).

Note that C<newSVrv()> doesn't create a reference, but returns a new SV
('obj') and makes 'obj_ref' a reference to it. Ultimately, 'obj_ref'
(which is the SV that C<new()> returns) holds a reference to the blessed
scalar in 'obj', which in turn contains an integer that corresponds to
the memory address of the C object.

The accessor methods are pretty straightforward. They return the current
value of their attribute.

The last method C<DESTROY()> is called automatically by Perl whenever an
object goes out of scope. This is where we can free all the memory used
by the object.

That's it. It's a very simplistic example. It doesn't show off any
advanced OO features, but it is pretty cool to see how easy the
implementation can be. The important Perl call is C<newSVrv()> which
creates a blessed scalar.

=item See Also

Read "Object Oriented Perl" by Damian Conway, for more useful ways of
doing OOP in Perl.

You can learn more Perl calls in L<perlapi>. If you don't have Perl
5.6.0 or higher, visit http://www.perldoc.com/perl5.6/pod/perlapi.html

=item Credits

=back

=cut --------------------------------------------------------------------------

=head1 The Main Course

=cut --------------------------------------------------------------------------

=head2 Exposing Shared Libraries

=over 4

=item Problem

You have this great C library and you want to be able to access parts of
it with Perl.

=item Solution

    print get('http://www.axkit.org');

    use Inline C => Config =>
               LIBS => '-lghttp';
    use Inline C => <<'END_OF_C_CODE';

    #include <ghttp.h>

    char *get(SV* uri) {
       SV* buffer;
       ghttp_request* request;

       buffer = NEWSV(0,0);
       request = ghttp_request_new();
       ghttp_set_uri(request, SvPV(uri, PL_na));

       ghttp_set_header(request, http_hdr_Connection, "close");

       ghttp_prepare(request);
       ghttp_process(request);

       sv_catpv(buffer, ghttp_get_body(request));

       ghttp_request_destroy(request);

       return SvPV(buffer, PL_na);
    }

    END_OF_C_CODE

=item Discussion

This example fetches and prints the HTML from http://www.axkit.org
It requires the GNOME http libraries. http://www.gnome.org

One of the most common questions I get is "How can I use Inline to make
use of some shared library?". Although it has always been possible to do
so, the configuration was ugly, and there were no specific examples.

With version 0.30 and higher, you can specify the use of shared
libraries easily with something like this:

    use Inline C => Config => LIBS => '-lghttp';
    use Inline C => "code ...";

or

    use Inline C => "code ...", LIBS => '-lghttp';

To specify a specific library path, use:

    use Inline C => "code ...", LIBS => '-L/your/lib/path -lyourlib';

To specify an include path use:

    use Inline C => "code ...",
               LIBS => '-lghttp',
               INC => '-I/your/inc/path';

=item See Also

The C<LIBS> and C<INC> configuration options are formatted and passed
into MakeMaker. For more info see L<ExtUtils::MakeMaker>. For more
options see L<Inline::C>.

=item Credits

This code was written by Matt Sergeant <matt@sergeant.org>, author of
many CPAN modules. The configuration syntax has been modified for use
with Inline v0.30.

=back

=head2 Automatic Function Wrappers

=over 4

=item Problem

You have some functions in a C library that you want to access from Perl
exactly as you would from C.

=item Solution

The error function C<erf()> is probably defined in your standard math
library. Annoyingly, Perl does not let you access it. To print out a
small table of its values, just say:

    perl -le 'use Inline C => q{ double erf(double); }, ENABLE => "AUTOWRAP"; print "$_ @{[erf($_)]}" for (0..10)'

The excellent C<Term::ReadLine::Gnu> implements Term::ReadLine using the
GNU ReadLine library. Here is an easy way to access just C<readline()>
from that library:

    package MyTerm;

    use Inline C => Config =>
               ENABLE => AUTOWRAP =>
               LIBS => "-lreadline -lncurses -lterminfo -ltermcap ";
    use Inline C => q{ char * readline(char *); };

    package main;
    my $x = MyTerm::readline("xyz: ");

Note however that it fails to C<free()> the memory returned by readline,
and that C<Term::ReadLine::Gnu> offers a much richer interface.

=item Discussion

We access existing functions by merely showing Inline their
declarations, rather than a full definition. Of course the function
declared must exist, either in a library already linked to Perl or in a
library specified using the C<LIBS> option.

The first example wraps a function from the standard math library, so
Inline requires no additional C<LIBS> directive. The second uses the
Config option to specify the libraries that contain the actual
compiled C code.

This behavior is always disabled by default. You must enable the
C<AUTOWRAP> option to make it work.

=item See Also

C<readline>, C<Term::ReadLine::Gnu>

=item Credits

GNU ReadLine was written by Brian Fox <bfox@ai.mit.edu> and Chet Ramey
<chet@ins.cwru.edu>. Term::ReadLine::Gnu was written by Hiroo Hayashi
<hiroo.hayashi@computer.org>. Both are far richer than the slim
interface given here!

The idea of producing wrapper code given only a function declaration is
taken from Swig by David M. Beazley <beazley@cs.uchicago.edu>.

Ingy's inline editorial insight:

This entire entry was contributed by Ariel Scolnicov
<ariels@compugen.co.il>. Ariel also first suggested the idea for Inline
to support function declaration processing.

=back

=cut --------------------------------------------------------------------------

=head2 Complex Data

=over 4

=item Problem

How do I deal with complex data types like hashes in Inline C?

=item Solution

    use Inline C => <<'END_OF_C_CODE';

    void dump_hash(SV* hash_ref) {
        HV* hash;
        HE* hash_entry;
        int num_keys, i;
        SV* sv_key;
        SV* sv_val;

        if (! SvROK(hash_ref))
            croak("hash_ref is not a reference");

        hash = (HV*)SvRV(hash_ref);
        num_keys = hv_iterinit(hash);
        for (i = 0; i < num_keys; i++) {
            hash_entry = hv_iternext(hash);
            sv_key = hv_iterkeysv(hash_entry);
            sv_val = hv_iterval(hash, hash_entry);
            printf("%s => %s\n", SvPV(sv_key, PL_na), SvPV(sv_val, PL_na));
        }
        return;
    }

    END_OF_C_CODE

    my %hash = (
                Author => "Brian Ingerson",
                Nickname => "INGY",
                Module => "Inline.pm",
                Version => "0.30",
                Language => "C",
               );

    dump_hash(\%hash);

=item Discussion

The world is not made of scalars alone, although they are definitely
the easiest creatures to deal with, when doing Inline stuff.
Sometimes we need to deal with arrays, hashes, and code references,
among other things.

Since Perl subroutine calls only pass scalars as arguments, we'll
need to use the argument type C<SV*> and pass references to more
complex types.

The above program dumps the key/value pairs of a hash. To figure it out,
just curl up with L<perlapi> for a couple hours. Actually, its fairly
straight forward once you are familiar with the calls.

Note the C<croak> function call. This is the proper way to die from your
C extensions.

=item See Also

See L<perlapi> for information about the Perl5 internal API.

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Hash of Lists

=over 4

=item Problem

How do I create a Hash of Lists from C?

=item Solution

    use Inline C;
    use Data::Dumper;

    $hash_ref = load_data("./cartoon.txt");
    print Dumper $hash_ref;

    __END__
    __C__

    static int next_word(char**, char*);

    SV* load_data(char* file_name) {
        char buffer[100], word[100], * pos;
        AV* array;
        HV* hash = newHV();
        FILE* fh = fopen(file_name, "r");

        while (fgets(pos = buffer, sizeof(buffer), fh)) {
            if (next_word(&pos, word)) {
                hv_store(hash, word, strlen(word),
                        newRV_noinc((SV*)array = newAV()), 0);
                while (next_word(&pos, word))
                    av_push(array, newSVpvf("%s", word));
            }
        }
        fclose(fh);
        return newRV_noinc((SV*) hash);
    }

    static int next_word(char** text_ptr, char* word) {
        char* text = *text_ptr;
        while(*text != '\0' &&
              *text <= ' ')
            text++;
        if (*text <= ' ')
            return 0;
        while(*text != '\0' &&
              *text > ' ') {
            *word++ = *text++;
        }
        *word = '\0';
        *text_ptr = text;
        return 1;
    }

=item Discussion

This is one of the larger recipes. But when you consider the number of
calories it has, it's not so bad. The function C<load_data> takes the
name of a file as it's input. The file C<cartoon.text> might look like:

    flintstones fred barney
    jetsons     george jane elroy
    simpsons    homer marge bart

The function will read the file, parsing each line into words. Then it
will create a new hash, whereby the first word in a line becomes a hash
key and the remaining words are put into an array whose reference
becomes the hash value. The output looks like this:

    $VAR1 = {
              'flintstones' => [
                                 'fred',
                                 'barney'
                               ],
              'simpsons' => [
                              'homer',
                              'marge',
                              'bart'
                            ],
              'jetsons' => [
                             'george',
                             'jane',
                             'elroy'
                           ]
            };

=item See Also

See L<perlapi> for information about the Perl5 internal API.

=item Credits

Al Danial <alnd@pacbell.net> requested a solution to this on
comp.lang.perl.misc. He borrowed the idea from the "Hash of Lists"
example in the Camel book.

=back

=cut --------------------------------------------------------------------------

=head1 Just Desserts

=cut --------------------------------------------------------------------------

=head2 Win32

=over 4

=item Problem

How do I access Win32 DLL-s using Inline?

=item Solution

    use Inline C => DATA =>
               LIBS => '-luser32';

    $text = "@ARGV" || 'Inline.pm works with MSWin32. Scary...';

    WinBox('Inline Text Box', $text);

    __END__
    __C__

    #include <windows.h>

    int WinBox(char* Caption, char* Text) {
      return MessageBoxA(0, Text, Caption, 0);
    }

=item Discussion

This example runs on MS Windows. It makes a text box appear on the
screen which contains a message of your choice.

The important thing is that its proof that you can use Inline to
interact with Windows DLL-s. Very scary indeed. 8-o

To use Inline on Windows with ActivePerl ( http://www.ActiveState.com )
you'll need MS Visual Studio. You can also use the Cygwin environment,
available at http://www.cygwin.com .

=item See Also

See L<Inline-Support> for more info on MSWin32 programming with Inline.

=item Credits

This example was adapted from some sample code written by Garrett Goebel
<garrett@scriptpro.com>

=back

=cut --------------------------------------------------------------------------

=head2 Embedding Perl in C

=over 4

=item Problem

How do I use Perl from a regular C program?

=item Solution

    #!/usr/bin/cpr

    int main(void) {

        printf("Using Perl version %s from a C program!\n\n",
               CPR_eval("use Config; $Config{version};"));

        CPR_eval("use Data::Dumper;");
        CPR_eval("print Dumper \\%INC;");

        return 0;

    }

=item Discussion

By using CPR. (C Perl Run)

This example uses another Inline module, C<Inline::CPR>, available
separately on CPAN. When you install this module it also installs a
binary interpreter called C</usr/bin/cpr>. (The path may be different on
your system)

When you feed a C program to the CPR interpreter, it automatically
compiles and runs your code using Inline. This gives you full access to
the Perl internals. CPR also provides a set of easy to use C macros for
calling Perl internals.

This means that you can effectively "run" C source code by putting a CPR
hashbang as the first line of your C program.

=item See Also

See L<Inline::CPR> for more information on using CPR.

C<Inline::CPR> can be obtained from
http://search.cpan.org/search?dist=Inline-CPR

=item Credits

Randal Schwartz <merlyn@stonehenge.com>, Randolph Bentson
<bentson@grieg.holmsjoen.com>, Richard Anderson <starfire@zipcon.net>,
and Tim Maher <tim@consultix-inc.com> helped me figure out how to write
a program that would work as a hashbang.

=back

=cut --------------------------------------------------------------------------

=head1 Entertaining Guests

As of version 0.30, Inline has the ability to work in cooperation with
other modules that want to expose a C API of their own. The general
syntax for doing this is:

    use Inline with => 'Module';
    use Inline C => ... ;

This tells C<Module> to pass configuration options to Inline. Options
like typemaps, include paths, and external libraries, are all resolved
automatically so you can just concentrate on writing the functions.

=cut --------------------------------------------------------------------------

=head2 Event handling with Event.pm

=over 4

=item Problem

You need to write a C callback for the C<Event.pm> module. Can this be
done more easily with Inline?

=item Solution

    use Inline with => 'Event';

    Event->timer(desc     => 'Timer #1',
                 interval => 2,
                 cb       => \&my_callback,
                );

    Event->timer(desc     => 'Timer #2',
                 interval => 3,
                 cb       => \&my_callback,
                );

    print "Starting...\n";
    Event::loop;

    use Inline C => <<'END';
    void my_callback(pe_event* event) {
        pe_timer * watcher = event->up;

        printf("%s\n\tEvent priority = %d\n\tWatcher priority = %d\n\n",
               SvPVX(watcher->base.desc),
               event->prio,
               watcher->base.prio
              );
    }
    END

=item Discussion

The first line tells Inline to load the C<Event.pm> module. Inline then
queries C<Event> for configuration information. It gets the name and
location of Event's header files, typemaps and shared objects. The
parameters that C<Event> returns look like:

    INC => "-I $path/Event",
    TYPEMAPS => "$path/Event/typemap",
    MYEXTLIB => "$path/auto/Event/Event.$so",
    AUTO_INCLUDE => '#include "EventAPI.h"',
    BOOT => 'I_EVENT_API("Inline");',

Doing all of this automatically allows you, the programmer, to simply
write a function that receives a pointer of type C<'pe_event*'>. This
gives you access to the C<Event> structure that was passed to you.

In this example, I simply print values out of the structure. The Perl
code defines 2 timer events which each invoke the same callback. The
first one, every two seconds, and the second one, every three seconds.

As of this writing, C<Event.pm> is the only CPAN module that works in
cooperation with Inline.

=item See Also

Read the C<Event.pm> documentation for more information. It contains a
tutorial showing several examples of using Inline with C<Event>.

=item Credits

Jochen Stenzel <perl@jochen-stenzel.de> originally came up with the idea
of mixing Inline and C<Event>. He also authored the C<Event> tutorial.

Joshua Pritikin <joshua.pritikin@db.com> is the author of C<Event.pm>.

=back

=cut --------------------------------------------------------------------------

=head1 Food for Thought

=cut --------------------------------------------------------------------------

=head2 Calling C from both Perl and C

=over 4

=item Problem

I'd like to be able to call the same C function from both Perl and C.
Also I like to define a C function that B<doesn't> get bound to Perl.
How do I do that?

=item Solution

    print "9 + 5 = ", add(9, 5), "\n";
    print "SQRT(9^2 + 5^2) = ", pyth(9, 5), "\n";
    print "9 * 5 = ", mult(9, 5), "\n";

    use Inline C => <<'END_C';
    int add(int x, int y) {
        return x + y;
    }
    static int mult(int x, int y) {
        return x * y;
    }
    double pyth(int x, int y) {
        return sqrt(add(mult(x, x), mult(y, y)));
    }
    END_C

=item Discussion

The program produces:

    9 + 5 = 14
    SQRT(9^2 + 5^2) = 10.295630140987
    Can't locate auto/main/mult.al in @INC ...

Every Inline function that is bound to Perl is also callable by C. You
don't have to do anything special. Inline arranges it so that all the
typemap code gets done by XS and is out of sight. By the time the C
function receives control, everything has been converted from Perl to C.

Of course if your function manipulates the Perl Stack, you
probably don't want to call it from C (unless you I<really> know
what you're doing).

If you declare a function as C<static>, Inline won't bind it to Perl.
That's why we were able to call C<mult()> from C but the call failed
from Perl.

=item See Also

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Calling Perl from C

=over 4

=item Problem

So now that I can call C from Perl, how do I call a Perl subroutine from
an Inline C function.

=item Solution

    use Inline C;

    for(1..5) {
       c_func_1('This is the first line');
       c_func_2('This is the second line');
       print "\n";
    }

    sub perl_sub_1 {
        print map "$_\n", @_;
    }

    __DATA__
    __C__

    void c_func_2(SV* text) {
         dSP;

         ENTER;
         SAVETMPS;

         XPUSHs(sv_2mortal(newSVpvf("Plus an extra line")));
         PUTBACK;

         call_pv("perl_sub_1", G_DISCARD);

         FREETMPS;
         LEAVE;
    }

    void c_func_1(SV* text) {
         c_func_2(text);
    }


=item Discussion

This demo previously made use of Inline Stack macros only - but that's
not the correct way to do it. Instead, base the callbacks on the
perlcall documentation (as we're now doing).

Actually, this program demonstrates calling a C function which calls
another C function which in turn calls a Perl subroutine.

The nice thing about Inline C functions is that you can call them from
both Perl-space B<and> C-space. That's because Inline creates a wrapper
function around each C function. When you use Perl to call C you're
actually calling that function's wrapper. The wrapper handles
typemapping and Stack management, and then calls your C function.

The first time we call C<c_func_1> which calls C<c_func_2>. The second
time we call C<c_func_2> directly. C<c_func_2> calls the Perl subroutine
(C<perl_sub_1>) using the internal C<perl_call_pv> function. It has to
put arguments on the stack by hand. Since there is already one argument
on the stack when we enter the function, the C<XPUSHs> ( which is equivalent
to an C<Inline_Stack_Push> ) adds a second argument.

We iterate through a 'for' loop 5 times just to demonstrate that things
still work correctly when we do that. (This was where the previous
rendition, making use solely of Inline Stack macros, fell down.)

=item See Also

See L<Inline::C> for more information about Stack macros.

See L<perlapi> for more information about the Perl5 internal API.

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Evaling C

=over 4

=item Problem

I've totally lost my marbles and I want to generate C code at run time,
and C<eval> it into Perl. How do I do this?

=item Solution

    use Inline;
    use Code::Generator;

    my $c_code = generate('foo_function');

    Inline->bind(C => $c_code);

    foo_function(1, 2, 3);

=item Discussion

I can't think of a real life application where you would want to
generate C code on the fly, but at least I know how I would do it.
:)

The C<bind()> function of Inline let's you bind (compile/load/execute) C
functions at run time. It takes all of the same arguments as 'use Inline
C => ...'.

The nice thing is that once a particular snippet is compiled, it remains
cached so that it doesn't need to be compiled again. I can imagine that
someday a mad scientist will dream up a self generating modeling system
that would run faster and faster over time.

If you know such a person, have them drop me a line.

=item See Also

=item Credits

=back

=cut --------------------------------------------------------------------------

=head2 Accessing Fortran subs using Inline::C

=over 4

=item Problem

I've been given a neat little sub written in fortran that takes, as its
args, two integers and returns their product. And I would like to use
that sub as is from Inline::C. By way of example, let's say that the
fortran source file is named 'prod.f', and that it looks like this:

      integer function sqarea(r,s)
      integer r, s
      sqarea = r*s
      return
      end

=item Solution

We can't access that code directly, but we can compile it into a
library which we *can* then access from Inline::C.
Using gcc we could run:

  gfortran -c prod.f -o prod.o
  ar cru libprod.a prod.o

The function is then accessible as follows:

  use warnings;

  use Inline C => Config =>
    LIBS =>
     '-L/full/path/to/libprod_location -lprod -lgfortran';

  use Inline C => <<'  EOC';

  int wrap_sqarea(int a, int b) {
      return sqarea_(&a, &b);
  }

  EOC

  $x = 15;
  $y = $x + 3;
  $ret = wrap_sqarea($x, $y);
  print "Product of $x and $y is $ret\n";

=item Discussion

Note firstly that, although the function is specified as 'sqarea'
in the source file, gfortran appends an underscore to the name when
the source is compiled. (I don't know if *all* fortran compilers do
this.) Therefore Inline::C needs to call the function as 'sqarea_'.

Secondly, because fortran subs pass args by reference, we need to
pass the *addresses* of the two integer args to sqarea() when we call
it from our Inline::C sub.

If using g77 instead of gfortran, the only necessary change is that
we specify '-lg2c' instead of '-lgfortran' in our 'LIBS' setting.

=item See Also

=item Credits

=back

=cut --------------------------------------------------------------------------

=head1 SEE ALSO

For generic information about Inline, see L<Inline>.

For information about using Inline with C see L<Inline::C>.

For information on supported languages and platforms see
L<Inline-Support>.

For information on writing your own Inline language support module, see
L<Inline-API>.

Inline's mailing list is inline@perl.org

To subscribe, send email to inline-subscribe@perl.org

=head1 AUTHOR

Brian Ingerson <INGY@cpan.org>

=head1 COPYRIGHT

Copyright (c) 2001, 2002. Brian Ingerson.

Copyright (c) 2008, 2010-2012. Sisyphus.

All Rights Reserved. This module is free software. It may be
used, redistributed and/or modified under the terms of the Perl
Artistic License.

See http://www.perl.com/perl/misc/Artistic.html

=cut