Data-Table-Text
view release on metacpan or search on metacpan
lib/Data/Table/Text.pm view on Meta::CPAN
sub writeGZipFile($$) # Write to a B<$file>, after creating a path to the file with L<makePath> if necessary, through L<gzip> a B<$string> whose content is encoded as L<utf8>.
{my ($file, $string) = @_; # File to write to, string to write
makePath($file);
open my $F, "| gzip>$file" or # Compress via gzip
confess "Cannot open file for write because:\n$file\n$!\n";
binmode($F, ":utf8"); # Input to gzip encoded as utf8
print {$F} $string;
close ($F);
-e $file or confess "Failed to write to file:\n$file\n";
$file
} # writeGZipFile
sub dumpGZipFile($$) # Write to a B<$file> a data B<$structure> through L<gzip>. This technique produces files that are a lot more compact files than those produced by L<Storable>, but the ex...
{my ($file, $structure) = @_; # File to write, reference to data
ref($structure) or confess "\$structure must contain a reference to data, not a scalar";
writeGZipFile($file, dump($structure));
} # dumpGZipFile
sub writeFiles($;$$) # Write the values of a B<$hash> reference into files identified by the key of each value using L<overWriteFile|/overWriteFile> optionally swapping the prefix of each fil...
{my ($hash, $old, $new) = @_; # Hash of key value pairs representing files and data, optional old prefix, new prefix
for my $file(sort keys %$hash) # Write file data for each hash key
{my $target = $old && $new ? swapFilePrefix($file, $old, $new) : $file; # Optionally swap file prefix
overWriteFile($file, $hash->{$file})
}
} # writeFiles
sub readFiles(@) # Read all the files in the specified list of folders into a hash.
{my (@folders) = @_; # Folders to read
my %h;
for my $file(searchDirectoryTreesForMatchingFiles(@folders)) # Files
{eval {$h{$file} = readFile($file)};
}
\%h
} # readFiles
sub includeFiles($) # Read the given file and expand all lines that start "includeThisFile " with the file named by the rest of the line and keep doing this until all the included files have...
{my ($expand) = @_; # File to expand
my %expanded; # Files already expanded
my @l = readFile $expand; # Read the first file
for my $i(1..99) # Limit on the number of expansion passes
{my @L; my $changed = 0; # Latest expansion
for my $l(@l) # Each line
{if ($l =~ m(\AincludeThisFile\s(.*)\Z))
{my $f = $1;
confess "No such file:\n$f\n" unless -e $f;
confess "File already included:\n$f\n" if $expanded{$f}++;
push @L, readFile $f;
++$changed;
}
else
{push @L, $l;
}
}
return @l unless $changed; # Return array of expanded lines if expansion is now complete
@l = @L;
}
confess "Expansion too deep";
} # includeFiles
sub appendFile($$) # Append to B<$file> a B<$string> of L<unicode> content encoded with L<utf8>, creating the $file first if necessary. Return the name of the $file on success else confess....
{my ($file, $string) = @_; # File to append to, string to append
$file or confess "No file name supplied\n";
$string or carp "No string for file:\n$file\n";
makePath($file);
open my $F, ">>$file" or
confess "Cannot open file for write file:\n$file\n$!\n";
binmode($F, ":utf8");
flock($F, 2);
print {$F} $string;
close ($F);
-e $file or confess "Failed to write to file:\n$file\n";
$file
} # appendFile
sub createEmptyFile($) # Create an empty file unless the file already exists and return the name of the file else confess if the file cannot be created.
{my ($file) = @_; # File to create or B<undef> for a temporary file
$file //= temporaryFile;
return $file if -e $file; # Return file name as proxy for success if file already exists
makePath($file);
open my $F, ">$file" or confess "Cannot create empty file:\n$file\n$!\n";
binmode($F);
print {$F} '';
close ($F);
-e $file or confess "Failed to create empty file:\n$file\n";
$file # Return file name on success
} # createEmptyFile
sub binModeAllUtf8 #P Set STDOUT and STDERR to accept utf8 without complaint.
{binmode $_, ":utf8" for *STDOUT, *STDERR;
}
sub setPermissionsForFile($$) # Apply L<chmod> to a B<$file> to set its B<$permissions>.
{my ($file, $permissions) = @_; # File, permissions settings per chmod
return undef unless confirmHasCommandLineCommand(q(chmod)); # Confirm we have chmod
qx(chmod $permissions $file); # Use chmod to set permissions
}
sub numberOfLinesInFile($) # Return the number of lines in a file.
{my ($file) = @_; # File
scalar split /\n/, readFile($file); # Number of lines
} # numberOfLinesInFile
sub overWriteHtmlFile($$) # Write an L<html> file to /var/www/html and make it readable.
{my ($file, $data) = @_; # Target file relative to /var/www/html, data to write
my $s = writeTempFile($data);
my $t = fpf(q(/var/www/html/), $file);
xxx qq(sudo mv $s $t; chmod o+r $t);
unlink $s;
}
sub overWritePerlCgiFile($$) # Write a L<Perl> file to /usr/lib/cgi-bin and make it executable after checking it for syntax errors.
{my ($file, $data) = @_; # Target file relative to /var/www/html, data to write
my $s = writeTempFile($data);
my $r = qx(perl -c $s 2>&1);
if ($r =~ m(syntax OK)si)
{my $t = fpf(q(/usr/lib/cgi-bin/), $file);
say STDERR qx(sudo mv $s $t; chmod o+rx $t);
}
else
{my @data = map {[$_]} split m/\n/, $data;
lib/Data/Table/Text.pm view on Meta::CPAN
unlink $a2;
mergeFolder($a, $b);
ok -e $b1; ok -e $b2;
copyFolder($a, $b);
ok -e $b1; ok !-e $b2;
copyFile($a1, $a2);
ok readFile($a1) eq readFile($a2);
writeFiles($files);
ok !moveFileNoClobber ($a1, $a2);
ok moveFileWithClobber($a1, $a2);
ok !-e $a1;
ok readFile($a2) eq q(1111);
ok moveFileNoClobber ($a2, $a1);
ok !-e $a2;
ok readFile($a1) eq q(1111);
clearFolder(q(aaa), 11);
clearFolder(q(bbb), 11);
=head3 includeFiles($expand)
Read the given file and expand all lines that start "includeThisFile " with the file named by the rest of the line and keep doing this until all the included files have been expanded or a repetition is detected. Returns the expanded file or confesse...
Parameter Description
1 $expand File to expand
B<Example:>
if (1)
{my $d = temporaryFolder;
my $a = "$d/a.txt";
my $b = "$d/b.txt";
my %d = ($a => <<END,
aaa
includeThisFile $d/b.txt
ccc
END
$b => <<END,
bbb
END
);
writeFiles(\%d);
is_deeply [includeFiles($a)], ["aaa
", "bbb
", "ccc
"]; # ðð
ð®ðºð½ð¹ð²
unlink $a, $b;
}
=head3 appendFile  ($file, $string)
Append to B<$file> a B<$string> of L<Unicode|https://en.wikipedia.org/wiki/Unicode> content encoded with L<utf8|https://en.wikipedia.org/wiki/UTF-8>, creating the $file first if necessary. Return the name of the $file on success else confess. The $fi...
Parameter Description
1 $file File to append to
2 $string String to append
B<Example:>
my $f = writeFile(undef, "aaa");
is_deeply [readFile $f], ["aaa"];
appendFile($f, "bbb"); # ðð
ð®ðºð½ð¹ð²
is_deeply [readFile $f], ["aaabbb"];
my $F = writeTempFile(qw(aaa bbb));
is_deeply [readFile $F], ["aaa
", "bbb
"];
eval {writeFile($f, q(ccc))};
ok $@ =~ m(File already exists:)i;
overWriteFile($F, q(ccc));
ok readFile($F) eq q(ccc);
unlink $f, $F;
=head3 createEmptyFile ($file)
Create an empty file unless the file already exists and return the name of the file else confess if the file cannot be created.
Parameter Description
1 $file File to create or B<undef> for a temporary file
B<Example:>
my $D = temporaryFolder;
ok -d $D;
my $d = fpd($D, q(ddd));
ok !-d $d;
my @f = map {createEmptyFile(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)];
my @D = findDirs($D);
my @e = ($D, $d);
my @E = sort @e;
is_deeply [@D], [@E];
is_deeply [sort map {fne $_} searchDirectoryTreesForMatchingFiles($d)],
["a.txt", "b.txt", "c.txt"];
is_deeply [sort map {fne $_} fileList(prefferedFileName "$d/*.txt")],
( run in 1.620 second using v1.01-cache-2.11-cpan-39bf76dae61 )