Useful Perl Modules
A lightning talk for the
Perl Workshop in Israel, 2007,
by Mikhael Goikhman.
See index page.
Eight Useful Modules
A quick introduction into the following useful modules:
-
File::Operations (save_file, load_file, remove_dir and many more)
-
Text::WikiText (this whole abstract is written in WikiText)
-
Data::NeatDump (neater than Data::Dumper)
-
Data::NeatFormat (show scalars and lists nicely)
-
Date::TimeFormat (there is more than one way to do it)
-
Exception (C++/Java like syntax try/catch/throw with call stack)
-
Parse::PerlConf (write configuration files using perl syntax)
-
Parse::Copec64 (store any complex perl data using letters and digits only)
BTW, the source of this slide is in Text::WikiText format.
File::Operations
Saving file
save_file($filename, $content);
save_file($filename, $content_ref);
save_file($filename, \@lines);
save_file($filename, $content, create_dirs => 1, is_utf8 => 0);
append_file($filename, $content);
Loading file
my $content = load_file($filename);
load_file($filename, \$content);
load_file($filename, \@lines);
my $content = load_file($filename, is_utf8 => 1);
File::Operations (cont.)
Other file-system operations
make_path("/tmp/my-own/dir0");
make_dir("/tmp/my-own/dir", 0711);
copy_file("/etc/issue", "/tmp/my-own/dir/issue");
remove_file("/tmp/my-own/dir/issue2");
clean_dir("/tmp/my-own/dir"); # no effect, empty already
remove_dir("/tmp/my-own");
Saving/loading tabular fields (TAB separated lines)
save_field_file($filename, $lines_of_fields);
my $lines_of_fields2 = load_field_file($filename);
save_field_file($filename, $table, create_dirs => 1, delim => ',');
my $table2 = load_field_file($filename, delim => ',');
Text::WikiText
Simple human-readable syntax
This is a new wiki text. Write bold words, italic words,
underlined words and more.
Paragraphs are delimited by an empty line.
See
syntax specification
for the exact rules.
Tables
Tables | are | simple |
1 | 2 | 3 |
4 | two columns |
5 | 6 | 7 |
Another table:
HTTP | codes |
301 | Moved |
302 | Found |
403 | Forbidden |
404 | Not found |
500 | Internal Error |
See the source of this slide.
Text::WikiText (cont.)
Lists
There are several kinds of lists
-
regular
-
numbered, like this:
-
good
-
bad
-
ugly
-
may be nested, if needed
More examples
preformatted text
as is
if ($condition) {
print "This is a code.";
}
Quotations:
A language that doesn't affect the way you think about
programming, is not worth knowing.
-- Alan Perlis
See the source of this slide.
Data::NeatDump
use Data::NeatDump;
print dump($long_struct);
print Dump($data1, $data2, $data3);
[
"First element",
{
key1 => "Short string",
key2 => "Another string",
},
[
2.71828182845905,
3.14159265358979,
],
]
Why not Data::Dumper?
Several reasons:
-
shorter usage
-
cleaner API
-
nicer and more structured output by default
-
hash keys are sorted
-
cuts long structures using ellipsis
-
but: for neat presentation, not general serialization