Puma
|
|
The Puma::Util modules are a series of rather useful utilities that can
be used by Perl programmers even if they choose not to implement any of
the other Puma components (such as the ServerPages module).
Puma::Util
The Puma:Util package contains exported functions that could not be (or
have not yet been) lumped into one of the other Util packages.
Any of the functions in this package may be deprecated in the future for
inclusion in a more specific package.
READFILE() AND WRITEFILE()
Perl has many methods to read and writ to files, but I have often found
that all I really needed is a single function that either reads or writes
an entire file.
These two methods provide that functionality.
use Puma::Util qw( readfile writefile); my $content = readfile('foo.txt'); writefile('foo2.txt', $content);
The above example demonstrates how to read the content of a file into a
scalar variable and then write that same content back out to a new file.
A simple example, but they're simple functions.
SPLITHASH() AND MERGEHASH()
The splithash() function takes a hash and splits it into two mased on a
list of keys that you give it.
The mergehash() function takes two hashes and merges them into one.
use Puma::Util qw( splithash mergehash ); my $hash1 = { one => 1, two => 2, three => 3, four => 4 }; my $hash2 = splithash($hash1, 'two', 'four'); my $rehash1 = mergehash($hash1, $hash2);
mergehash() has an additional feature where it will recursively copy the
content of any nested hashes rather than simply copying the reference.
QUOTE()
The quote function returns a safely quoted string using Perl's qq quoting
mechanism:
use Puma::Util qw( quote ); print quote('Plain old string') ."\n"; print quote('String with [square braces]') ."\n"; print quote('String with (round braces)') ."\n"; print quote('String with [square braces] and (round braces)') ."\n";
will create the output:
qq[Plain old string] qq(String with [square braces]) qq[String with (round braces)] qq(String with [square braces] and \(round braces\)) |