background preloader

Packages

Facebook Twitter

Child. Child - Object oriented simple interface to fork() Fork is too low level, and difficult to manage. Often people forget to exit at the end, reap their children, and check exit status. The problem is the low level functions provided to do these things. Throw in pipes for IPC and you just have a pile of things nobody wants to think about. Child is an Object Oriented interface to fork. It provides a clean way to start a child process, and manage it afterwords. It provides methods for running, waiting, killing, checking, and even communicating with a child process.

NOTE: kill() is unpredictable on windows, strawberry perl sends the kill signal to the parent as well as the child. use Child; my $child = Child->new(sub { my ( $parent ) = @_; .... }); my $proc = $child->start $proc->complete || $proc->kill(9); $proc->wait; Child can export the child() shortcut function when requested. Use Child qw/child/; my $proc = child { my $parent = shift; ... }; You can also request IPC: Child->wait_all()

Carp. Carp - alternative warn and die for modules use Carp; carp "string trimmed to 80 chars"; croak "We're outta here! "; confess "not implemented"; use Carp qw(cluck); cluck "This is how we got here! "; The Carp routines are useful in your own modules because they act like die() or warn(), but with a message which is more likely to be useful to a user of your module. In the case of cluck, confess, and longmess that context is a summary of every call in the call-stack. You can also alter the way the output and logic of Carp works, by changing some global variables in the Carp namespace. Here is a more complete description of how carp and croak work. Any call from a package to itself is safe. Forcing a Stack Trace As a debugging aid, you can force Carp to treat a croak as a confess and a carp as a cluck across all modules.

This feature is enabled by 'importing' the non-existent symbol 'verbose'. Perl -MCarp=verbose script.pl Alternately, you can set the global variable $Carp::Verbose to true. Hash::Merge::Simple. Hash::Merge::Simple - Recursively merge two or more hashes, simply version 0.051 use Hash::Merge::Simple qw/ merge /; my $a = { a => 1 }; my $b = { a => 100, b => 2}; my $c = merge $a, $b; my $a = { a => 1, c => 3, d => { i => 2 }, r => {} }; my $b = { b => 2, a => 100, d => { l => 4 } }; my $c = merge $a, $b; my $everything = merge $this, $that, $mine, $yours, $kitchen_sink, ...; Hash::Merge::Simple will recursively merge two or more hashes and return the result as a new hash reference.

This code was pretty much taken straight from Catalyst::Utils, and modified to handle more than 2 hashes at the same time. Hash::Merge::Simple->merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) Hash::Merge::Simple::merge( <hash1>, <hash2>, <hash3>, ..., <hashN> ) Merge <hash1> through <hashN>, with the nth-most (rightmost) hash taking precedence.

Returns a new hash reference representing the merge. NOTE: The code does not currently check for cycles, so infinite loops are possible: Hash::Merge Catalyst::Utils Clone. Path::Class. Path::Class - Cross-platform path specification manipulation version 0.24 use Path::Class; my $dir = dir('foo', 'bar'); my $file = file('bob', 'file.txt'); print "dir: $dir\n"; print "file: $file\n"; my $subdir = $dir->subdir('baz'); my $parent = $subdir->parent; my $parent2 = $parent->parent; my $dir2 = $file->dir; use Path::Class qw(foreign_file foreign_dir); my $file = foreign_file('Mac', ':foo:file.txt'); print $file->dir; print $file->as_foreign('Win32'); my $dir_handle = $dir->open or die "Can't read $dir: $!

"; my $file_handle = $file->open($mode) or die "Can't read $file: $! "; Path::Class is a module for manipulation of file and directory specifications (strings describing their locations, like '/home/ken/foo.txt' or 'C:\Windows\Foo.txt') in a cross-platform manner. In fact, Path::Class uses File::Spec internally, wrapping all the unsightly details so you can concentrate on your application code. Can be written using Path::Class as or even as file A synonym for Path::Class::File->new. Text::Wrap. Text::Wrap - line wrapping to form simple paragraphs Example 1 use Text::Wrap; $initial_tab = "\t"; $subsequent_tab = ""; print wrap($initial_tab, $subsequent_tab, @text); print fill($initial_tab, $subsequent_tab, @text); $lines = wrap($initial_tab, $subsequent_tab, @text); @paragraphs = fill($initial_tab, $subsequent_tab, @text); Example 2 use Text::Wrap qw(wrap $columns $huge); $columns = 132; $huge = 'die'; $huge = 'wrap'; $huge = 'overflow'; Example 3 use Text::Wrap; $Text::Wrap::columns = 72; print wrap('', '', @text); Text::Wrap::wrap() is a very simple paragraph formatter.

Text::Wrap::fill() is a simple multi-paragraph formatter. Both wrap() and fill() return a single string. Text::Wrap::wrap() has a number of variables that control its behavior. Lines are wrapped at $Text::Wrap::columns columns (default value: 76). It is possible to control which characters terminate words by modifying $Text::Wrap::break. Historical notes: 'die' used to be the default value of $huge. Code: Result: List::MoreUtils. List::MoreUtils - Provide the stuff missing in List::Util use List::MoreUtils qw{ any all none notall true false firstidx first_index lastidx last_index insert_after insert_after_string apply indexes after after_incl before before_incl firstval first_value lastval last_value each_array each_arrayref pairwise natatime mesh zip uniq distinct minmax part }; List::MoreUtils provides some trivial but commonly needed functionality on lists which is not going to go into List::Util.

All of the below functions are implementable in only a couple of lines of Perl code. Using the functions from this module however should give slightly better performance as everything is implemented in C. Any BLOCK LIST Returns a true value if any item in LIST meets the criterion given through BLOCK. Print "At least one value undefined" if any { ! Returns false otherwise, or if LIST is empty. all BLOCK LIST Returns a true value if all items in LIST meet the criterion given through BLOCK, or if LIST is empty. Example: or. URI. URI - Uniform Resource Identifiers (absolute and relative) $u1 = URI->new(" $u2 = URI->new("foo", "http"); $u3 = $u2->abs($u1); $u4 = $u3->clone; $u5 = URI->new(" $str = $u->as_string; $str = "$u"; $scheme = $u->scheme; $opaque = $u->opaque; $path = $u->path; $frag = $u->fragment; $u->scheme("ftp"); $u->host("ftp.perl.com"); $u->path("cpan/"); This module implements the URI class.

Objects of this class represent "Uniform Resource Identifier references" as specified in RFC 2396 (and updated by RFC 2732). A Uniform Resource Identifier is a compact string of characters that identifies an abstract or physical resource. A Uniform Resource Identifier can be further classified as either a Uniform Resource Locator (URL) or a Uniform Resource Name (URN). The distinction between URL and URN does not matter to the URI class interface. <scheme>:<scheme-specific-part><scheme>://<authority><path>? The following methods construct new URI objects:

English. English - use nice English (or awk) names for ugly punctuation variables use English qw( -no_match_vars ) ; use English; ... if ($ERRNO =~ /denied/) { ... } This module provides aliases for the built-in variables whose names no one seems to like to read. Variables with side-effects which get triggered just by accessing them (like $0) will still be affected. For those variables that have an awk version, both long and short English alternatives are provided. For example, the $/ variable can be referred to either $RS or $INPUT_RECORD_SEPARATOR if you are using the English module. See perlvar for a complete list of these. This module can provoke sizeable inefficiencies for regular expressions, due to unfortunate implementation details.

Use English qw( -no_match_vars ) ; . Syntax highlighting: File::chdir. File::chdir - a more sensible way to change directories This documentation describes version 0.1004. use File::chdir; $CWD = "/foo/bar"; { local $CWD = "/moo/baz"; ... } Perl's chdir() has the unfortunate problem of being very, very, very global. If any part of your program calls chdir() or if any library you use calls chdir(), it changes the current working directory for the whole program.

This sucks. File::chdir gives you an alternative, $CWD and @CWD. These two variables combine all the power of chdir(), File::Spec and Cwd. Use the $CWD variable instead of chdir() and Cwd. use File::chdir; $CWD = $dir; print $CWD; It can be localized, and it does the right thing. $CWD = "/foo"; { local $CWD = "/bar"; } $CWD always returns the absolute path in the native form for the operating system.

$CWD and normal chdir() work together just fine. @CWD represents the current working directory as an array, each directory in the path is an element of the array. @CWD = qw(usr local src perl); $CWD = 'foo'; File::stat. File::stat - by-name interface to Perl's built-in stat() functions This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. This object has methods that return the similarly named structure field name from the stat(2) function; namely, dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks.

As of version 1.02 (provided with perl 5.12) the object provides "-X" overloading, so you can call filetest operators (-f, -x, and so on) on it. It also provides a ->cando method, called like $st->cando( ACCESS, EFFECTIVE ) where ACCESS is one of S_IRUSR, S_IWUSR or S_IXUSR from the Fcntl module, and EFFECTIVE indicates whether to use effective (true) or real (false) ids.

If you don't want to use the objects, you may import the ->cando method into your namespace as a regular function called stat_cando. My $stat_obj = stat $_; my $stat_obj = File::stat::populate(CORE::stat(_));