Name
Data::Table::Text - Write data in tabular text format.
Synopsis
use Data::Table::Text;
# Print a table:
my $d =
[[qq(a), qq(b\nbb), qq(c\ncc\nccc\n)],
[qq(1), qq(1\n22), qq(1\n22\n333\n)],
];
my $t = formatTable($d, [qw(A BB CCC)]);
ok $t eq <<END;
A BB CCC
1 a b c
bb cc
ccc
2 1 1 1
22 22
333
END
# Print a table containing tables and make it into a report:
my $D = [[qq(See the\ntable\nopposite), $t],
[qq(Or\nthis\none), $t],
];
my $T = formatTable($D, [qw(Description Table)], head=><<END);
Table of Tables.
Table has NNNN rows each of which contains a table.
END
ok $T eq <<END;
Table of Tables.
Table has 2 rows each of which contains a table.
Description Table
1 See the A BB CCC
table 1 a b c
opposite bb cc
ccc
2 1 1 1
22 22
333
2 Or A BB CCC
this 1 a b c
one bb cc
ccc
2 1 1 1
22 22
333
END
# Print an array of arrays:
my $aa = formatTable
([[qw(A B C )],
[qw(AA BB CC )],
[qw(AAA BBB CCC)],
[qw(1 22 333)]],
[qw (aa bb cc)]);
ok $aa eq <<END;
aa bb cc
1 A B C
2 AA BB CC
3 AAA BBB CCC
4 1 22 333
END
# Print an array of hashes:
my $ah = formatTable
([{aa=> "A", bb => "B", cc => "C" },
{aa=> "AA", bb => "BB", cc => "CC" },
{aa=> "AAA", bb => "BBB", cc => "CCC" },
{aa=> 1, bb => 22, cc => 333 }]);
ok $ah eq <<END;
aa bb cc
1 A B C
2 AA BB CC
3 AAA BBB CCC
4 1 22 333
END
# Print a hash of arrays:
my $ha = formatTable
({"" => ["aa", "bb", "cc"],
"1" => ["A", "B", "C"],
"22" => ["AA", "BB", "CC"],
"333" => ["AAA", "BBB", "CCC"],
"4444" => [1, 22, 333]},
[qw(Key A B C)]
);
ok $ha eq <<END;
Key A B C
aa bb cc
1 A B C
22 AA BB CC
333 AAA BBB CCC
4444 1 22 333
END
# Print a hash of hashes:
my $hh = formatTable
({a => {aa=>"A", bb=>"B", cc=>"C" },
aa => {aa=>"AA", bb=>"BB", cc=>"CC" },
aaa => {aa=>"AAA", bb=>"BBB", cc=>"CCC" },
aaaa => {aa=>1, bb=>22, cc=>333 }});
ok $hh eq <<END;
aa bb cc
a A B C
aa AA BB CC
aaa AAA BBB CCC
aaaa 1 22 333
END
# Print an array of scalars:
my $a = formatTable(["a", "bb", "ccc", 4], [q(#), q(Col)]);
ok $a eq <<END;
# Col
0 a
1 bb
2 ccc
3 4
END
# Print a hash of scalars:
my $h = formatTable({aa=>"AAAA", bb=>"BBBB", cc=>"333"}, [qw(Key Title)]);
ok $h eq <<END;
Key Title
aa AAAA
bb BBBB
cc 333
END
Description
Write data in tabular text format.
The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.
Time stamps
Date and timestamps as used in logs of long running commands.
dateTimeStamp()
Year-monthNumber-day at hours:minute:seconds
Example:
ok 𝗱𝗮𝘁𝗲𝗧𝗶𝗺𝗲𝗦𝘁𝗮𝗺𝗽 =~ m(\A\d{4}-\d\d-\d\d at \d\d:\d\d:\d\d\Z);
dateTimeStampName()
Date time stamp without white space.
Example:
ok 𝗱𝗮𝘁𝗲𝗧𝗶𝗺𝗲𝗦𝘁𝗮𝗺𝗽𝗡𝗮𝗺𝗲 =~ m(\A_on_\d{4}_\d\d_\d\d_at_\d\d_\d\d_\d\d\Z);
dateStamp()
Year-monthName-day
Example:
ok 𝗱𝗮𝘁𝗲𝗦𝘁𝗮𝗺𝗽 =~ m(\A\d{4}-\w{3}-\d\d\Z);
versionCode()
YYYYmmdd-HHMMSS
Example:
ok 𝘃𝗲𝗿𝘀𝗶𝗼𝗻𝗖𝗼𝗱𝗲 =~ m(\A\d{8}-\d{6}\Z);
versionCodeDashed()
YYYY-mm-dd-HH:MM:SS
Example:
ok 𝘃𝗲𝗿𝘀𝗶𝗼𝗻𝗖𝗼𝗱𝗲𝗗𝗮𝘀𝗵𝗲𝗱 =~ m(\A\d{4}-\d\d-\d\d-\d\d:\d\d:\d\d\Z);
timeStamp()
hours:minute:seconds
Example:
ok 𝘁𝗶𝗺𝗲𝗦𝘁𝗮𝗺𝗽 =~ m(\A\d\d:\d\d:\d\d\Z);
microSecondsSinceEpoch()
Micro seconds since unix epoch.
Example:
ok 𝗺𝗶𝗰𝗿𝗼𝗦𝗲𝗰𝗼𝗻𝗱𝘀𝗦𝗶𝗻𝗰𝗲𝗘𝗽𝗼𝗰𝗵 > 47*365*24*60*60*1e6;
Command execution
Various ways of processing commands.
xxx(@)
Execute a shell command. The command to execute is specified as one or more strings which are joined together after removing any new lines. Optionally the last string can be a regular expression that is used to test the output generated by the execution the command: if the regular expression fails the command output is printed, else it is suppressed as being uninteresting.
Parameter Description
1 @cmd Command to execute followed by an optional regular expression to test the results
Example:
{ok 𝘅𝘅𝘅("echo aaa") =~ /aaa/;
yyy($)
Execute a block of shell commands line by line after removing comments - stop if there is a non zero return code from any command.
Parameter Description
1 $cmd Commands to execute separated by new lines
Example:
ok !𝘆𝘆𝘆 <<END;
echo aaa
echo bbb
END
zzz($$$$)
Execute lines of commands after replacing new lines with && then check that the pipeline execution results in a return code of zero and that the execution results match the optional regular expression if one has been supplied; confess() to an error if either check fails.
Parameter Description
1 $cmd Commands to execute - one per line with no trailing &&
2 $success Optional regular expression to check for acceptable results
3 $returnCode Optional regular expression to check the acceptable return codes
4 $message Message of explanation if any of the checks fail
Example:
ok 𝘇𝘇𝘇(<<END, qr(aaa\s*bbb)s);
echo aaa
echo bbb
END
parseCommandLineArguments(&$$)
Classify the specified array of words referred to by $args into positional and keyword parameters, call the specified sub with a reference to an array of positional parameters followed by a reference to a hash of keywords and their values then return the value returned by this sub. The keywords names will be validated if $valid is either a reference to an array of valid keywords names or a hash of valid keyword names => textual descriptions. Confess with a table of valid keywords definitions if the $valid keywords are specified and an invalid one is presented.
Parameter Description
1 $sub Sub to call
2 $args List of arguments to parse
3 $valid Optional list of valid parameters else all parameters will be accepted
Example:
my $r = 𝗽𝗮𝗿𝘀𝗲𝗖𝗼𝗺𝗺𝗮𝗻𝗱𝗟𝗶𝗻𝗲𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝘀 {[@_]}
[qw( aaa bbb -c --dd --eee=EEEE -f=F), q(--gg=g g), q(--hh=h h)];
is_deeply $r,
[["aaa", "bbb"],
{c=>undef, dd=>undef, eee=>"EEEE", f=>"F", gg=>"g g", hh=>"h h"},
];
call(&@)
Call the specified sub in a separate process, wait for it to complete, copy back the named our variables, free the memory used.
Parameter Description
1 $sub Sub to call
2 @our Our variable names with preceding sigils to copy back
Example:
ˢ{our $a = q(1);
our @a = qw(1);
our %a = (a=>1);
our $b = q(1);
for(2..4) {
𝗰𝗮𝗹𝗹 {$a = $_ x 1000; $a[0] = $_; $a{a} = $_; $b = 2;} qw($a @a %a);
ok $a == $_ x 1000;
ok $a[0] == $_;
ok $a{a} == $_;
ok $b == 1;
}
};
Files and paths
Operations on files and paths.
Statistics
Information about each file.
fileSize($)
Get the size of a file.
Parameter Description
1 $file File name
Example:
my $f = writeFile("zzz.data", "aaa");
ok 𝗳𝗶𝗹𝗲𝗦𝗶𝘇𝗲($f) == 3;
fileModTime($)
Get the modified time of a file in seconds since the epoch.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗶𝗹𝗲𝗠𝗼𝗱𝗧𝗶𝗺𝗲($0) =~ m(\A\d+\Z)s;
fileOutOfDate(&$@)
Calls the specified sub once for each source file that is missing, then calls the sub for the target if there were any missing files or if the target is older than any of the non missing source files or if the target does not exist. The file name is passed to the sub each time in $_. Returns the files to be remade in the order they should be made.
Parameter Description
1 $make Make with this sub
2 $target Target file
3 @source Source files
Example:
if (0) {
my @Files = qw(a b c);
my @files = (@Files, qw(d));
writeFile($_, $_), sleep 1 for @Files;
my $a = '';
my @a = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {$a .= $_} q(a), @files;
ok $a eq 'da';
is_deeply [@a], [qw(d a)];
my $b = '';
my @b = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {$b .= $_} q(b), @files;
ok $b eq 'db';
is_deeply [@b], [qw(d b)];
my $c = '';
my @c = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {$c .= $_} q(c), @files;
ok $c eq 'dc';
is_deeply [@c], [qw(d c)];
my $d = '';
my @d = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {$d .= $_} q(d), @files;
ok $d eq 'd';
is_deeply [@d], [qw(d)];
my @A = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {} q(a), @Files;
my @B = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {} q(b), @Files;
my @C = 𝗳𝗶𝗹𝗲𝗢𝘂𝘁𝗢𝗳𝗗𝗮𝘁𝗲 {} q(c), @Files;
is_deeply [@A], [qw(a)];
is_deeply [@B], [qw(b)];
is_deeply [@C], [];
unlink for @Files;
}
firstFileThatExists(@)
Returns the name of the first file that exists or undef if none of the named files exist.
Parameter Description
1 @files Files to check
Example:
my $d = temporaryFolder;
ok $d eq 𝗳𝗶𝗿𝘀𝘁𝗙𝗶𝗹𝗲𝗧𝗵𝗮𝘁𝗘𝘅𝗶𝘀𝘁𝘀("$d/$d", $d);
Components
File names and components.
Fusion
Create file names from file name components.
filePath(@)
Create a file name from an array of file name components. If all the components are blank then a blank file name is returned. Identical to fpf.
Parameter Description
1 @file File name components
Example:
if (1) {
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵 (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok filePathDir(qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok filePathDir('', qw(aaa)) eq "aaa/";
ok filePathDir('') eq "";
ok filePathExt(qw(aaa xxx)) eq "aaa.xxx";
ok filePathExt(qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
ok fpd (qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok fpf (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok fpe (qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
}
fpf is a synonym for filePath.
filePathDir(@)
Create a directory name from an array of file name components. If all the components are blank then a blank file name is returned. Identical to fpd.
Parameter Description
1 @file Directory name components
Example:
if (1) {
ok filePath (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵𝗗𝗶𝗿(qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵𝗗𝗶𝗿('', qw(aaa)) eq "aaa/";
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵𝗗𝗶𝗿('') eq "";
ok filePathExt(qw(aaa xxx)) eq "aaa.xxx";
ok filePathExt(qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
ok fpd (qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok fpf (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok fpe (qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
}
fpd is a synonym for filePathDir.
filePathExt(@)
Create a file name from an array of file name components the last of which is an extension. Identical to fpe.
Parameter Description
1 @File File name components and extension
Example:
if (1) {
ok filePath (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok filePathDir(qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok filePathDir('', qw(aaa)) eq "aaa/";
ok filePathDir('') eq "";
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵𝗘𝘅𝘁(qw(aaa xxx)) eq "aaa.xxx";
ok 𝗳𝗶𝗹𝗲𝗣𝗮𝘁𝗵𝗘𝘅𝘁(qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
ok fpd (qw(/aaa bbb ccc ddd)) eq "/aaa/bbb/ccc/ddd/";
ok fpf (qw(/aaa bbb ccc ddd.eee)) eq "/aaa/bbb/ccc/ddd.eee";
ok fpe (qw(aaa bbb xxx)) eq "aaa/bbb.xxx";
}
fpe is a synonym for filePathExt.
Fission
Get file name components from file names.
fp($)
Get path from file name.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗽 (q(a/b/c.d.e)) eq q(a/b/);
fpn($)
Remove extension from file name.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗽𝗻(q(a/b/c.d.e)) eq q(a/b/c.d);
fn($)
Remove path and extension from file name.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗻 (q(a/b/c.d.e)) eq q(c.d);
fne($)
Remove path from file name.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗻𝗲(q(a/b/c.d.e)) eq q(c.d.e);
fe($)
Get extension of file name.
Parameter Description
1 $file File name
Example:
ok 𝗳𝗲 (q(a/b/c.d.e)) eq q(e);
checkFile($)
Return the name of the specified file if it exists, else confess the maximum extent of the path that does exist.
Parameter Description
1 $file File to check
Example:
my $d = filePath (my @d = qw(a b c d));
my $f = filePathExt(qw(a b c d e x));
my $F = filePathExt(qw(a b c e d));
createEmptyFile($f);
ok 𝗰𝗵𝗲𝗰𝗸𝗙𝗶𝗹𝗲($d);
ok 𝗰𝗵𝗲𝗰𝗸𝗙𝗶𝗹𝗲($f);
quoteFile($)
Quote a file name.
Parameter Description
1 $file File name
Example:
ok 𝗾𝘂𝗼𝘁𝗲𝗙𝗶𝗹𝗲(fpe(qw(a "b" c))) eq q("a/\"b\".c");
removeFilePrefix($@)
Removes a file prefix from an array of files.
Parameter Description
1 $prefix File prefix
2 @files Array of file names
Example:
is_deeply [qw(a b)], [&𝗿𝗲𝗺𝗼𝘃𝗲𝗙𝗶𝗹𝗲𝗣𝗿𝗲𝗳𝗶𝘅(qw(a/ a/a a/b))];
is_deeply [qw(b)], [&𝗿𝗲𝗺𝗼𝘃𝗲𝗙𝗶𝗹𝗲𝗣𝗿𝗲𝗳𝗶𝘅("a/", "a/b")];
titleToUniqueFileName($$$$)
Create a file name from a title that is unique within the set %uniqueNames.
Parameter Description
1 $uniqueFileNames Unique file names hash {} which will be updated by this method
2 $title Title
3 $suffix File name suffix
4 $ext File extension
Example:
ˢ{my $f = {};
ok q(a_p.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_p_2.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_p_3.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_q.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
ok q(a_q_5.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
ok q(a_q_6.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
};
ok q(a_p.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_p_2.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_p_3.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a p txt));
ok q(a_q.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
ok q(a_q_5.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
ok q(a_q_6.txt) eq &𝘁𝗶𝘁𝗹𝗲𝗧𝗼𝗨𝗻𝗶𝗾𝘂𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f, qw(a q txt));
Position
Position in the file system.
currentDirectory()
Get the current working directory.
Example:
𝗰𝘂𝗿𝗿𝗲𝗻𝘁𝗗𝗶𝗿𝗲𝗰𝘁𝗼𝗿𝘆;
currentDirectoryAbove()
The path to the folder above the current working folder.
Example:
𝗰𝘂𝗿𝗿𝗲𝗻𝘁𝗗𝗶𝗿𝗲𝗰𝘁𝗼𝗿𝘆𝗔𝗯𝗼𝘃𝗲;
parseFileName($)
Parse a file name into (path, name, extension).
Parameter Description
1 $file File name to parse
Example:
is_deeply [𝗽𝗮𝗿𝘀𝗲𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲 "a.b/c.d.e"], [qw(a.b/ c.d e)];
fullFileName()
Full name of a file.
Example:
𝗳𝘂𝗹𝗹𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲(fpe(qw(a txt)));
absFromAbsPlusRel($$)
Create an absolute file from an absolute file and a relative file.
Parameter Description
1 $a Absolute file name
2 $f Relative file name
Example:
ok "/home/la/perl/aaa.pl" eq 𝗮𝗯𝘀𝗙𝗿𝗼𝗺𝗔𝗯𝘀𝗣𝗹𝘂𝘀𝗥𝗲𝗹("/home/la/perl/bbb", "aaa.pl");
ok "/home/la/perl/aaa.pl" eq 𝗮𝗯𝘀𝗙𝗿𝗼𝗺𝗔𝗯𝘀𝗣𝗹𝘂𝘀𝗥𝗲𝗹("/home/il/perl/bbb.pl", "../../la/perl/aaa.pl");
relFromAbsAgainstAbs($$)
Derive a relative file name for the first absolute file name relative to the second absolute file name.
Parameter Description
1 $f Absolute file to be made relative
2 $a Absolute file name to make relative to.
Example:
ok "bbb.pl" eq 𝗿𝗲𝗹𝗙𝗿𝗼𝗺𝗔𝗯𝘀𝗔𝗴𝗮𝗶𝗻𝘀𝘁𝗔𝗯𝘀("/home/la/perl/bbb.pl", "/home/la/perl/aaa.pl");
ok "../perl/bbb.pl" eq 𝗿𝗲𝗹𝗙𝗿𝗼𝗺𝗔𝗯𝘀𝗔𝗴𝗮𝗶𝗻𝘀𝘁𝗔𝗯𝘀("/home/la/perl/bbb.pl", "/home/la/java/aaa.jv");
Temporary
Temporary files and folders
temporaryFile()
Create a temporary file that will automatically be unlinked during END processing.
Example:
my $f = 𝘁𝗲𝗺𝗽𝗼𝗿𝗮𝗿𝘆𝗙𝗶𝗹𝗲;
temporaryFolder()
Create a temporary folder that will automatically be rmdired during END processing.
Example:
my $D = 𝘁𝗲𝗺𝗽𝗼𝗿𝗮𝗿𝘆𝗙𝗼𝗹𝗱𝗲𝗿;
temporaryDirectory is a synonym for temporaryFolder.
Find
Find files and folders below a folder.
findFiles($$)
Find all the files under a folder and optionally filter the selected files with a regular expression.
Parameter Description
1 $dir Folder to start the search with
2 $filter Optional regular expression to filter files
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {createEmptyFile(fpe($d, $_, qw(txt)))} qw(a b c);
is_deeply [sort map {fne $_} 𝗳𝗶𝗻𝗱𝗙𝗶𝗹𝗲𝘀($d, qr(txt\Z))], [qw(a.txt b.txt c.txt)];
findDirs($$)
Find all the folders under a folder and optionally filter the selected folders with a regular expression.
Parameter Description
1 $dir Folder to start the search with
2 $filter Optional regular expression to filter files
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {createEmptyFile(fpe($d, $_, qw(txt)))} qw(a b c);
is_deeply [𝗳𝗶𝗻𝗱𝗗𝗶𝗿𝘀($D)], [$D, $d];
fileList($)
Files that match a given search pattern handed to bsd_glob.
Parameter Description
1 $pattern Search pattern
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {createEmptyFile(fpe($d, $_, qw(txt)))} qw(a b c);
is_deeply [sort map {fne $_} 𝗳𝗶𝗹𝗲𝗟𝗶𝘀𝘁("$d/*.txt")],
["a.txt", "b.txt", "c.txt"];
searchDirectoryTreesForMatchingFiles(@)
Search the specified directory trees for the files (not folders) that match the specified extensions. The argument list should include at least one path name to be useful. If no file extension is supplied then all the files below the specified paths are returned.
Parameter Description
1 @foldersandExtensions Mixture of folder names and extensions
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {createEmptyFile(fpe($d, $_, qw(txt)))} qw(a b c);
is_deeply [sort map {fne $_} 𝘀𝗲𝗮𝗿𝗰𝗵𝗗𝗶𝗿𝗲𝗰𝘁𝗼𝗿𝘆𝗧𝗿𝗲𝗲𝘀𝗙𝗼𝗿𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴𝗙𝗶𝗹𝗲𝘀($d)],
["a.txt", "b.txt", "c.txt"];
matchPath($)
Given an absolute path find out how much of the path actually exists.
Parameter Description
1 $file File name
Example:
my $d = filePath (my @d = qw(a b c d));
ok 𝗺𝗮𝘁𝗰𝗵𝗣𝗮𝘁𝗵($d) eq $d;
findFileWithExtension($@)
Find the first extension from the specified extensions that produces a file that exists when appended to the specified file.
Parameter Description
1 $file File name minus extensions
2 @ext Possible extensions
Example:
my $f = createEmptyFile(fpe(my $d = temporaryFolder, qw(a jpg)));
my $F = 𝗳𝗶𝗻𝗱𝗙𝗶𝗹𝗲𝗪𝗶𝘁𝗵𝗘𝘅𝘁𝗲𝗻𝘀𝗶𝗼𝗻(fpf($d, q(a)), qw(txt data jpg));
ok $F eq "jpg";
clearFolder($$)
Remove all the files and folders under and including the specified folder as long as the number of files to be removed is less than the specified limit.
Parameter Description
1 $folder Folder
2 $limitCount Maximum number of files to remove to limit damage
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {createEmptyFile(fpe($d, $_, qw(txt)))} qw(a b c);
𝗰𝗹𝗲𝗮𝗿𝗙𝗼𝗹𝗱𝗲𝗿($D, 5);
ok !-e $_ for @f;
ok !-d $D;
Read and write files
Read and write strings from and to files creating paths as needed.
readFile($)
Read a file containing unicode in utf8.
Parameter Description
1 $file Name of file to read
Example:
my $f = writeFile(undef, "aaa");
my $s = 𝗿𝗲𝗮𝗱𝗙𝗶𝗹𝗲($f);
ok $s eq "aaa";
appendFile($f, "bbb");
my $S = 𝗿𝗲𝗮𝗱𝗙𝗶𝗹𝗲($f);
ok $S eq "aaabbb";
evalFile($)
Read a file containing unicode in utf8, evaluate it, confess to any errors and then return any result - an improvement on do which silently ignores any problems.
Parameter Description
1 $file File to read
Example:
my $f = dumpFile(undef, [qw(aaa bbb ccc)]);
my $s = 𝗲𝘃𝗮𝗹𝗙𝗶𝗹𝗲($f);
is_deeply $s, [qw(aaa bbb ccc)];
evalGZipFile($)
Read a file containing compressed utf8, evaluate it, confess to any errors or return any result.
Parameter Description
1 $file File to read
Example:
if (1) {
my $d = [1, 2, 3=>{a=>4, b=>5}];
my $file = dumpGZipFile(q(zzz.zip), $d);
ok -e $file;
my $D = 𝗲𝘃𝗮𝗹𝗚𝗭𝗶𝗽𝗙𝗶𝗹𝗲($file);
is_deeply $d, $D;
unlink $file;
}
readBinaryFile($)
Read binary file - a file whose contents are not to be interpreted as unicode.
Parameter Description
1 $file File to read
Example:
my $f = writeBinaryFile(undef, 0xff x 8);
my $s = 𝗿𝗲𝗮𝗱𝗕𝗶𝗻𝗮𝗿𝘆𝗙𝗶𝗹𝗲($f);
ok $s eq 0xff x 8;
readGZipFile($)
Read the specified $file, containing compressed utf8, through gzip
Parameter Description
1 $file File to read.
Example:
if (1) {
my $s = '𝝰'x1e3;
my $file = writeGZipFile(q(zzz.zip), $s);
ok -e $file;
my $S = 𝗿𝗲𝗮𝗱𝗚𝗭𝗶𝗽𝗙𝗶𝗹𝗲($file);
ok $s eq $S;
ok length($s) == length($S);
unlink $file;
}
makePath($)
Make the path for the specified file name or folder.
Parameter Description
1 $file File
Example:
my $d = fpd(my $D = temporaryDirectory, qw(a));
my $f = fpe($d, qw(bbb txt));
ok !-d $d;
𝗺𝗮𝗸𝗲𝗣𝗮𝘁𝗵($f);
ok -d $d;
overWriteFile($$)
Write a unicode utf8 string to a file after creating a path to the file if necessary and return the name of the file on success else confess. If the file already exists it is overwritten.
Parameter Description
1 $file File to write to or B<undef> for a temporary file
2 $string Unicode string to write
Example:
my $f = dumpFile(undef, [qw(aaa bbb ccc)]);
ok 𝗼𝘃𝗲𝗿𝗪𝗿𝗶𝘁𝗲𝗙𝗶𝗹𝗲($f, q({qw(aaa bbb ccc)]));
owf is a synonym for overWriteFile.
writeFile($$)
Write a unicode utf8 string to a new file that does not already exist after creating a path to the file if necessary and return the name of the file on success else confess if a problem occurred or the file does already exist.
Parameter Description
1 $file New file to write to or B<undef> for a temporary file
2 $string String to write
Example:
my $f = 𝘄𝗿𝗶𝘁𝗲𝗙𝗶𝗹𝗲(undef, "aaa");
my $s = readFile($f);
ok $s eq "aaa";
appendFile($f, "bbb");
my $S = readFile($f);
ok $S eq "aaabbb";
dumpFile($$)
Dump a data structure to a file
Parameter Description
1 $file File to write to or B<undef> for a temporary file
2 $struct Address of data structure to write
Example:
my $f = 𝗱𝘂𝗺𝗽𝗙𝗶𝗹𝗲(undef, [qw(aaa bbb ccc)]);
my $s = evalFile($f);
is_deeply $s, [qw(aaa bbb ccc)];
writeGZipFile($$)
Write a unicode utf8 string through gzip to a file.
Parameter Description
1 $file File to write to
2 $string String to write
Example:
if (1) {
my $s = '𝝰'x1e3;
my $file = 𝘄𝗿𝗶𝘁𝗲𝗚𝗭𝗶𝗽𝗙𝗶𝗹𝗲(q(zzz.zip), $s);
ok -e $file;
my $S = readGZipFile($file);
ok $s eq $S;
ok length($s) == length($S);
unlink $file;
}
dumpGZipFile($$)
Write a data structure through gzip to a file.
Parameter Description
1 $file File to write
2 $data Reference to data
Example:
if (1) {
my $d = [1, 2, 3=>{a=>4, b=>5}];
my $file = 𝗱𝘂𝗺𝗽𝗚𝗭𝗶𝗽𝗙𝗶𝗹𝗲(q(zzz.zip), $d);
ok -e $file;
my $D = evalGZipFile($file);
is_deeply $d, $D;
unlink $file;
}
writeFiles($$)
Write the values of a hash into files identified by the key of each value using overWriteFile
Parameter Description
1 $hash Hash of key value pairs representing files and data
2 $folder Optional folder to contain files else the current folder
Example:
my $h =
{"aaa/1.txt"=>"1111",
"aaa/2.txt"=>"2222",
};
𝘄𝗿𝗶𝘁𝗲𝗙𝗶𝗹𝗲𝘀($h);
for(sort keys %$h)
{ok -e $_;
ok readFile($_) eq $h->{$_};
}
appendFile($$)
Append a unicode utf8 string to a file, possibly creating the file and the path to the file if necessary and return the name of the file on success else confess.
Parameter Description
1 $file File to append to
2 $string String to append
Example:
my $f = writeFile(undef, "aaa");
my $s = readFile($f);
ok $s eq "aaa";
𝗮𝗽𝗽𝗲𝗻𝗱𝗙𝗶𝗹𝗲($f, "bbb");
my $S = readFile($f);
ok $S eq "aaabbb";
writeBinaryFile($$)
Write a non unicode string to a file in after creating a path to the file if necessary and return the name of the file on success else confess.
Parameter Description
1 $file File to write to or B<undef> for a temporary file
2 $string Non unicode string to write
Example:
my $f = 𝘄𝗿𝗶𝘁𝗲𝗕𝗶𝗻𝗮𝗿𝘆𝗙𝗶𝗹𝗲(undef, 0xff x 8);
my $s = readBinaryFile($f);
ok $s eq 0xff x 8;
createEmptyFile($)
Create an empty file - writeFile complains if no data is written to the file - and return the name of the file on success else confess.
Parameter Description
1 $file File to create or B<undef> for a temporary file
Example:
my $D = temporaryFolder;
my $d = fpd($D, q(ddd));
my @f = map {𝗰𝗿𝗲𝗮𝘁𝗲𝗘𝗺𝗽𝘁𝘆𝗙𝗶𝗹𝗲(fpe($d, $_, qw(txt)))} qw(a b c);
is_deeply [sort map {fne $_} findFiles($d, qr(txt\Z))], [qw(a.txt b.txt c.txt)];
numberOfLinesInFile($)
The number of lines in a file
Parameter Description
1 $file File
Example:
my $f = writeFile(undef, "a
b
");
ok 𝗻𝘂𝗺𝗯𝗲𝗿𝗢𝗳𝗟𝗶𝗻𝗲𝘀𝗜𝗻𝗙𝗶𝗹𝗲($f) == 2;
Images
Image operations.
imageSize($)
Return (width, height) of an image obtained via Imagemagick.
Parameter Description
1 $image File containing image
Example:
my ($width, $height) = 𝗶𝗺𝗮𝗴𝗲𝗦𝗶𝘇𝗲(fpe(qw(a image jpg)));
convertImageToJpx($$$)
Convert an image to jpx format using Imagemagick.
Parameter Description
1 $source Source file
2 $target Target folder (as multiple files will be created)
3 $Size Optional size of each tile - defaults to 256
Example:
𝗰𝗼𝗻𝘃𝗲𝗿𝘁𝗜𝗺𝗮𝗴𝗲𝗧𝗼𝗝𝗽𝘅(fpe(qw(a image jpg)), fpe(qw(a image jpg)), 256);
convertDocxToFodt($$)
Convert a docx file to fodt using unoconv which must not be running elsewhere at the time. Unoconv can be installed via:
sudo apt install sharutils unoconv
Parameters:
Parameter Description
1 $inputFile Input file
2 $outputFile Output file
Example:
𝗰𝗼𝗻𝘃𝗲𝗿𝘁𝗗𝗼𝗰𝘅𝗧𝗼𝗙𝗼𝗱𝘁(fpe(qw(a docx)), fpe(qw(a fodt)));
cutOutImagesInFodtFile($$$)
Cut out the images embedded in a fodt file, perhaps produced via convertDocxToFodt, placing them in the specified folder and replacing them in the source file with:
<image href="$imageFile" outputclass="imageType">.
This conversion requires that you have both Imagemagick and unoconv installed on your system:
sudo apt install sharutils imagemagick unoconv
Parameters:
Parameter Description
1 $inputFile Input file
2 $outputFolder Output folder for images
3 $imagePrefix A prefix to be added to image file names
Example:
𝗰𝘂𝘁𝗢𝘂𝘁𝗜𝗺𝗮𝗴𝗲𝘀𝗜𝗻𝗙𝗼𝗱𝘁𝗙𝗶𝗹𝗲(fpe(qw(source fodt)), fpd(qw(images)), q(image));
Encoding and Decoding
Encode and decode using Json and Mime.
encodeJson($)
Encode Perl to Json.
Parameter Description
1 $string Data to encode
Example:
my $A = 𝗲𝗻𝗰𝗼𝗱𝗲𝗝𝘀𝗼𝗻(my $a = {a=>1,b=>2, c=>[1..2]});
my $b = decodeJson($A);
is_deeply $a, $b;
decodeJson($)
Decode Perl from Json.
Parameter Description
1 $string Data to decode
Example:
my $A = encodeJson(my $a = {a=>1,b=>2, c=>[1..2]});
my $b = 𝗱𝗲𝗰𝗼𝗱𝗲𝗝𝘀𝗼𝗻($A);
is_deeply $a, $b;
encodeBase64($)
Encode a string in base 64.
Parameter Description
1 $string String to encode
Example:
my $A = 𝗲𝗻𝗰𝗼𝗱𝗲𝗕𝗮𝘀𝗲𝟲𝟰(my $a = "Hello World" x 10);
my $b = decodeBase64($A);
ok $a eq $b;
decodeBase64($)
Decode a string in base 64.
Parameter Description
1 $string String to decode
Example:
my $A = encodeBase64(my $a = "Hello World" x 10);
my $b = 𝗱𝗲𝗰𝗼𝗱𝗲𝗕𝗮𝘀𝗲𝟲𝟰($A);
ok $a eq $b;
convertUnicodeToXml($)
Convert a string with unicode points that are not directly representable in ascii into string that replaces these points with their representation on Xml making the string usable in Xml documents.
Parameter Description
1 $s String to convert
Example:
ok 𝗰𝗼𝗻𝘃𝗲𝗿𝘁𝗨𝗻𝗶𝗰𝗼𝗱𝗲𝗧𝗼𝗫𝗺𝗹('setenta e três') eq q(setenta e três);
Numbers
Numeric operations,
powerOfTwo($)
Test whether a number is a power of two, return the power if it is else undef.
Parameter Description
1 $n Number to check
Example:
ok 𝗽𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(1) == 0;
ok 𝗽𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(2) == 1;
ok !𝗽𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(3);
ok 𝗽𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(4) == 2;
containingPowerOfTwo($)
Find log two of the lowest power of two greater than or equal to a number.
Parameter Description
1 $n Number to check
Example:
ok 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗶𝗻𝗴𝗣𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(1) == 0;
ok 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗶𝗻𝗴𝗣𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(2) == 1;
ok 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗶𝗻𝗴𝗣𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(3) == 2;
ok 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗶𝗻𝗴𝗣𝗼𝘄𝗲𝗿𝗢𝗳𝗧𝘄𝗼(4) == 2;
Sets
Set operations.
setIntersectionOfTwoArraysOfWords($$)
Intersection of two arrays of words.
Parameter Description
1 $a Reference to first array of words
2 $b Reference to second array of words
Example:
is_deeply [qw(a b c)],
[𝘀𝗲𝘁𝗜𝗻𝘁𝗲𝗿𝘀𝗲𝗰𝘁𝗶𝗼𝗻𝗢𝗳𝗧𝘄𝗼𝗔𝗿𝗿𝗮𝘆𝘀𝗢𝗳𝗪𝗼𝗿𝗱𝘀([qw(e f g a b c )], [qw(a A b B c C)])];
setUnionOfTwoArraysOfWords($$)
Union of two arrays of words.
Parameter Description
1 $a Reference to first array of words
2 $b Reference to second array of words
Example:
is_deeply [qw(a b c)],
[𝘀𝗲𝘁𝗨𝗻𝗶𝗼𝗻𝗢𝗳𝗧𝘄𝗼𝗔𝗿𝗿𝗮𝘆𝘀𝗢𝗳𝗪𝗼𝗿𝗱𝘀([qw(a b c )], [qw(a b)])];
contains($@)
Returns the indices at which an item matches elements of the specified array. If the item is a regular expression then it is matched as one, else it is a number it is matched as a number, else as a string.
Parameter Description
1 $item Item
2 @array Array
Example:
is_deeply [1], [𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝘀(1,0..1)];
is_deeply [1,3], [𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝘀(1, qw(0 1 0 1 0 0))];
is_deeply [0, 5], [𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝘀('a', qw(a b c d e a b c d e))];
is_deeply [0, 1, 5], [𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝘀(qr(a+), qw(a baa c d e aa b c d e))];
Minima and Maxima
Find the smallest and largest elements of arrays.
min(@)
Find the minimum number in a list.
Parameter Description
1 @n Numbers
Example:
ok 𝗺𝗶𝗻(1) == 1;
ok 𝗺𝗶𝗻(5,4,2,3) == 2;
max(@)
Find the maximum number in a list.
Parameter Description
1 @n Numbers
Example:
ok !𝗺𝗮𝘅;
ok 𝗺𝗮𝘅(1) == 1;
ok 𝗺𝗮𝘅(1,4,2,3) == 4;
Format
Format data structures as tables.
maximumLineLength($)
Find the longest line in a string
Parameter Description
1 $string String of lines of text
Example:
ok 3 == 𝗺𝗮𝘅𝗶𝗺𝘂𝗺𝗟𝗶𝗻𝗲𝗟𝗲𝗻𝗴𝘁𝗵(<<END);
a
bb
ccc
END
formatTableBasic($)
Tabularize an array of arrays of text.
Parameter Description
1 $data Reference to an array of arrays of data to be formatted as a table.
Example:
my $d = [[qw(a 1)], [qw(bb 22)], [qw(ccc 333)], [qw(dddd 4444)]];
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲𝗕𝗮𝘀𝗶𝗰($d) eq <<END;
a 1
bb 22
ccc 333
dddd 4444
END
formatTable($$%)
Format various data structures as a table. Optionally create a report from the table using the following optional report options:
file=>$file the name of a file to write the report to.
head=>$head a header line in which DDDD will be replaced with the data and time and NNNN will be replaced with the number of rows in the table.
zero=>$zero if true the report will be written to the specified file even if empty.
Parameters:
Parameter Description
1 $data Data to be formatted
2 $title Optional reference to an array of titles
3 %options Options
Example:
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
([[qw(A B C D )],
[qw(AA BB CC DD )],
[qw(AAA BBB CCC DDD )],
[qw(AAAA BBBB CCCC DDDD)],
[qw(1 22 333 4444)]], [qw(aa bb cc)]) eq <<END;
aa bb cc
1 A B C D
2 AA BB CC DD
3 AAA BBB CCC DDD
4 AAAA BBBB CCCC DDDD
5 1 22 333 4444
END
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
([[qw(1 B C)],
[qw(22 BB CC)],
[qw(333 BBB CCC)],
[qw(4444 22 333)]], [qw(aa bb cc)]) eq <<END;
aa bb cc
1 1 B C
2 22 BB CC
3 333 BBB CCC
4 4444 22 333
END
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
([{aa=>'A', bb=>'B', cc=>'C'},
{aa=>'AA', bb=>'BB', cc=>'CC'},
{aa=>'AAA', bb=>'BBB', cc=>'CCC'},
{aa=>'1', bb=>'22', cc=>'333'}
]) eq <<END;
aa bb cc
1 A B C
2 AA BB CC
3 AAA BBB CCC
4 1 22 333
END
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
({''=>[qw(aa bb cc)],
1=>[qw(A B C)],
22=>[qw(AA BB CC)],
333=>[qw(AAA BBB CCC)],
4444=>[qw(1 22 333)]}) eq <<END;
aa bb cc
1 A B C
22 AA BB CC
333 AAA BBB CCC
4444 1 22 333
END
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
({1=>{aa=>'A', bb=>'B', cc=>'C'},
22=>{aa=>'AA', bb=>'BB', cc=>'CC'},
333=>{aa=>'AAA', bb=>'BBB', cc=>'CCC'},
4444=>{aa=>'1', bb=>'22', cc=>'333'}}) eq <<END;
aa bb cc
1 A B C
22 AA BB CC
333 AAA BBB CCC
4444 1 22 333
END
ok 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲({aa=>'A', bb=>'B', cc=>'C'}, [qw(aaaa bbbb)]) eq <<END;
aaaa bbbb
aa A
bb B
cc C
END
if (1) {
my $file = fpe(qw(report txt)); # Create a report
my $t = 𝗳𝗼𝗿𝗺𝗮𝘁𝗧𝗮𝗯𝗹𝗲
([["a",undef], [undef, "b0ac"]], # Data - please replace 0a with a new line
[undef, "BC"], # Column titles
file=>$file, # Output file
head=><<END); # Header
Sample report.
Table has NNNN rows.
END
ok -e $file;
ok readFile($file) eq $t;
unlink $file;
ok $t eq <<END;
Sample report.
Table has 2 rows.
This file: report.txt
BC
1 a
2 b
c
END
}
keyCount($$)
Count keys down to the specified level.
Parameter Description
1 $maxDepth Maximum depth to count to
2 $ref Reference to an array or a hash
Example:
my $a = [[1..3], {map{$_=>1} 1..3}];
my $h = {a=>[1..3], b=>{map{$_=>1} 1..3}};
ok 𝗸𝗲𝘆𝗖𝗼𝘂𝗻𝘁(2, $a) == 6;
ok 𝗸𝗲𝘆𝗖𝗼𝘂𝗻𝘁(2, $h) == 6;
Lines
Load data structures from lines.
loadArrayFromLines($)
Load an array from lines of text in a string.
Parameter Description
1 $string The string of lines from which to create an array
Example:
my $s = 𝗹𝗼𝗮𝗱𝗔𝗿𝗿𝗮𝘆𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
a a
b b
END
is_deeply $s, [q(a a), q(b b)];
ok formatTable($s) eq <<END;
0 a a
1 b b
END
loadHashFromLines($)
Load a hash: first word of each line is the key and the rest is the value.
Parameter Description
1 $string The string of lines from which to create a hash
Example:
my $s = 𝗹𝗼𝗮𝗱𝗛𝗮𝘀𝗵𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
a 10 11 12
b 20 21 22
END
is_deeply $s, {a => q(10 11 12), b =>q(20 21 22)};
ok formatTable($s) eq <<END;
a 10 11 12
b 20 21 22
END
loadArrayArrayFromLines($)
Load an array of arrays from lines of text: each line is an array of words.
Parameter Description
1 $string The string of lines from which to create an array of arrays
Example:
my $s = 𝗹𝗼𝗮𝗱𝗔𝗿𝗿𝗮𝘆𝗔𝗿𝗿𝗮𝘆𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
A B C
AA BB CC
END
is_deeply $s, [[qw(A B C)], [qw(AA BB CC)]];
ok formatTable($s) eq <<END;
1 A B C
2 AA BB CC
END
loadHashArrayFromLines($)
Load a hash of arrays from lines of text: the first word of each line is the key, the remaining words are the array contents.
Parameter Description
1 $string The string of lines from which to create a hash of arrays
Example:
my $s = 𝗹𝗼𝗮𝗱𝗛𝗮𝘀𝗵𝗔𝗿𝗿𝗮𝘆𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
a A B C
b AA BB CC
END
is_deeply $s, {a =>[qw(A B C)], b => [qw(AA BB CC)] };
ok formatTable($s) eq <<END;
a A B C
b AA BB CC
END
loadArrayHashFromLines($)
Load an array of hashes from lines of text: each line is an hash of words.
Parameter Description
1 $string The string of lines from which to create an array of arrays
Example:
my $s = 𝗹𝗼𝗮𝗱𝗔𝗿𝗿𝗮𝘆𝗛𝗮𝘀𝗵𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
A 1 B 2
AA 11 BB 22
END
is_deeply $s, [{A=>1, B=>2}, {AA=>11, BB=>22}];
ok formatTable($s) eq <<END;
A AA B BB
1 1 2
2 11 22
END
loadHashHashFromLines($)
Load a hash of hashes from lines of text: the first word of each line is the key, the remaining words are the sub hash contents.
Parameter Description
1 $string The string of lines from which to create a hash of arrays
Example:
my $s = 𝗹𝗼𝗮𝗱𝗛𝗮𝘀𝗵𝗛𝗮𝘀𝗵𝗙𝗿𝗼𝗺𝗟𝗶𝗻𝗲𝘀 <<END;
a A 1 B 2
b AA 11 BB 22
END
is_deeply $s, {a=>{A=>1, B=>2}, b=>{AA=>11, BB=>22}};
ok formatTable($s) eq <<END;
A AA B BB
a 1 2
b 11 22
END
checkKeys($$)
Check the keys in a hash.
Parameter Description
1 $test The hash to test
2 $permitted A hash of the permitted keys and their meanings
Example:
eval q{𝗰𝗵𝗲𝗰𝗸𝗞𝗲𝘆𝘀({a=>1, b=>2, d=>3}, {a=>1, b=>2, c=>3})};
ok nws($@) =~ m(\AInvalid options chosen: d Permitted.+?: a 1 b 2 c 3);
LVALUE methods
Replace $a->{value} = $b with $a->value = $b which reduces the amount of typing required, is easier to read and provides a hard check that {value} is spelled correctly.
genLValueScalarMethods(@)
Generate lvalue scalar methods in the current package, A method whose value has not yet been set will return a new scalar with value undef. Suffixing X to the scalar name will confess if a value has not been set.
Parameter Description
1 @names List of method names
Example:
package Scalars;
my $a = bless{};
Data::Table::Text::𝗴𝗲𝗻𝗟𝗩𝗮𝗹𝘂𝗲𝗦𝗰𝗮𝗹𝗮𝗿𝗠𝗲𝘁𝗵𝗼𝗱𝘀(qw(aa bb cc));
$a->aa = 'aa';
Test::More::ok $a->aa eq 'aa';
Test::More::ok !$a->bb;
Test::More::ok $a->bbX eq q();
$a->aa = undef;
Test::More::ok !$a->aa;
addLValueScalarMethods(@)
Generate lvalue scalar methods in the current package if they do not already exist. A method whose value has not yet been set will return a new scalar with value undef. Suffixing X to the scalar name will confess if a value has not been set.
Parameter Description
1 @names List of method names
Example:
my $class = "Data::Table::Text::Test";
my $a = bless{}, $class;
𝗮𝗱𝗱𝗟𝗩𝗮𝗹𝘂𝗲𝗦𝗰𝗮𝗹𝗮𝗿𝗠𝗲𝘁𝗵𝗼𝗱𝘀(qq(${class}::$_)) for qw(aa bb aa bb);
$a->aa = 'aa';
ok $a->aa eq 'aa';
ok !$a->bb;
ok $a->bbX eq q();
$a->aa = undef;
ok !$a->aa;
genLValueScalarMethodsWithDefaultValues(@)
Generate lvalue scalar methods with default values in the current package. A reference to a method whose value has not yet been set will return a scalar whose value is the name of the method.
Parameter Description
1 @names List of method names
Example:
package ScalarsWithDefaults;
my $a = bless{};
Data::Table::Text::𝗴𝗲𝗻𝗟𝗩𝗮𝗹𝘂𝗲𝗦𝗰𝗮𝗹𝗮𝗿𝗠𝗲𝘁𝗵𝗼𝗱𝘀𝗪𝗶𝘁𝗵𝗗𝗲𝗳𝗮𝘂𝗹𝘁𝗩𝗮𝗹𝘂𝗲𝘀(qw(aa bb cc));
Test::More::ok $a->aa eq 'aa';
genLValueArrayMethods(@)
Generate lvalue array methods in the current package. A reference to a method that has no yet been set will return a reference to an empty array.
Parameter Description
1 @names List of method names
Example:
package Arrays;
my $a = bless{};
Data::Table::Text::𝗴𝗲𝗻𝗟𝗩𝗮𝗹𝘂𝗲𝗔𝗿𝗿𝗮𝘆𝗠𝗲𝘁𝗵𝗼𝗱𝘀(qw(aa bb cc));
$a->aa->[1] = 'aa';
Test::More::ok $a->aa->[1] eq 'aa';
genLValueHashMethods(@)
Generate lvalue hash methods in the current package. A reference to a method that has no yet been set will return a reference to an empty hash.
Parameter Description
1 @names Method names
Example:
package Hashes;
my $a = bless{};
Data::Table::Text::𝗴𝗲𝗻𝗟𝗩𝗮𝗹𝘂𝗲𝗛𝗮𝘀𝗵𝗠𝗲𝘁𝗵𝗼𝗱𝘀(qw(aa bb cc));
$a->aa->{a} = 'aa';
Test::More::ok $a->aa->{a} eq 'aa';
genHash($%)
Return a $blessed hash with the specified $attributes. updateDocumentation will generate documentation at "Hash Definitions" for the hash defined by the call to genHash if the call is laid out as in the example below.
Parameter Description
1 $bless Package name
2 %attributes Hash of attribute names and values
Example:
if (1) {
my $o = 𝗴𝗲𝗻𝗛𝗮𝘀𝗵(q(TestHash), # Definition of a blessed hash.
a=>q(aa), # Definition of attribute aa.
b=>q(bb), # Definition of attribute bb.
);
ok $o->a eq q(aa);
is_deeply $o, {a=>"aa", b=>"bb"};
my $p = 𝗴𝗲𝗻𝗛𝗮𝘀𝗵(q(TestHash),
c=>q(cc), # Definition of attribute cc.
);
ok $p->c eq q(cc);
ok $p->a = q(aa);
ok $p->a eq q(aa);
is_deeply $p, {a=>"aa", c=>"cc"};
loadHash($p, a=>11, b=>22); # Load the hash
is_deeply $p, {a=>11, b=>22, c=>"cc"};
my $r = eval {loadHash($p, d=>44)}; # Try to load the hash
ok $@ =~ m(Cannot load attribute: d);
}
loadHash($%)
Load the specified $hash generated with genHash with %attributes. Confess to any unknown attribute names.
Parameter Description
1 $hash Hash
2 %attributes Hash of attribute names and values to be loaded
Example:
if (1) {
my $o = genHash(q(TestHash), # Definition of a blessed hash.
a=>q(aa), # Definition of attribute aa.
b=>q(bb), # Definition of attribute bb.
);
ok $o->a eq q(aa);
is_deeply $o, {a=>"aa", b=>"bb"};
my $p = genHash(q(TestHash),
c=>q(cc), # Definition of attribute cc.
);
ok $p->c eq q(cc);
ok $p->a = q(aa);
ok $p->a eq q(aa);
is_deeply $p, {a=>"aa", c=>"cc"};
𝗹𝗼𝗮𝗱𝗛𝗮𝘀𝗵($p, a=>11, b=>22); # Load the hash
is_deeply $p, {a=>11, b=>22, c=>"cc"};
my $r = eval {𝗹𝗼𝗮𝗱𝗛𝗮𝘀𝗵($p, d=>44)}; # Try to load the hash
ok $@ =~ m(Cannot load attribute: d);
}
assertPackageRefs($@)
Confirm that the specified references are to the specified package
Parameter Description
1 $package Package
2 @refs References
Example:
if (1) {
eval q{𝗮𝘀𝘀𝗲𝗿𝘁𝗣𝗮𝗰𝗸𝗮𝗴𝗲𝗥𝗲𝗳𝘀(q(bbb), bless {}, q(aaa))};
ok $@ =~ m(\AWanted reference to bbb, but got aaa);
}
assertRef(@)
Confirm that the specified references are to the package into which this routine has been exported.
Parameter Description
1 @refs References
Example:
if (1) {
eval q{𝗮𝘀𝘀𝗲𝗿𝘁𝗥𝗲𝗳(bless {}, q(aaa))};
ok $@ =~ m(\AWanted reference to Data::Table::Text, but got aaa);
}
ˢ(&)
Immediately executed inline sub to allow a code block before if.
Parameter Description
1 $sub Sub enclosed in {} without the word "sub"
Example:
ok ˢ{1} == 1;
ok ˢ{1};
ˢ{my $s =
ˢ{if (1)
{return q(aa) if 1;
q(bb);
}
};
ok $s eq q(aa);
};
arrayToHash(@)
Create a hash from an array
Parameter Description
1 @array Array
Example:
is_deeply 𝗮𝗿𝗿𝗮𝘆𝗧𝗼𝗛𝗮𝘀𝗵(qw(a b c)), {a=>1, b=>1, c=>1};
Strings
Actions on strings.
indentString($$)
Indent lines contained in a string or formatted table by the specified string.
Parameter Description
1 $string The string of lines to indent
2 $indent The indenting string
Example:
my $t = [qw(aa bb cc)];
my $d = [[qw(A B C)], [qw(AA BB CC)], [qw(AAA BBB CCC)], [qw(1 22 333)]];
ok $s eq <<END;
1 A B C
2 AA BB CC
3 AAA BBB CCC
4 1 22 333
END
isBlank($)
Test whether a string is blank.
Parameter Description
1 $string String
Example:
ok 𝗶𝘀𝗕𝗹𝗮𝗻𝗸("");
ok 𝗶𝘀𝗕𝗹𝗮𝗻𝗸("
");
trim($)
Remove any white space from the front and end of a string.
Parameter Description
1 $string String
Example:
ok 𝘁𝗿𝗶𝗺(" a b ") eq join ' ', qw(a b);
pad($$$)
Pad a string with blanks or the specified padding character to a multiple of a specified length.
Parameter Description
1 $string String
2 $length Tab width
3 $pad Padding char
Example:
ok 𝗽𝗮𝗱('abc ', 2).'=' eq "abc =";
ok 𝗽𝗮𝗱('abc ', 3).'=' eq "abc=";
ok 𝗽𝗮𝗱('abc ', 4, q(.)).'=' eq "abc.=";
firstNChars($$)
First N characters of a string.
Parameter Description
1 $string String
2 $length Length
Example:
ok 𝗳𝗶𝗿𝘀𝘁𝗡𝗖𝗵𝗮𝗿𝘀(q(abc), 2) eq q(ab);
ok 𝗳𝗶𝗿𝘀𝘁𝗡𝗖𝗵𝗮𝗿𝘀(q(abc), 4) eq q(abc);
nws($$)
Normalize white space in a string to make comparisons easier. Leading and trailing white space is removed; blocks of white space in the interior are reduced to a single space. In effect: this puts everything on one long line with never more than one space at a time. Optionally a maximum length is applied to the normalized string.
Parameter Description
1 $string String to normalize
2 $length Maximum length of result
Example:
ok 𝗻𝘄𝘀(qq(a b c)) eq q(a b c);
stringsAreNotEqual($$)
Return the common start followwe by the two non equal tails of two non equal strings or an empty list if the strings are equal.
Parameter Description
1 $a First string
2 $b Second string
Example:
if (1) {
ok !𝘀𝘁𝗿𝗶𝗻𝗴𝘀𝗔𝗿𝗲𝗡𝗼𝘁𝗘𝗾𝘂𝗮𝗹(q(abc), q(abc));
ok 𝘀𝘁𝗿𝗶𝗻𝗴𝘀𝗔𝗿𝗲𝗡𝗼𝘁𝗘𝗾𝘂𝗮𝗹(q(abc), q(abd));
is_deeply [𝘀𝘁𝗿𝗶𝗻𝗴𝘀𝗔𝗿𝗲𝗡𝗼𝘁𝗘𝗾𝘂𝗮𝗹(q(abc), q(abd))], [qw(ab c d)];
is_deeply [𝘀𝘁𝗿𝗶𝗻𝗴𝘀𝗔𝗿𝗲𝗡𝗼𝘁𝗘𝗾𝘂𝗮𝗹(q(ab), q(abd))], [q(ab), '', q(d)];
}
javaPackage($)
Extract the package name from a java string or file.
Parameter Description
1 $java Java file if it exists else the string of java
Example:
overWriteFile($f, <<END);
// Test
package com.xyz;
END
ok 𝗷𝗮𝘃𝗮𝗣𝗮𝗰𝗸𝗮𝗴𝗲($f) eq "com.xyz";
javaPackageAsFileName($)
Extract the package name from a java string or file and convert it to a file name.
Parameter Description
1 $java Java file if it exists else the string of java
Example:
overWriteFile($f, <<END);
// Test
package com.xyz;
END
ok 𝗷𝗮𝘃𝗮𝗣𝗮𝗰𝗸𝗮𝗴𝗲𝗔𝘀𝗙𝗶𝗹𝗲𝗡𝗮𝗺𝗲($f) eq "com/xyz";
perlPackage($)
Extract the package name from a perl string or file.
Parameter Description
1 $perl Perl file if it exists else the string of perl
Example:
overWriteFile($f, <<END);
package a::b;
END
ok 𝗽𝗲𝗿𝗹𝗣𝗮𝗰𝗸𝗮𝗴𝗲($f) eq "a::b";
printQw(@)
Print an array of words in qw() format.
Parameter Description
1 @words Array of words
Example:
ok 𝗽𝗿𝗶𝗻𝘁𝗤𝘄(qw(a b c)) eq q(qw(a b c));
numberOfLinesInString($)
The number of lines in a string.
Parameter Description
1 $string String
Example:
ok 𝗻𝘂𝗺𝗯𝗲𝗿𝗢𝗳𝗟𝗶𝗻𝗲𝘀𝗜𝗻𝗦𝘁𝗿𝗶𝗻𝗴("a
b
") == 2;
Unicode
Translate ascii alphanumerics in strings to various Unicode blocks.
boldString($)
Convert alphanumerics in a string to bold.
Parameter Description
1 $string String to convert
Example:
ok 𝗯𝗼𝗹𝗱𝗦𝘁𝗿𝗶𝗻𝗴(q(zZ)) eq q(𝘇𝗭);
boldStringUndo($)
Undo alphanumerics in a string to bold.
Parameter Description
1 $string String to convert
Example:
if (1)
{my $n = 1234567890;
ok 𝗯𝗼𝗹𝗱𝗦𝘁𝗿𝗶𝗻𝗴𝗨𝗻𝗱𝗼 (boldString($n)) == $n;
ok enclosedStringUndo (enclosedString($n)) == $n;
ok enclosedReversedStringUndo(enclosedReversedString($n)) == $n;
ok superScriptStringUndo (superScriptString($n)) == $n;
ok subScriptStringUndo (subScriptString($n)) == $n;
}
enclosedString($)
Convert alphanumerics in a string to enclosed alphanumerics.
Parameter Description
1 $string String to convert
Example:
ok 𝗲𝗻𝗰𝗹𝗼𝘀𝗲𝗱𝗦𝘁𝗿𝗶𝗻𝗴(q(hello world 1234)) eq q(ⓗⓔⓛⓛⓞ ⓦⓞⓡⓛⓓ ①②③④);
enclosedStringUndo($)
Undo alphanumerics in a string to enclosed alphanumerics.
Parameter Description
1 $string String to convert
Example:
if (1)
{my $n = 1234567890;
ok boldStringUndo (boldString($n)) == $n;
ok 𝗲𝗻𝗰𝗹𝗼𝘀𝗲𝗱𝗦𝘁𝗿𝗶𝗻𝗴𝗨𝗻𝗱𝗼 (enclosedString($n)) == $n;
ok enclosedReversedStringUndo(enclosedReversedString($n)) == $n;
ok superScriptStringUndo (superScriptString($n)) == $n;
ok subScriptStringUndo (subScriptString($n)) == $n;
}
enclosedReversedString($)
Convert alphanumerics in a string to enclosed reversed alphanumerics.
Parameter Description
1 $string String to convert
Example:
ok 𝗲𝗻𝗰𝗹𝗼𝘀𝗲𝗱𝗥𝗲𝘃𝗲𝗿𝘀𝗲𝗱𝗦𝘁𝗿𝗶𝗻𝗴(q(hello world 1234)) eq q(🅗🅔🅛🅛🅞 🅦🅞🅡🅛🅓 ➊➋➌➍);
enclosedReversedStringUndo($)
Undo alphanumerics in a string to enclosed reversed alphanumerics.
Parameter Description
1 $string String to convert
Example:
if (1)
{my $n = 1234567890;
ok boldStringUndo (boldString($n)) == $n;
ok enclosedStringUndo (enclosedString($n)) == $n;
ok 𝗲𝗻𝗰𝗹𝗼𝘀𝗲𝗱𝗥𝗲𝘃𝗲𝗿𝘀𝗲𝗱𝗦𝘁𝗿𝗶𝗻𝗴𝗨𝗻𝗱𝗼(enclosedReversedString($n)) == $n;
ok superScriptStringUndo (superScriptString($n)) == $n;
ok subScriptStringUndo (subScriptString($n)) == $n;
}
superScriptString($)
Convert alphanumerics in a string to super scripts
Parameter Description
1 $string String to convert
Example:
ok 𝘀𝘂𝗽𝗲𝗿𝗦𝗰𝗿𝗶𝗽𝘁𝗦𝘁𝗿𝗶𝗻𝗴(1234567890) eq q(¹²³⁴⁵⁶⁷⁸⁹⁰);
superScriptStringUndo($)
Undo alphanumerics in a string to super scripts
Parameter Description
1 $string String to convert
Example:
if (1)
{my $n = 1234567890;
ok boldStringUndo (boldString($n)) == $n;
ok enclosedStringUndo (enclosedString($n)) == $n;
ok enclosedReversedStringUndo(enclosedReversedString($n)) == $n;
ok 𝘀𝘂𝗽𝗲𝗿𝗦𝗰𝗿𝗶𝗽𝘁𝗦𝘁𝗿𝗶𝗻𝗴𝗨𝗻𝗱𝗼 (superScriptString($n)) == $n;
ok subScriptStringUndo (subScriptString($n)) == $n;
}
subScriptString($)
Convert alphanumerics in a string to sub scripts
Parameter Description
1 $string String to convert
Example:
ok 𝘀𝘂𝗯𝗦𝗰𝗿𝗶𝗽𝘁𝗦𝘁𝗿𝗶𝗻𝗴(1234567890) eq q(₁₂₃₄₅₆₇₈₉₀);
subScriptStringUndo($)
Undo alphanumerics in a string to sub scripts
Parameter Description
1 $string String to convert
Example:
if (1)
{my $n = 1234567890;
ok boldStringUndo (boldString($n)) == $n;
ok enclosedStringUndo (enclosedString($n)) == $n;
ok enclosedReversedStringUndo(enclosedReversedString($n)) == $n;
ok superScriptStringUndo (superScriptString($n)) == $n;
ok 𝘀𝘂𝗯𝗦𝗰𝗿𝗶𝗽𝘁𝗦𝘁𝗿𝗶𝗻𝗴𝗨𝗻𝗱𝗼 (subScriptString($n)) == $n;
}
Cloud Cover
Useful for operating across the cloud.
makeDieConfess()
Force die to confess where the death occurred.
Example:
𝗺𝗮𝗸𝗲𝗗𝗶𝗲𝗖𝗼𝗻𝗳𝗲𝘀𝘀
ipAddressViaArp($)
Get the ip address of a server on the local network by hostname via arp
Parameter Description
1 $hostName Host name
Example:
𝗶𝗽𝗔𝗱𝗱𝗿𝗲𝘀𝘀𝗩𝗶𝗮𝗔𝗿𝗽(q(secarias));
saveCodeToS3($$$$)
Save source code files.
Parameter Description
1 $saveCodeEvery Save every seconds
2 $zipFileName Zip file name
3 $bucket Bucket/key
4 $S3Parms Additional S3 parameters like profile or region as a string
Example:
𝘀𝗮𝘃𝗲𝗖𝗼𝗱𝗲𝗧𝗼𝗦𝟯(1200, q(projectName), q(bucket/folder), q(--only-show-errors));
addCertificate($)
Add a certificate to the current ssh session.
Parameter Description
1 $file File containing certificate
Example:
𝗮𝗱𝗱𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲(fpf(qw(.ssh cert)));
hostName()
The name of the host we are running on.
Example:
𝗵𝗼𝘀𝘁𝗡𝗮𝗺𝗲;
userId()
The userid we are currently running under.
Example:
𝘂𝘀𝗲𝗿𝗜𝗱;
wwwEncode($)
Replace spaces in a string with %20 .
Parameter Description
1 $string String
Example:
ok 𝘄𝘄𝘄𝗘𝗻𝗰𝗼𝗱𝗲(q(a b c)) eq q(a%20%20b%20c);
startProcess(&\%$)
Start new processes while the number of child processes recorded in %$pids is less than the specified $maximum. Use waitForAllStartedProcessesToFinish to wait for all these processes to finish.
Parameter Description
1 $sub Sub to start
2 $pids Hash in which to record the process ids
3 $maximum Maximum number of processes to run at a time
Example:
if (0) {
my %pids;
ˢ{𝘀𝘁𝗮𝗿𝘁𝗣𝗿𝗼𝗰𝗲𝘀𝘀 {} %pids, 1; ok 1 >= keys %pids} for 1..8;
waitForAllStartedProcessesToFinish(%pids);
ok !keys(%pids)
}
waitForAllStartedProcessesToFinish(\%)
Wait until all the processes started by startProcess have finished.
Parameter Description
1 $pids Hash of started process ids
Example:
if (0) {
my %pids;
ˢ{startProcess {} %pids, 1; ok 1 >= keys %pids} for 1..8;
𝘄𝗮𝗶𝘁𝗙𝗼𝗿𝗔𝗹𝗹𝗦𝘁𝗮𝗿𝘁𝗲𝗱𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗲𝘀𝗧𝗼𝗙𝗶𝗻𝗶𝘀𝗵(%pids);
ok !keys(%pids)
}
newProcessStarter($$)
Create a new process starter with which to start parallel processes up to a specified $maximumNumberOfProcesses maximum number of parallel processes at a time, wait for all the started processes to finish and then optionally retrieve their results from the folder named by $transferArea.
Parameter Description
1 $maximumNumberOfProcesses Maximum number of processes to start
2 $transferArea Optional folder to be used to save results.
Example:
if (1)
{my $N = 100;
my $s = 𝗻𝗲𝘄𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗦𝘁𝗮𝗿𝘁𝗲𝗿(4, q(processes));
for my $i(1..$N)
{Data::Table::Text::Starter::start($s, sub{$i*$i});
}
is_deeply
[sort {$a <=> $b} Data::Table::Text::Starter::finish($s)],
[map {$_**2} 1..$N];
clearFolder($s->transferArea, 1e2);
}
Data::Table::Text::Starter::start($$)
Start a new process to run the specified $sub.
Parameter Description
1 $starter Starter
2 $sub Sub to be run.
Example:
if (1)
{my $N = 100;
my $s = newProcessStarter(4, q(processes));
for my $i(1..$N)
{𝗗𝗮𝘁𝗮::𝗧𝗮𝗯𝗹𝗲::𝗧𝗲𝘅𝘁::𝗦𝘁𝗮𝗿𝘁𝗲𝗿::𝘀𝘁𝗮𝗿𝘁($s, sub{$i*$i});
}
is_deeply
[sort {$a <=> $b} Data::Table::Text::Starter::finish($s)],
[map {$_**2} 1..$N];
clearFolder($s->transferArea, 1e2);
}
Data::Table::Text::Starter::finish($)
Wait for all started processes to finish and return their consolidated results.
Parameter Description
1 $starter Starter
Example:
if (1)
{my $N = 100;
my $s = newProcessStarter(4, q(processes));
for my $i(1..$N)
{Data::Table::Text::Starter::start($s, sub{$i*$i});
}
is_deeply
[sort {$a <=> $b} 𝗗𝗮𝘁𝗮::𝗧𝗮𝗯𝗹𝗲::𝗧𝗲𝘅𝘁::𝗦𝘁𝗮𝗿𝘁𝗲𝗿::𝗳𝗶𝗻𝗶𝘀𝗵($s)],
[map {$_**2} 1..$N];
clearFolder($s->transferArea, 1e2);
}
newServiceIncarnation($$)
Create a new service incarnation to record the start up of a new instance of a service and return the description as a Data::Exchange::Service Definition hash.
Parameter Description
1 $service Service name
2 $file Optional details file
Example:
if (1)
{my $s = 𝗻𝗲𝘄𝗦𝗲𝗿𝘃𝗶𝗰𝗲𝗜𝗻𝗰𝗮𝗿𝗻𝗮𝘁𝗶𝗼𝗻("aaa", q(bbb.txt));
is_deeply $s->check, $s;
my $t = 𝗻𝗲𝘄𝗦𝗲𝗿𝘃𝗶𝗰𝗲𝗜𝗻𝗰𝗮𝗿𝗻𝗮𝘁𝗶𝗼𝗻("aaa", q(bbb.txt));
is_deeply $t->check, $t;
ok $t->start >= $s->start+1;
ok !$s->check(1);
unlink q(bbb.txt);
}
Data::Exchange::Service::check($$)
Check that we are the current incarnation of the named service with details obtained from newServiceIncarnation. If the optional $continue flag has been set then return the service details if this is the current service incarnation else undef. Otherwise if the $continue flag is false confess unless this is the current service incarnation thus bringing the earlier version of this service to an abrupt end.
Parameter Description
1 $s Current service details
2 $continue Return result if B<$continue> is true else confess if the service has been replaced
Example:
if (1)
{my $s = newServiceIncarnation("aaa", q(bbb.txt));
is_deeply $s->check, $s;
my $t = newServiceIncarnation("aaa", q(bbb.txt));
is_deeply $t->check, $t;
ok $t->start >= $s->start+1;
ok !$s->check(1);
unlink q(bbb.txt);
}
Documentation
Extract, format and update documentation for a perl module.
htmlToc($@)
Generate a table of contents for some html.
Parameter Description
1 $replace Sub-string within the html to be replaced with the toc
2 $html String of html
Example:
ok nws(𝗵𝘁𝗺𝗹𝗧𝗼𝗰("XXXX", <<END)), '𝗵𝘁𝗺𝗹𝗧𝗼𝗰'
<h1 id="1" otherprops="1">Chapter 1</h1>
<h2 id="11" otherprops="11">Section 1</h1>
<h1 id="2" otherprops="2">Chapter 2</h1>
XXXX
END
eq nws(<<END);
<h1 id="1" otherprops="1">Chapter 1</h1>
<h2 id="11" otherprops="11">Section 1</h1>
<h1 id="2" otherprops="2">Chapter 2</h1>
<table cellspacing=10 border=0>
<tr><td>
<tr><td align=right>1<td> <a href="#1">Chapter 1</a>
<tr><td align=right>2<td> <a href="#11">Section 1</a>
<tr><td>
<tr><td align=right>3<td> <a href="#2">Chapter 2</a>
</table>
END
updateDocumentation($)
Update documentation from the comments in a perl script. Comments between the lines marked with:
#Dn title # description
and:
#D
where n is either 1, 2 or 3 indicating the heading level of the section and the # is in column 1.
Methods are formatted as:
sub name(signature) #FLAGS comment describing method
{my ($parameters) = @_; # comments for each parameter separated by commas.
FLAGS can be chosen from:
- I
-
method of interest to new users
- P
-
private method
- r
-
optionally replaceable method
- R
-
required replaceable method
- S
-
static method
- X
-
die rather than received a returned undef result
Other flags will be handed to the method extractDocumentationFlags(flags to process, method name) found in the file being documented, this method should return [the additional documentation for the method, the code to implement the flag].
Text following 'Example:' in the comment (if present) will be placed after the parameters list as an example. Lines containing comments consisting of '#T'.methodName will also be aggregated and displayed as examples for that method.
Lines formatted as:
BEGIN{*source=*target}
starting in column 1 will define a synonym for a method.
Lines formatted as:
#C emailAddress text
will be aggregated in the acknowledgments section at the end of the documentation.
The character sequence \n in the comment will be expanded to one new line, \m to two new lines and L<$_>,L<confess>,L<die>,L<eval>,L<lvalueMethod> to links to the perl documentation.
Search for '#D1': in https://metacpan.org/source/PRBRENAN/Data-Table-Text-20180810/lib/Data/Table/Text.pm to see more examples of such documentation in action - although it is quite difficult to see as it looks just like normal comments placed in the code.
Parameters:
Parameter Description
1 $perlModule Optional file name with caller's file being the default
Example:
{my $s = 𝘂𝗽𝗱𝗮𝘁𝗲𝗗𝗼𝗰𝘂𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻(<<'END' =~ s(#) (#)gsr =~ s(~) ()gsr);
package Sample::Module;
#D1 Samples # Sample methods.
sub sample($@) #R Documentation for the: sample() method. See also L<Data::Table::Text::sample2|/Data::Table::Text::sample2>. #Tsample
{my ($node, @context) = @_; # Node, optional context
1
}
~BEGIN{*smpl=*sample}
sub Data::Table::Text::sample2(\&@) #PS Documentation for the sample2() method.
{my ($sub, @context) = @_; # Sub to call, context.
1
}
ok sample(undef, qw(a b c)) == 1; #Tsample
if (1) #Tsample
{ok sample(q(a), qw(a b c)) == 2;
ok sample(undef, qw(a b c)) == 1;
}
ok sample(<<END2)) == 1; #Tsample
sample data
END2
ok $s =~ m'=head2 sample28\$\@29';
Hash Definitions
Data::Exchange::Service Definition
Service details.
file - The file in which the service start details is being recorded.
service - The name of the service.
start - The time this service was started time plus a minor hack to simplify testing.
Data::Table::Text::Starter Definition
Process starter definition.
maximumNumberOfProcesses - The maximum number of processes to start in parallel at one time. If this limit is exceeded, the start of subsequent processes will be delayed until processes started earlier have finished.
transferArea - The name of the folder in which files transferring results from the child to the parent process will be stored.
TestHash Definition
Definition of a blessed hash.
a - Definition of attribute aa.
b - Definition of attribute bb.
Private Methods
denormalizeFolderName($)
Remove any trailing folder separator from a folder name component.
Parameter Description
1 $name Name
renormalizeFolderName($)
Normalize a folder name component by adding a trailing separator.
Parameter Description
1 $name Name
trackFiles($@)
Track the existence of files.
Parameter Description
1 $label Label
2 @files Files
printFullFileName()
Print a file name on a separate line with escaping so it can be used easily from the command line.
readUtf16File($)
Read a file containing unicode in utf-16 format.
Parameter Description
1 $file Name of file to read
binModeAllUtf8()
Set STDOUT and STDERR to accept utf8 without complaint.
Example:
𝗯𝗶𝗻𝗠𝗼𝗱𝗲𝗔𝗹𝗹𝗨𝘁𝗳𝟴;
convertImageToJpx690($$$)
Convert an image to jpx format using versions of Imagemagick version 6.9.0 and above.
Parameter Description
1 $source Source file
2 $target Target folder (as multiple files will be created)
3 $Size Optional size of each tile - defaults to 256
formatTableMultiLine($$)
Tabularize text that has new lines in it.
Parameter Description
1 $data Reference to an array of arrays of data to be formatted as a table
2 $separator Optional line separator to use instead of new line for each row.
formatTableAA($$)
Tabularize an array of arrays.
Parameter Description
1 $data Data to be formatted
2 $title Optional reference to an array of titles
formatTableHA($$)
Tabularize a hash of arrays.
Parameter Description
1 $data Data to be formatted
2 $title Optional titles
formatTableAH($)
Tabularize an array of hashes.
Parameter Description
1 $data Data to be formatted
formatTableHH($)
Tabularize a hash of hashes.
Parameter Description
1 $data Data to be formatted
formatTableA($$)
Tabularize an array.
Parameter Description
1 $data Data to be formatted
2 $title Optional title
formatTableH($$)
Tabularize a hash.
Parameter Description
1 $data Data to be formatted
2 $title Optional title
saveSourceToS3($$)
Save source code.
Parameter Description
1 $aws Aws target file and keywords
2 $saveIntervalInSeconds Save internal
Data::Table::Text::Starter::waitOne($)
Wait for one process to finish and consolidate its results.
Parameter Description
1 $starter Starter
extractTest($)
Remove example markers from test code.
Parameter Description
1 $string String containing test line
docUserFlags($$$$)
Generate documentation for a method by calling the extractDocumentationFlags method in the package being documented, passing it the flags for a method and the name of the method. The called method should return the documentation to be inserted for the named method.
Parameter Description
1 $flags Flags
2 $perlModule File containing documentation
3 $package Package containing documentation
4 $name Name of method to be processed
updatePerlModuleDocumentation($)
Update the documentation in a perl file and show said documentation in a web browser.
Parameter Description
1 $perlModule File containing the code of the perl module
Synonyms
fpd is a synonym for filePathDir - Create a directory name from an array of file name components.
fpe is a synonym for filePathExt - Create a file name from an array of file name components the last of which is an extension.
fpf is a synonym for filePath - Create a file name from an array of file name components.
owf is a synonym for overWriteFile - Write a unicode utf8 string to a file after creating a path to the file if necessary and return the name of the file on success else confess.
temporaryDirectory is a synonym for temporaryFolder - Create a temporary folder that will automatically be rmdired during END processing.
Index
1 absFromAbsPlusRel - Create an absolute file from an absolute file and a relative file.
2 addCertificate - Add a certificate to the current ssh session.
3 addLValueScalarMethods - Generate lvalue scalar methods in the current package if they do not already exist.
4 appendFile - Append a unicode utf8 string to a file, possibly creating the file and the path to the file if necessary and return the name of the file on success else confess.
5 arrayToHash - Create a hash from an array
6 assertPackageRefs - Confirm that the specified references are to the specified package
7 assertRef - Confirm that the specified references are to the package into which this routine has been exported.
8 binModeAllUtf8 - Set STDOUT and STDERR to accept utf8 without complaint.
9 boldString - Convert alphanumerics in a string to bold.
10 boldStringUndo - Undo alphanumerics in a string to bold.
11 call - Call the specified sub in a separate process, wait for it to complete, copy back the named our variables, free the memory used.
12 checkFile - Return the name of the specified file if it exists, else confess the maximum extent of the path that does exist.
13 checkKeys - Check the keys in a hash.
14 clearFolder - Remove all the files and folders under and including the specified folder as long as the number of files to be removed is less than the specified limit.
15 containingPowerOfTwo - Find log two of the lowest power of two greater than or equal to a number.
16 contains - Returns the indices at which an item matches elements of the specified array.
17 convertDocxToFodt - Convert a docx file to fodt using unoconv which must not be running elsewhere at the time.
18 convertImageToJpx - Convert an image to jpx format using Imagemagick.
19 convertImageToJpx690 - Convert an image to jpx format using versions of Imagemagick version 6.
20 convertUnicodeToXml - Convert a string with unicode points that are not directly representable in ascii into string that replaces these points with their representation on Xml making the string usable in Xml documents.
21 createEmptyFile - Create an empty file - writeFile complains if no data is written to the file - and return the name of the file on success else confess.
22 currentDirectory - Get the current working directory.
23 currentDirectoryAbove - The path to the folder above the current working folder.
24 cutOutImagesInFodtFile - Cut out the images embedded in a fodt file, perhaps produced via convertDocxToFodt, placing them in the specified folder and replacing them in the source file with:
<image href="$imageFile" outputclass="imageType">.
25 Data::Exchange::Service::check - Check that we are the current incarnation of the named service with details obtained from newServiceIncarnation.
26 Data::Table::Text::Starter::finish - Wait for all started processes to finish and return their consolidated results.
27 Data::Table::Text::Starter::start - Start a new process to run the specified $sub.
28 Data::Table::Text::Starter::waitOne - Wait for one process to finish and consolidate its results.
29 dateStamp - Year-monthName-day
30 dateTimeStamp - Year-monthNumber-day at hours:minute:seconds
31 dateTimeStampName - Date time stamp without white space.
32 decodeBase64 - Decode a string in base 64.
33 decodeJson - Decode Perl from Json.
34 denormalizeFolderName - Remove any trailing folder separator from a folder name component.
35 docUserFlags - Generate documentation for a method by calling the extractDocumentationFlags method in the package being documented, passing it the flags for a method and the name of the method.
36 dumpFile - Dump a data structure to a file
37 dumpGZipFile - Write a data structure through gzip to a file.
38 enclosedReversedString - Convert alphanumerics in a string to enclosed reversed alphanumerics.
39 enclosedReversedStringUndo - Undo alphanumerics in a string to enclosed reversed alphanumerics.
40 enclosedString - Convert alphanumerics in a string to enclosed alphanumerics.
41 enclosedStringUndo - Undo alphanumerics in a string to enclosed alphanumerics.
42 encodeBase64 - Encode a string in base 64.
43 encodeJson - Encode Perl to Json.
44 evalFile - Read a file containing unicode in utf8, evaluate it, confess to any errors and then return any result - an improvement on do which silently ignores any problems.
45 evalGZipFile - Read a file containing compressed utf8, evaluate it, confess to any errors or return any result.
46 extractTest - Remove example markers from test code.
47 fe - Get extension of file name.
48 fileList - Files that match a given search pattern handed to bsd_glob.
49 fileModTime - Get the modified time of a file in seconds since the epoch.
50 fileOutOfDate - Calls the specified sub once for each source file that is missing, then calls the sub for the target if there were any missing files or if the target is older than any of the non missing source files or if the target does not exist.
51 filePath - Create a file name from an array of file name components.
52 filePathDir - Create a directory name from an array of file name components.
53 filePathExt - Create a file name from an array of file name components the last of which is an extension.
54 fileSize - Get the size of a file.
55 findDirs - Find all the folders under a folder and optionally filter the selected folders with a regular expression.
56 findFiles - Find all the files under a folder and optionally filter the selected files with a regular expression.
57 findFileWithExtension - Find the first extension from the specified extensions that produces a file that exists when appended to the specified file.
58 firstFileThatExists - Returns the name of the first file that exists or undef if none of the named files exist.
59 firstNChars - First N characters of a string.
60 fn - Remove path and extension from file name.
61 fne - Remove path from file name.
62 formatTable - Format various data structures as a table.
63 formatTableA - Tabularize an array.
64 formatTableAA - Tabularize an array of arrays.
65 formatTableAH - Tabularize an array of hashes.
66 formatTableBasic - Tabularize an array of arrays of text.
67 formatTableH - Tabularize a hash.
68 formatTableHA - Tabularize a hash of arrays.
69 formatTableHH - Tabularize a hash of hashes.
70 formatTableMultiLine - Tabularize text that has new lines in it.
71 fp - Get path from file name.
72 fpn - Remove extension from file name.
73 fullFileName - Full name of a file.
74 genHash - Return a $blessed hash with the specified $attributes.
75 genLValueArrayMethods - Generate lvalue array methods in the current package.
76 genLValueHashMethods - Generate lvalue hash methods in the current package.
77 genLValueScalarMethods - Generate lvalue scalar methods in the current package, A method whose value has not yet been set will return a new scalar with value undef.
78 genLValueScalarMethodsWithDefaultValues - Generate lvalue scalar methods with default values in the current package.
79 hostName - The name of the host we are running on.
80 htmlToc - Generate a table of contents for some html.
81 imageSize - Return (width, height) of an image obtained via Imagemagick.
82 indentString - Indent lines contained in a string or formatted table by the specified string.
83 ipAddressViaArp - Get the ip address of a server on the local network by hostname via arp
84 isBlank - Test whether a string is blank.
85 javaPackage - Extract the package name from a java string or file.
86 javaPackageAsFileName - Extract the package name from a java string or file and convert it to a file name.
87 keyCount - Count keys down to the specified level.
88 loadArrayArrayFromLines - Load an array of arrays from lines of text: each line is an array of words.
89 loadArrayFromLines - Load an array from lines of text in a string.
90 loadArrayHashFromLines - Load an array of hashes from lines of text: each line is an hash of words.
91 loadHash - Load the specified $hash generated with genHash with %attributes.
92 loadHashArrayFromLines - Load a hash of arrays from lines of text: the first word of each line is the key, the remaining words are the array contents.
93 loadHashFromLines - Load a hash: first word of each line is the key and the rest is the value.
94 loadHashHashFromLines - Load a hash of hashes from lines of text: the first word of each line is the key, the remaining words are the sub hash contents.
95 makeDieConfess - Force die to confess where the death occurred.
96 makePath - Make the path for the specified file name or folder.
97 matchPath - Given an absolute path find out how much of the path actually exists.
98 max - Find the maximum number in a list.
99 maximumLineLength - Find the longest line in a string
100 microSecondsSinceEpoch - Micro seconds since unix epoch.
101 min - Find the minimum number in a list.
102 newProcessStarter - Create a new process starter with which to start parallel processes up to a specified $maximumNumberOfProcesses maximum number of parallel processes at a time, wait for all the started processes to finish and then optionally retrieve their results from the folder named by $transferArea.
103 newServiceIncarnation - Create a new service incarnation to record the start up of a new instance of a service and return the description as a Data::Exchange::Service Definition hash.
104 numberOfLinesInFile - The number of lines in a file
105 numberOfLinesInString - The number of lines in a string.
106 nws - Normalize white space in a string to make comparisons easier.
107 overWriteFile - Write a unicode utf8 string to a file after creating a path to the file if necessary and return the name of the file on success else confess.
108 pad - Pad a string with blanks or the specified padding character to a multiple of a specified length.
109 parseCommandLineArguments - Classify the specified array of words referred to by $args into positional and keyword parameters, call the specified sub with a reference to an array of positional parameters followed by a reference to a hash of keywords and their values then return the value returned by this sub.
110 parseFileName - Parse a file name into (path, name, extension).
111 perlPackage - Extract the package name from a perl string or file.
112 powerOfTwo - Test whether a number is a power of two, return the power if it is else undef.
113 printFullFileName - Print a file name on a separate line with escaping so it can be used easily from the command line.
114 printQw - Print an array of words in qw() format.
115 quoteFile - Quote a file name.
116 readBinaryFile - Read binary file - a file whose contents are not to be interpreted as unicode.
117 readFile - Read a file containing unicode in utf8.
118 readGZipFile - Read the specified $file, containing compressed utf8, through gzip
119 readUtf16File - Read a file containing unicode in utf-16 format.
120 relFromAbsAgainstAbs - Derive a relative file name for the first absolute file name relative to the second absolute file name.
121 removeFilePrefix - Removes a file prefix from an array of files.
122 renormalizeFolderName - Normalize a folder name component by adding a trailing separator.
123 saveCodeToS3 - Save source code files.
124 saveSourceToS3 - Save source code.
125 searchDirectoryTreesForMatchingFiles - Search the specified directory trees for the files (not folders) that match the specified extensions.
126 setIntersectionOfTwoArraysOfWords - Intersection of two arrays of words.
127 setUnionOfTwoArraysOfWords - Union of two arrays of words.
128 startProcess - Start new processes while the number of child processes recorded in %$pids is less than the specified $maximum.
129 stringsAreNotEqual - Return the common start followwe by the two non equal tails of two non equal strings or an empty list if the strings are equal.
130 subScriptString - Convert alphanumerics in a string to sub scripts
131 subScriptStringUndo - Undo alphanumerics in a string to sub scripts
132 superScriptString - Convert alphanumerics in a string to super scripts
133 superScriptStringUndo - Undo alphanumerics in a string to super scripts
134 temporaryFile - Create a temporary file that will automatically be unlinked during END processing.
135 temporaryFolder - Create a temporary folder that will automatically be rmdired during END processing.
136 timeStamp - hours:minute:seconds
137 titleToUniqueFileName - Create a file name from a title that is unique within the set %uniqueNames.
138 trackFiles - Track the existence of files.
139 trim - Remove any white space from the front and end of a string.
140 updateDocumentation - Update documentation from the comments in a perl script.
141 updatePerlModuleDocumentation - Update the documentation in a perl file and show said documentation in a web browser.
142 userId - The userid we are currently running under.
143 versionCode - YYYYmmdd-HHMMSS
144 versionCodeDashed - YYYY-mm-dd-HH:MM:SS
145 waitForAllStartedProcessesToFinish - Wait until all the processes started by startProcess have finished.
146 writeBinaryFile - Write a non unicode string to a file in after creating a path to the file if necessary and return the name of the file on success else confess.
147 writeFile - Write a unicode utf8 string to a new file that does not already exist after creating a path to the file if necessary and return the name of the file on success else confess if a problem occurred or the file does already exist.
148 writeFiles - Write the values of a hash into files identified by the key of each value using overWriteFile
149 writeGZipFile - Write a unicode utf8 string through gzip to a file.
150 wwwEncode - Replace spaces in a string with %20 .
151 xxx - Execute a shell command.
152 yyy - Execute a block of shell commands line by line after removing comments - stop if there is a non zero return code from any command.
153 zzz - Execute lines of commands after replacing new lines with && then check that the pipeline execution results in a return code of zero and that the execution results match the optional regular expression if one has been supplied; confess() to an error if either check fails.
154 ˢ - Immediately executed inline sub to allow a code block before if.
Installation
This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:
sudo cpan install Data::Table::Text
Author
Copyright
Copyright (c) 2016-2018 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
Acknowledgements
Thanks to the following people for their help with this module:
- mim@cpan.org
-
Testing on windows