Skip to content

Commit d6e73ca

Browse files
authored
Merge pull request #27 from cxw42/vnext
v0.500.4
2 parents 4328bde + 39d4f10 commit d6e73ca

File tree

13 files changed

+138
-57
lines changed

13 files changed

+138
-57
lines changed

Changes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Revision history for Text-PerlPP
22
(Note: GH = GitHub issue; # = RT issue)
33

4+
0.500.4 2018/05/29
5+
Updated tests
6+
7+
0.500.3 2018/05/28
8+
Fixes to tests; updated documentation
9+
410
0.500.2 2018/05/27
511
Fixes to installation package
612

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ t/04-include.t
1818
t/05-external-command.t
1919
t/06-macro.t
2020
t/07-invalid.t
21+
t/08-persistent-state.t
2122
t/a.txt
2223
t/b.txt
2324
t/c.txt

README

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ README.md.
1616

1717
REQUIRES
1818

19-
Perl 5.10+ and Getopt::Long 2.50. There is a fatpacked version that
19+
Perl 5.10.1+ and Getopt::Long 2.50. There is a fatpacked version that
2020
already includes Getopt::Long in the GitHub releases archive:
2121
https://github.com/interpreters/perlpp/releases
2222

@@ -45,13 +45,46 @@ Yet another alternative way of installing
4545
- Copy lib/Text/PerlPP.pm to a directory in your @INC
4646
- Copy bin/perlpp to a directory in your PATH.
4747

48+
DEVELOPING
49+
50+
We welcome contributions through the normal GitHub pull-request (PR) workflow.
51+
The build system is straight ExtUtils::MakeMaker.
52+
53+
Before developing, run
54+
55+
perl Makefile.PL
56+
cpanm --installdeps . # if you have cpanminus installed
57+
58+
Then, to test your code directly from the lib/ directory, run
59+
60+
make testhere
61+
62+
Before submitting a PR, please test with the normal
63+
64+
make ; make test
65+
66+
sequence, as well as with
67+
68+
make testhere
69+
make testpacked
70+
71+
That last one tests the packed version made by pack.PL in blib/perlpp.
72+
We test on p5p Perl 5.10.1 (cygwin x86) and 5.26.1 (cygwin x64),
73+
and cperl 5.26.2c (cygwin x64).
74+
4875
SUPPORT AND DOCUMENTATION
4976

5077
After installing, you can find documentation for this module with the
5178
perldoc command.
5279

80+
perldoc perlpp
81+
82+
describes syntax and invocation, while
83+
5384
perldoc Text::PerlPP
5485

86+
describes using perlpp within another program.
87+
5588
You can also look for information at:
5689

5790
GitHub (report bugs here)

README.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PerlPP: Perl preprocessor
44
Translates **Text+Perl** to **Text**.
55
It can be used for any kind of text templating, e.g. code generation.
66
No external modules are required, just a single file.
7-
Requires Perl 5.10+.
7+
Requires Perl 5.10.1+.
88

99
PerlPP runs in two passes: it generates a Perl script from your input, and then
1010
it runs the generated script. If you see `error at (eval ##)`
@@ -58,11 +58,11 @@ Perl code is included between `<?` and `?>` tags.
5858
There are several modes, indicated by the character after the `<?`:
5959

6060
<? code mode: Perl code is between the tags.
61-
<?= echo mode: prints a Perl expression
62-
<?: internal-command mode: executed by PerlPP itself (see below)
61+
<?= echo mode: prints a Perl expression.
62+
<?: internal-command mode: executed by PerlPP itself.
6363
<?/ code mode, beginning with printing a line break.
6464
<?# comment mode: everything in <?# ... ?> is ignored.
65-
<?! external mode: everything in <?! ... ?> is run as an external command
65+
<?! external mode: everything in <?! ... ?> is run as an external command.
6666

6767
The code mode is started by `<?` followed by any number of whitespaces
6868
or line breaks.
@@ -82,11 +82,11 @@ The Generated Script
8282

8383
The generated script:
8484

85-
- is in its own package, named based on the input filename
86-
- `use`s `5.010`, `strict`, and `warnings`
85+
- is in its own package, named based on the input filename and a unique number
86+
- `use`s `5.010001`, `strict`, and `warnings`
8787
- provides constants `true` (=`!!1`) and `false` (=`!!0`) (with `use constant`)
88-
- Declares `my %D` and initializes `%D` based on any **-D** options you provide
89-
- Declares `my %S` and initializes `%S` based on any **-s** options you provide
88+
- declares `my %D` and initializes `%D` based on any **-D** options you provide
89+
- declares `my %S` and initializes `%S` based on any **-s** options you provide
9090

9191
Other than that, everything in the script comes from your input file(s).
9292
Use the **-E** option to see the generated script.
@@ -159,7 +159,7 @@ So `<?/ ... ?>` is effectively a shorthand for `<? print "\n"; ... ?>`.
159159

160160
The example
161161

162-
<?!echo Howdy!?>
162+
<?! echo Howdy! ?>
163163

164164
produces the output
165165

@@ -203,18 +203,21 @@ In this case words like `fooSomeWord` will become `barSomeWord`.
203203

204204
will run `some_perl_code;` at the time of script generation. Whatever output
205205
the perl code produces will be included verbatim in the script output.
206+
Within `some_perl_code`, the current PerlPP instance is available as `$PSelf`.
207+
206208
This can be used to dynamically select which files you want to include,
207-
using the provided `Include()` function. For example:
209+
using the provided `Include()` method. For example:
208210

209-
<?:macro my $fn="some_name"; Include $fn; ?>
211+
<?:macro my $fn="some_name"; $PSelf->Include($fn); ?>
210212

211213
has the same effect as
212214

213215
<?:include some_name ?>
214216

215-
but `$fn` can be determined programmatically. Note that it is not currently
216-
possible to select the filename to `Include` based on defines set with **-D**,
217-
since those do not take effect until the script has been generated.
217+
but `$fn` can be determined programmatically. Note that defines set with
218+
**-D** or **-s** do not take effect effect until after the script has been
219+
generated, which is after the macro code runs. However, those are available
220+
as hashes `$PSelf->{Defs}` and `$PSelf->{Sets}` in macro code.
218221

219222
Capturing
220223
---------
@@ -298,15 +301,16 @@ Tests with `<?:if NAME ... ?>` and `<?:elsif NAME ... ?>` have two restrictions:
298301

299302
For example, `<?:if FOO eq "something" ?>` (note the whitespace before `?>`!)
300303
will work fine. However, if you want to test `(FOO+1)*3`, you will need
301-
to use the full Perl code.
304+
to use the full Perl code `<? if( (FOO+1)*3 == 42 ) { ... } ?>` instead of
305+
`<?:if ?>` and `<?:endif?>`.
302306

303307
Other Features
304308
--------------
305309

306310
### Custom Preprocessors
307311

308312
It's possible to create your own pre/post-processors in a `<?:macro ?>` block
309-
using `PerlPP::AddPreprocessor` and `PerlPP::AddPostprocessor`.
313+
using `$PSelf->AddPreprocessor` and `$PSelf->AddPostprocessor`.
310314
This feature is used in [BigBenBox](https://github.com/d-ash/BigBenBox) for
311315
generating code in the C programming language.
312316

@@ -329,14 +333,6 @@ and create corresponding *~/.vim/after/syntax/FILETYPE.vim*
329333

330334
FILETYPE can be determined with `:set ft?`
331335

332-
## Developing PerlPP
333-
334-
Perlpp should run on any Perl v5.10+. However, it does require `Getopt::Long`
335-
v2.50 or higher, so you might have to grab that from CPAN.
336-
To run the tests, you also need to grab `IPC::Run3`.
337-
338-
The `Makefile` just runs the tests in `t/`; there is no build step.
339-
340336
## Copyright
341337

342338
Distributed under the MIT license --- see

lib/Text/PerlPP.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package Text::PerlPP;
55

66
# Semantic versioning, packed per Perl rules. Must always be at least one
77
# digit left of the decimal, and six digits right of the decimal.
8-
our $VERSION = '0.500002';
8+
our $VERSION = '0.500004';
99

1010
use 5.010001;
1111
use strict;

t/02-basic.t

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use rlib 'lib';
44
use PerlPPTest;
55

6-
use IPC::Run3;
76
(my $whereami = __FILE__) =~ s/02-basic\.t$//;
87
#diag join(' ', 'File is', __FILE__, 'whereami', $whereami);
98

@@ -39,7 +38,7 @@ my @testcases=(
3938

4039
); #@testcases
4140

42-
plan tests => count_tests(\@testcases, 1, 2);
41+
plan tests => scalar count_tests(\@testcases, 1, 2);
4342

4443
for my $lrTest (@testcases) {
4544
my ($testin, $refout, $referr) = @$lrTest;

t/02-readme.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ RESULT
8888
[ '<?= "!" . "?>foo<?= 42 ?><?" . "bar" ?>', '!foo42bar' ],
8989
); #@testcases
9090

91-
plan tests => count_tests(\@testcases, 1, 2);
91+
plan tests => scalar count_tests(\@testcases, 1, 2);
9292

9393
for my $lrTest (@testcases) {
9494
my ($testin, $refout, $referr) = @$lrTest;

t/03-cmdline.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ $testcases->load('-v','',qr/\bversion\b/)->
101101

102102
; #$testcases
103103

104-
plan tests => count_tests($testcases->arr, 3, 4);
104+
plan tests => scalar count_tests($testcases->arr, 3, 4);
105105

106106
for my $lrTest (@{$testcases->arr}) {
107107
my ($where, $opts, $testin, $out_re, $err_re) = @$lrTest;

t/04-include.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ my @testcases=(
2929
"a\nb\nc\n"],
3030
); #@testcases
3131

32-
plan tests => count_tests(\@testcases, 2, 3);
32+
plan tests => scalar count_tests(\@testcases, 2, 3);
3333

3434
for my $lrTest (@testcases) {
3535
my ($lineno, $testin, $refout, $referr) = @$lrTest;

t/05-external-command.t

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#!/usr/bin/env perl
2-
# Tests of perlpp <?!...?> external commands
2+
# Tests of perlpp <?!...?> external commands.
33
#
4-
# TODO: On non-Unix, test only `echo` with no parameters.
4+
# Note: On non-Unix, we test only `echo` with no parameters.
5+
# On non-Unix, non-Windows, we skip this because yours truly doesn't know
6+
# enough about what external commands are available! :)
57

68
use rlib 'lib';
79
use PerlPPTest qw(:DEFAULT quote_string);
810
use List::Util 'any';
911

10-
if(any { $_ eq $^O } 'VMS', 'os390', 'os400', 'riscos', 'amigaos') {
11-
plan skip_all => "I don't know how to run this test on $^O";
12-
exit;
12+
my $os = $^O; # so we can mock $^O to test this test code!
13+
14+
if(any { $_ eq $os } 'VMS', 'os390', 'os400', 'riscos', 'amigaos') {
15+
plan skip_all => "I don't know how to run this test on $os";
1316
}
1417

1518
(my $whereami = __FILE__) =~ s/macro\.t$//;
@@ -38,26 +41,27 @@ do {
3841
is($out, "howdy\n", "basic echo");
3942
};
4043

41-
if (any { $_ eq $^O } 'dos', 'os2', 'MSWin') {
42-
skip "I don't know how to run the rest of the tests on $^O", $ntests-1;
43-
exit;
44-
}
44+
SKIP: {
45+
if (any { $_ eq $os } 'dos', 'os2', 'MSWin32') {
46+
skip "I don't know how to run the rest of the tests on $os", $ntests-1;
47+
}
4548

46-
for my $lrTest (@testcases) {
47-
my ($opts, $testin, $out_re, $err_re) = @$lrTest;
48-
my ($out, $err);
49+
for my $lrTest (@testcases) {
50+
my ($opts, $testin, $out_re, $err_re) = @$lrTest;
51+
my ($out, $err);
4952

50-
#diag "perlpp $opts <<<@{[quote_string $testin]}";
51-
run_perlpp $opts, \$testin, \$out, \$err;
53+
#diag "perlpp $opts <<<@{[quote_string $testin]}";
54+
run_perlpp $opts, \$testin, \$out, \$err;
5255

53-
if(defined $out_re) {
54-
like($out, $out_re);
55-
}
56-
if(defined $err_re) {
57-
like($err, $err_re);
58-
}
56+
if(defined $out_re) {
57+
like($out, $out_re);
58+
}
59+
if(defined $err_re) {
60+
like($err, $err_re);
61+
}
5962

60-
} # foreach test
63+
} # foreach test
64+
} #SKIP
6165

6266
# TODO test -o / --output, and processing input from files rather than stdin
6367

t/06-macro.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ my @testcases=(
2222

2323
); #@testcases
2424

25-
plan tests => count_tests(\@testcases, 2, 3);
25+
plan tests => scalar count_tests(\@testcases, 2, 3);
2626

2727
for my $lrTest (@testcases) {
2828
my ($opts, $testin, $out_re, $err_re) = @$lrTest;

t/08-persistent-state.t

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!perl
2+
# PerlPP: test persistence of state across calls to Main
3+
use rlib 'lib';
4+
use PerlPPTest;
5+
6+
# When testing perlpp as an external command, there is by definition no state
7+
# persistence. Therefore, skip this test file.
8+
if($ENV{PERLPP_NOUSE}) {
9+
plan skip_all => 'Persistent state not tested in this configuration (PERLPP_NOUSE)';
10+
}
11+
12+
plan tests => 3;
13+
14+
my ($in, $out, $err);
15+
my @ioe=\($in, $out, $err);
16+
my $instance = Text::PerlPP->new;
17+
18+
$in = '';
19+
run_perlpp {instance=>$instance, args=>['-D','foo=42']}, @ioe;
20+
is($out, '', 'first call returns nothing');
21+
is($err, '', 'first call succeeds');
22+
23+
$in = 'foo';
24+
run_perlpp {instance=>$instance}, @ioe;
25+
is($out, '42', 'definition carries forward');
26+
27+
done_testing();
28+
# vi: set ts=4 sts=4 sw=4 et ai: #

t/lib/PerlPPTest.pm

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,29 @@ sub L {
3838
} #L
3939

4040
# run_perlpp: Run perlpp
41-
# Args: $lrArgs, $refStdin, $refStdout, $refStderr
41+
# Args: $args, $refStdin, $refStdout, $refStderr
42+
# $args can be: a string, in which case it is split with shellwords();
43+
# an arrayref, in which case it is used as the args; or
44+
# a hashref with (all optional):
45+
# {args} string or arrayref as above
46+
# {instance} an existing Text::PerlPP instance to use
4247
sub run_perlpp {
43-
#say STDERR "args ", Dumper(\@_);
48+
#say STDERR "run_perlpp: ", Dumper(\@_);
4449
my $lrArgs = shift;
50+
my $instance;
51+
if(ref $lrArgs eq 'HASH') {
52+
$instance = $lrArgs->{instance}; # nonexistent => falsy, so it's OK
53+
$lrArgs = $lrArgs->{args}; # or undef, which will become [] below.
54+
#say STDERR "args updated: ", Dumper($lrArgs);
55+
}
56+
4557
my $refStdin = shift // \(my $nullstdin);
4658
my $refStdout = shift // \(my $nullstdout);
4759
my $refStderr = shift // \(my $nullstderr);
4860

4961
my $retval;
5062

51-
$lrArgs = [shellwords($lrArgs)] if ref $lrArgs ne 'ARRAY';
63+
$lrArgs = [shellwords($lrArgs // '')] if ref $lrArgs ne 'ARRAY';
5264
#do { (my $args = Dumper($lrArgs)) =~ s/^/##/gm;
5365
#say STDERR "## args:\n$args"; };
5466

@@ -76,6 +88,8 @@ sub run_perlpp {
7688

7789
} else { # Run perl code under this perl
7890
#say STDERR "# running perlpp internal";
91+
$instance = Text::PerlPP->new unless $instance;
92+
7993
#say STDERR "# redirecting stdin";
8094
open local(*STDIN), '<', $refStdin or die $!;
8195
#say STDERR "# redirected stdin";
@@ -86,7 +100,7 @@ sub run_perlpp {
86100
($$refStdout, $$refStderr, @result) = capture {
87101
# Thanks to http://www.perlmonks.org/bare/?node_id=289391 by Zaxo
88102
#say STDERR "# running perlpp";
89-
my $result = Text::PerlPP->new->Main($lrArgs);
103+
my $result = $instance->Main($lrArgs);
90104
#say STDERR "# done running perlpp";
91105
$result;
92106
};

0 commit comments

Comments
 (0)