Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c389fd8

Browse files
authoredDec 23, 2020
GH #18163: Stricter, cleaned up test example: (#18165)
* GH #18163: Stricter, cleaned up test example: This just adds 'my $i' to make the test pass on strict, and it cleans it up and provides test names for the tests. I kept the tabs that were used. * Use relative path * Indent for readability * Missing variables * Add another reference * Remove '&' in function calls * Add some hints * Normalize quoting in WriteMakeFile examples * Remove explicit quotes * Apply all additional suggestions * replace tabs, make line shorter
1 parent d296ead commit c389fd8

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed
 

‎dist/ExtUtils-ParseXS/lib/perlxstut.pod

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ Mytest directory.
115115
The file Makefile.PL should look something like this:
116116

117117
use ExtUtils::MakeMaker;
118+
118119
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
119120
# the contents of the Makefile that is written.
120121
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'
126127
);
127128

128129
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.
276277
# so read its man page ( perldoc Test::More ) for help writing this
277278
# test script.
278279

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 );
282283

283284
We will be calling the test script through the command "C<make test>". You
284285
should see output that looks something like this:
@@ -390,24 +391,40 @@ Add the following to the end of Mytest.xs:
390391

391392
Edit the Makefile.PL file so that the corresponding line looks like this:
392393

393-
'LIBS' => ['-lm'], # e.g., '-lm'
394+
LIBS => ['-lm'], # e.g., '-lm'
394395

395396
Generate the Makefile and run make. Change the test number in Mytest.t to
396397
"9" and add the following tests:
397398

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' );
403420

404421
Running "C<make test>" should now print out that all nine tests are okay.
405422

406423
Notice that in these new test cases, the argument passed to round was a
407424
scalar variable. You might be wondering if you can round a constant or
408425
literal. To see what happens, temporarily add the following line to Mytest.t:
409426

410-
&Mytest::round(3);
427+
Mytest::round(3);
411428

412429
Run "C<make test>" and notice that Perl dies with a fatal error. Perl won't
413430
let you change the value of constants!
@@ -534,7 +551,7 @@ In the mylib directory, create a file mylib.h that looks like this:
534551
Also create a file mylib.c that looks like this:
535552

536553
#include <stdlib.h>
537-
#include "./mylib.h"
554+
#include "mylib.h"
538555

539556
double
540557
foo(int a, long b, const char *c)
@@ -547,9 +564,9 @@ And finally create a file Makefile.PL that looks like this:
547564
use ExtUtils::MakeMaker;
548565
$Verbose = 1;
549566
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)'},
553570
);
554571

555572

@@ -576,7 +593,7 @@ on Win32 systems.
576593
We will now create the main top-level Mytest2 files. Change to the directory
577594
above Mytest2 and run the following command:
578595

579-
% h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h
596+
% h2xs -O -n Mytest2 Mytest2/mylib/mylib.h
580597

581598
This will print out a warning about overwriting Mytest2, but that's okay.
582599
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
587604
the WriteMakefile call so that it looks like this:
588605

589606
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)',
596613
);
597614

598615
and then at the end add a subroutine (which will override the pre-existing
@@ -606,9 +623,7 @@ with "cd"!
606623
';
607624
}
608625

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:
612627

613628
mylib/Makefile.PL
614629
mylib/mylib.c
@@ -642,12 +657,12 @@ Now run perl on the top-level Makefile.PL. Notice that it also created a
642657
Makefile in the mylib directory. Run make and watch that it does cd into
643658
the mylib directory and run make in there as well.
644659

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",
646661
and add the following lines to the end of the script:
647662

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 );
651666

652667
(When dealing with floating-point comparisons, it is best to not check for
653668
equality, but rather that the difference between the expected and actual
@@ -1017,9 +1032,12 @@ after the include of "XSUB.h":
10171032
Also add the following code segment to Mytest.t while incrementing the "9"
10181033
tests to "11":
10191034

1020-
@a = &Mytest::statfs("/blech");
1035+
my @a;
1036+
1037+
@a = Mytest::statfs("/blech");
10211038
ok( scalar(@a) == 1 && $a[0] == 2 );
1022-
@a = &Mytest::statfs("/");
1039+
1040+
@a = Mytest::statfs("/");
10231041
is( scalar(@a), 7 );
10241042

10251043
=head2 New Things in this Example
@@ -1152,7 +1170,7 @@ Mytest.xs:
11521170
And add the following code to Mytest.t, while incrementing the "11"
11531171
tests to "13":
11541172

1155-
$results = Mytest::multi_statfs([ '/', '/blech' ]);
1173+
my $results = Mytest::multi_statfs([ '/', '/blech' ]);
11561174
ok( ref $results->[0] );
11571175
ok( ! ref $results->[1] );
11581176

@@ -1246,21 +1264,24 @@ typeglobs and stuff. Well, it isn't.
12461264
Suppose that for some strange reason we need a wrapper around the
12471265
standard C library function C<fputs()>. This is all we need:
12481266

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"
12541272

1255-
#include <stdio.h>
1273+
#include <stdio.h>
12561274

1257-
int
1258-
fputs(s, stream)
1259-
char * s
1260-
FILE * stream
1275+
int
1276+
fputs(s, stream)
1277+
char * s
1278+
FILE * stream
12611279

12621280
The real work is done in the standard typemap.
12631281

1282+
For more details, see
1283+
L<perlapio/"Co-existence with stdio">.
1284+
12641285
B<But> you lose all the fine stuff done by the perlio layers. This
12651286
calls the stdio function C<fputs()>, which knows nothing about them.
12661287

@@ -1382,7 +1403,7 @@ Some systems may have installed Perl version 5 as "perl5".
13821403
=head1 See also
13831404

13841405
For more information, consult L<perlguts>, L<perlapi>, L<perlxs>, L<perlmod>,
1385-
and L<perlpod>.
1406+
L<perlapio>, and L<perlpod>
13861407

13871408
=head1 Author
13881409

@@ -1396,6 +1417,8 @@ by Nick Ing-Simmons.
13961417

13971418
Changes for h2xs as of Perl 5.8.x by Renee Baecker
13981419

1420+
This document is now maintained as part of Perl itself.
1421+
13991422
=head2 Last Changed
14001423

1401-
2012-01-20
1424+
2020-10-05

0 commit comments

Comments
 (0)
Please sign in to comment.