App-pltest
view release on metacpan or search on metacpan
pod/examples.pod view on Meta::CPAN
You can also return a sub-hash, of only the keys matching any regexps you
pass:
pltest 'echo Config "random", qr/stream/'
=back
=head2 Tables
=over
=item Number Bases
Perl natively handles the 4 different bases common to programming. If you
want to list them side by side, simply quadruple them and C<f(orm)> them with
the 4 corresponding formats. Note the alternate parameter index syntax "1:":
pltest 'form "0b%08b 0%1:03o %1:3d 0x%1:02x"
for 0..0xff'
That makes a rather long table. You can get a better overview with 4 nested
tables, looping over a preset C<@A(RGV)>:
pltest -OA '0..0x3f' 'say join "\t",
map Form( "0b%08b 0%1:03o %1:3d 0x%1:02x", $ARGV+$_ ), 0, 0x40, 0x80, 0xc0'
If you prefer to enumerate sideways, it's easier. C<@A(RGV)> is in the right
order so we can loop in chunks of 4:
pltest -O4 -A '0..0xff' 'form join( "\t", map "0b%08b 0%$_:03o %$_:3d 0x%$_:02x", 1..4 ), @$ARGV'
=item ISO Paper Sizes
I<ISO replaced 8 standards by one. Now we have 9 standards. :-(>
Since C<@A(RGV)> is initially empty, B<-A> isn't looping. So we can do other
things as a side effect, rather than in a separate B<-B>. Use Perl's lovely
list assignment to swap and alternately halve the numbers. Because halving
happens before echoing, start with double size:
pltest -oA '($w, $h) = (1189, 1682); 0..10' \
'form "A%-2d %4dmm x %4dmm", $_, ($w, $h) = ($h / 2, $w)'
The table could easily be widened to cover B- & C-formats, by extending each
list of 2, to a corresponding list of 6, e.g. C<($Aw, $Ah, $Bw, ...)>. But a
more algorithmic approach seems better. The format is tripled (with cheat
spaces at the beginning). The main program loops over C<@A(RGV)>, thanks to
B<-O>, doing the same as above, but on anonymous elements of C<@d>, which got
set as a side effect of B<-A>:
pltest -OA '@d = (["A", 1189, 1682], ["B", 1414, 2000], ["C", 1297, 1834]); 0..10' \
'form " %3s %4dmm x %4dmm" x 3,
map +("$$_[0]$ARGV", ($$_[1], $$_[2]) = ($$_[2] / 2, $$_[1])), @d'
=item ANSI Background;Foreground Color Table
I<What color is a mirror? It depends whom you ask. ;-)>
You get numbers to fill into C<"\e[BGm">, C<"\e[FGm"> or C<"\e[BG;FGm"> to get
a color and close it with C<"\e[m">. There are twice twice 8 different colors
for dim & bright and for background & foreground. Hence the multiplication of
escape codes and of values to fill them.
This fills C<@A(RGV)> in B<-A> twice, the 2nd time looping over the 1st list,
as though it had been given on the command line. It maps it to the 16-fold
number format to print the header, swallowing every other number with 0-width.
Then the main program loops over it pairwise with C<$A(RGV)>, thanks to
B<-o2>, to print the body. Numbers are duplicated with C<(N)x2>, once to go
into the escape sequence, once to be displayed:
pltest -o2A 1..8 -A '$_, $_+39, $_+8, $_+99' -B 'form "co: bg;fg"."%4d%.0s"x16, @ARGV;
$b = Form "\e[%dm%3d "x16, map +(($_)x2, ($_+60)x2), 30..37' \
'form "%2d: %4d; \e[%2:dm$b\e[m", @$_'
=item Terminal Rulers
If you need a ruler to better count the width of some other output, you can
print out one of the following. These are either decimal, by groups of 5 or
hex by groups of 4 and 8. The latter two do the same. But instead of one
static ruler of length 100 or 256, they repeat and adapt. Depending on your
terminal emulator and Shell, the variable C<$COLUMNS> may track the current
width. If so, pass it in a single argument to "loop" over with B<-o>, else
provide the desired width yourself:
pltest 'say map "$_...:.....", 0..9'
pltest 'say map "$_..:...|...:....", 0..9, "A".."F"'
pltest -o 'say substr join( "", map "$_...:.....", 0..9 ) x ($_ / 100 + 1), 0, $_' $COLUMNS
pltest -o 'say substr join( "", map "$_..:...|...:....", 0..9, "A".."F" ) x ($_ / 256 + 1), 0, $_' $COLUMNS
=back
=head2 Math
=over
=item Minimum and Maximum
The C<List::Util> functions C<min> and C<max> are imported for you:
pltest 'echo max 1..5'
If you have just several numbers on each line and want their minimums, you can
autosplit (B<-a>) to C<@F(IELD)>:
pltest -a 'echo min @FIELD' file*
If on the same you just want the overall minimum, you can use the print-at-end
variable C<$R(ESULT)>, which you initialise to infinity in a B<-B> begin
program:
pltest -aB '$RESULT = "inf"' '$RESULT = min $RESULT, @FIELD' file*
Likewise for overall maximum, you start with negative infinity:
pltest -aB '$RESULT = "-inf"' '$RESULT = max $RESULT, @FIELD' file*
=item Median, Quartiles, Percentiles
The median is the number where half the list is less and half is greater.
Similarly the 1st quartile is where 25% are less and the 3rd where 25% are
greater. Use a list slice to extract these 3 and a 97th percentile, by
( run in 2.933 seconds using v1.01-cache-2.11-cpan-f56aa216473 )