Skip to content

Commit 48668ec

Browse files
authored
Merge pull request #8 from cxw42/script-parms
Support passing parameters from the command line into scripts via `-s` and `%S`.
2 parents 510bfe2 + 6d9f913 commit 48668ec

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ all: test
44

55
test:
66
perl -e 'use Test::Harness "runtests"; runtests @ARGV;' -- t/*.t 2>/dev/null
7-
#Pure shell: for f in t/*.t ; do echo "$$f" ; perl "$$f" ; done 2>/dev/null
7+
8+
9+
#Note: if you don't have Test::Harness, you can use:
10+
# for f in t/*.t ; do echo "$$f" ; perl "$$f" ; done 2>/dev/null
811

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ No external modules are required, just a single file.
88
Usage: perl perlpp.pl [options] [filename]
99
Options:
1010
-o, --output filename Output to the file instead of STDOUT.
11+
-s, --set name=value Set $S{name}=value in the generated code.
12+
The hash %S always exists, but is empty
13+
if you haven't specified any -s options.
1114
-e, --eval expression Evaluate the expression(s) before any Perl code.
1215
-d, --debug Don't evaluate Perl code, just write it to STDERR.
1316
-h, --help Usage help.
@@ -124,7 +127,7 @@ the perl code produces will be included verbatim in the script output.
124127
This can be used to dynamically select which files you want to include,
125128
using
126129

127-
<?:macro Include "whatever_filename"; ?>
130+
<?:macro my $fn="some_name"; Include $fn; ?>
128131

129132
Capturing
130133
---------

perlpp.pl

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,20 @@ sub Main {
434434
StartOB();
435435
print "package PPP_${Package};\nuse strict;\nuse warnings;\n";
436436

437-
# TODO transfer parameters from the command line to the processed file.
438-
# Per commit 7bbe05c, %DEF is for those parameters.
439-
print "my %DEF = ();\n";
437+
# Transfer parameters from the command line (-s) to the processed file.
438+
# The parameters are in %S, by analogy with -s.
439+
print "my %S = (\n";
440+
for my $defname (keys %{$opts{DEFS}}) {
441+
print " $defname => ", ${$opts{DEFS}}{$defname}, ",\n";
442+
}
443+
print ");\n";
440444

445+
# Initial code from the command line, if any
441446
print $opts{EVAL}, "\n" if $opts{EVAL};
442447

448+
# The input file
443449
ProcessFile( $opts{INPUT_FILENAME} );
450+
444451
my $script = EndOB(); # The generated Perl script
445452

446453
if ( $opts{DEBUG} ) {
@@ -479,6 +486,24 @@ =head1 OPTIONS
479486
480487
Output to B<filename> instead of STDOUT.
481488
489+
=item -s, --set B<name>=B<value>
490+
491+
In the generated script, set C<< $S{B<name>} >> to B<value>.
492+
The hash C<%S> always exists, but is empty if no B<-s> options are
493+
given on the command line.
494+
495+
Note: If your shell strips quotes, you may need to escape them. B<value> must
496+
be a valid Perl expression. So, under bash, this works:
497+
498+
perlpp -s name=\"Hello, world!\"
499+
500+
The backslashes (C<\"> instead of C<">) are required to prevent bash
501+
from removing the double-quotes. Alternatively, this works:
502+
503+
perlpp -s 'name="Hello, World"'
504+
505+
with the whole argument to B<-s> in single quotes.
506+
482507
=item -e, --eval B<statement>
483508
484509
Evaluate the B<statement> before any other Perl code in the generated
@@ -492,6 +517,14 @@ =head1 OPTIONS
492517
493518
Usage help.
494519
520+
=item --man
521+
522+
Full documentation
523+
524+
=item -?, --usage
525+
526+
Shows just the usage summary
527+
495528
=back
496529
497530
=head1 COPYRIGHT

t/cmdline.t

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Tests of perlpp command-line options
33
use strict;
44
use warnings;
5-
use Test::More;
5+
use Test::More 'no_plan';
66
use IPC::Run3;
77
use constant CMD => 'perl perlpp.pl';
88

@@ -18,9 +18,18 @@ my @testcases=(
1818
['--eval \'my $foo=42;\'','<?= $foo ?>', qr/^42$/],
1919
['-d -e \'my $foo=42;\'','<?= $foo ?>', qr/^my \$foo=42;/m],
2020
['--debug --eval \'my $foo=42;\'','<?= $foo ?>', qr/^print\s+\$foo\s*;/m],
21+
['-s foo=1', '<?= $S{foo} ?>',qr/^1$/],
22+
['-s foo=\"blah\"', '<?= $S{foo} ?>',qr/^blah$/],
23+
# Have to escape the double-quotes so perl sees it as a string
24+
# literal instead of a bareword.
25+
['-s foo=42 -s bar=127', '<?= $S{foo} * $S{bar} ?>',qr/^5334$/],
26+
['', '<? $S{x}="%S always exists even if empty"; ?><?= $S{x} ?>',
27+
qr/^%S always exists even if empty$/],
2128
); #@testcases
2229

23-
plan tests => scalar @testcases;
30+
#plan tests => scalar @testcases;
31+
# TODO count the out_re and err_re in @testcases, since the number of
32+
# tests is the sum of those counts.
2433

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

0 commit comments

Comments
 (0)