@@ -115,14 +115,15 @@ Mytest directory.
115
115
The file Makefile.PL should look something like this:
116
116
117
117
use ExtUtils::MakeMaker;
118
+
118
119
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
119
120
# the contents of the Makefile that is written.
120
121
WriteMakefile(
121
- NAME => 'Mytest',
122
- VERSION_FROM => 'Mytest.pm', # finds $VERSION
123
- LIBS => [''], # e.g., '-lm'
124
- DEFINE => '', # e.g., '-DHAVE_SOMETHING'
125
- INC => '', # e.g., '-I/usr/include/other'
122
+ NAME => 'Mytest',
123
+ VERSION_FROM => 'Mytest.pm', # finds $VERSION
124
+ LIBS => [''], # e.g., '-lm'
125
+ DEFINE => '', # e.g., '-DHAVE_SOMETHING'
126
+ INC => '-I ', # e.g., '-I. -I/usr/include/other'
126
127
);
127
128
128
129
The file Mytest.pm should start with something like this:
@@ -276,9 +277,9 @@ when the test is correct, "not ok" when it is not.
276
277
# so read its man page ( perldoc Test::More ) for help writing this
277
278
# test script.
278
279
279
- is(& Mytest::is_even(0), 1);
280
- is(& Mytest::is_even(1), 0);
281
- is(& Mytest::is_even(2), 1);
280
+ is( Mytest::is_even(0), 1 );
281
+ is( Mytest::is_even(1), 0 );
282
+ is( Mytest::is_even(2), 1 );
282
283
283
284
We will be calling the test script through the command "C<make test>". You
284
285
should see output that looks something like this:
@@ -390,24 +391,40 @@ Add the following to the end of Mytest.xs:
390
391
391
392
Edit the Makefile.PL file so that the corresponding line looks like this:
392
393
393
- ' LIBS' => ['-lm'], # e.g., '-lm'
394
+ LIBS => ['-lm'], # e.g., '-lm'
394
395
395
396
Generate the Makefile and run make. Change the test number in Mytest.t to
396
397
"9" and add the following tests:
397
398
398
- $i = -1.5; &Mytest::round($i); is( $i, -2.0 );
399
- $i = -1.1; &Mytest::round($i); is( $i, -1.0 );
400
- $i = 0.0; &Mytest::round($i); is( $i, 0.0 );
401
- $i = 0.5; &Mytest::round($i); is( $i, 1.0 );
402
- $i = 1.2; &Mytest::round($i); is( $i, 1.0 );
399
+ my $i;
400
+
401
+ $i = -1.5;
402
+ Mytest::round($i);
403
+ is( $i, -2.0, 'Rounding -1.5 to -2.0' );
404
+
405
+ $i = -1.1;
406
+ Mytest::round($i);
407
+ is( $i, -1.0, 'Rounding -1.1 to -1.0' );
408
+
409
+ $i = 0.0;
410
+ Mytest::round($i);
411
+ is( $i, 0.0, 'Rounding 0.0 to 0.0' );
412
+
413
+ $i = 0.5;
414
+ Mytest::round($i);
415
+ is( $i, 1.0, 'Rounding 0.5 to 1.0' );
416
+
417
+ $i = 1.2;
418
+ Mytest::round($i);
419
+ is( $i, 1.0, 'Rounding 1.2 to 1.0' );
403
420
404
421
Running "C<make test>" should now print out that all nine tests are okay.
405
422
406
423
Notice that in these new test cases, the argument passed to round was a
407
424
scalar variable. You might be wondering if you can round a constant or
408
425
literal. To see what happens, temporarily add the following line to Mytest.t:
409
426
410
- & Mytest::round(3);
427
+ Mytest::round(3);
411
428
412
429
Run "C<make test>" and notice that Perl dies with a fatal error. Perl won't
413
430
let you change the value of constants!
@@ -534,7 +551,7 @@ In the mylib directory, create a file mylib.h that looks like this:
534
551
Also create a file mylib.c that looks like this:
535
552
536
553
#include <stdlib.h>
537
- #include "./ mylib.h"
554
+ #include "mylib.h"
538
555
539
556
double
540
557
foo(int a, long b, const char *c)
@@ -547,9 +564,9 @@ And finally create a file Makefile.PL that looks like this:
547
564
use ExtUtils::MakeMaker;
548
565
$Verbose = 1;
549
566
WriteMakefile(
550
- NAME => 'Mytest2::mylib',
551
- SKIP => [qw(all static static_lib dynamic dynamic_lib)],
552
- clean => {'FILES' => 'libmylib$(LIB_EXT)'},
567
+ NAME => 'Mytest2::mylib',
568
+ SKIP => [qw(all static static_lib dynamic dynamic_lib)],
569
+ clean => {'FILES' => 'libmylib$(LIB_EXT)'},
553
570
);
554
571
555
572
@@ -576,7 +593,7 @@ on Win32 systems.
576
593
We will now create the main top-level Mytest2 files. Change to the directory
577
594
above Mytest2 and run the following command:
578
595
579
- % h2xs -O -n Mytest2 ./ Mytest2/mylib/mylib.h
596
+ % h2xs -O -n Mytest2 Mytest2/mylib/mylib.h
580
597
581
598
This will print out a warning about overwriting Mytest2, but that's okay.
582
599
Our files are stored in Mytest2/mylib, and will be untouched.
@@ -587,12 +604,12 @@ will be generating a library in it. Let's add the argument MYEXTLIB to
587
604
the WriteMakefile call so that it looks like this:
588
605
589
606
WriteMakefile(
590
- ' NAME' => 'Mytest2',
591
- ' VERSION_FROM' => 'Mytest2.pm', # finds $VERSION
592
- ' LIBS' => [''], # e.g., '-lm'
593
- ' DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
594
- ' INC' => '', # e.g., '-I/usr/include/other'
595
- ' MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)',
607
+ NAME => 'Mytest2',
608
+ VERSION_FROM => 'Mytest2.pm', # finds $VERSION
609
+ LIBS => [''], # e.g., '-lm'
610
+ DEFINE => '', # e.g., '-DHAVE_SOMETHING'
611
+ INC => '', # e.g., '-I/usr/include/other'
612
+ MYEXTLIB => 'mylib/libmylib$(LIB_EXT)',
596
613
);
597
614
598
615
and then at the end add a subroutine (which will override the pre-existing
@@ -606,9 +623,7 @@ with "cd"!
606
623
';
607
624
}
608
625
609
- Let's also fix the MANIFEST file so that it accurately reflects the contents
610
- of our extension. The single line that says "mylib" should be replaced by
611
- the following three lines:
626
+ Let's also fix the MANIFEST file by appending the following three lines:
612
627
613
628
mylib/Makefile.PL
614
629
mylib/mylib.c
@@ -642,12 +657,12 @@ Now run perl on the top-level Makefile.PL. Notice that it also created a
642
657
Makefile in the mylib directory. Run make and watch that it does cd into
643
658
the mylib directory and run make in there as well.
644
659
645
- Now edit the Mytest2.t script and change the number of tests to "4 ",
660
+ Now edit the Mytest2.t script and change the number of tests to "5 ",
646
661
and add the following lines to the end of the script:
647
662
648
- is( & Mytest2::foo(1, 2, "Hello, world!"), 7 );
649
- is( & Mytest2::foo(1, 2, "0.0"), 7 );
650
- ok( abs(& Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 );
663
+ is( Mytest2::foo( 1, 2, "Hello, world!" ), 7 );
664
+ is( Mytest2::foo( 1, 2, "0.0" ), 7 );
665
+ ok( abs( Mytest2::foo( 0, 0, "-3.4" ) - 0.6 ) <= 0.01 );
651
666
652
667
(When dealing with floating-point comparisons, it is best to not check for
653
668
equality, but rather that the difference between the expected and actual
@@ -1017,9 +1032,12 @@ after the include of "XSUB.h":
1017
1032
Also add the following code segment to Mytest.t while incrementing the "9"
1018
1033
tests to "11":
1019
1034
1020
- @a = &Mytest::statfs("/blech");
1035
+ my @a;
1036
+
1037
+ @a = Mytest::statfs("/blech");
1021
1038
ok( scalar(@a) == 1 && $a[0] == 2 );
1022
- @a = &Mytest::statfs("/");
1039
+
1040
+ @a = Mytest::statfs("/");
1023
1041
is( scalar(@a), 7 );
1024
1042
1025
1043
=head2 New Things in this Example
@@ -1152,7 +1170,7 @@ Mytest.xs:
1152
1170
And add the following code to Mytest.t, while incrementing the "11"
1153
1171
tests to "13":
1154
1172
1155
- $results = Mytest::multi_statfs([ '/', '/blech' ]);
1173
+ my $results = Mytest::multi_statfs([ '/', '/blech' ]);
1156
1174
ok( ref $results->[0] );
1157
1175
ok( ! ref $results->[1] );
1158
1176
@@ -1246,21 +1264,24 @@ typeglobs and stuff. Well, it isn't.
1246
1264
Suppose that for some strange reason we need a wrapper around the
1247
1265
standard C library function C<fputs()>. This is all we need:
1248
1266
1249
- #define PERLIO_NOT_STDIO 0
1250
- #define PERL_NO_GET_CONTEXT
1251
- #include "EXTERN.h"
1252
- #include "perl.h"
1253
- #include "XSUB.h"
1267
+ #define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */
1268
+ #define PERL_NO_GET_CONTEXT /* This is more efficient */
1269
+ #include "EXTERN.h"
1270
+ #include "perl.h"
1271
+ #include "XSUB.h"
1254
1272
1255
- #include <stdio.h>
1273
+ #include <stdio.h>
1256
1274
1257
- int
1258
- fputs(s, stream)
1259
- char * s
1260
- FILE * stream
1275
+ int
1276
+ fputs(s, stream)
1277
+ char * s
1278
+ FILE * stream
1261
1279
1262
1280
The real work is done in the standard typemap.
1263
1281
1282
+ For more details, see
1283
+ L<perlapio/"Co-existence with stdio">.
1284
+
1264
1285
B<But> you lose all the fine stuff done by the perlio layers. This
1265
1286
calls the stdio function C<fputs()>, which knows nothing about them.
1266
1287
@@ -1382,7 +1403,7 @@ Some systems may have installed Perl version 5 as "perl5".
1382
1403
=head1 See also
1383
1404
1384
1405
For more information, consult L<perlguts>, L<perlapi>, L<perlxs>, L<perlmod>,
1385
- and L<perlpod>.
1406
+ L<perlapio>, and L<perlpod>
1386
1407
1387
1408
=head1 Author
1388
1409
@@ -1396,6 +1417,8 @@ by Nick Ing-Simmons.
1396
1417
1397
1418
Changes for h2xs as of Perl 5.8.x by Renee Baecker
1398
1419
1420
+ This document is now maintained as part of Perl itself.
1421
+
1399
1422
=head2 Last Changed
1400
1423
1401
- 2012-01-20
1424
+ 2020-10-05
0 commit comments