NAME
Data::Util - various utility functions
SYNOPSIS
use Data::Util;
my $re = list2re qw/foo bar baz/;
print byval { s/foo/bar/ } $text;
foo(mapval { chomp } @lines);
DESCRIPTION
This module defines a few generally useful utility functions. I got tired of redefining or working around them, so I wrote this module.
Functions
- list2re LIST
-
Converts a list of strings to a regex that matches any of the strings. Especially useful in combination with
keys
. Example:my $re = list2re keys %hash; $str =~ s/($re)/$hash{$1}/g;
- byval BLOCK SCALAR
-
Takes a code block and a value, runs the block with
$_
set to that value, and returns the final value of$_
. The global value of$_
is not affected.$_
isn't aliased to the input value either, so modifying$_
in the block will not affect the passed in value. Example:foo(byval { s/!/?/g } $str); # Calls foo() with the value of $str, but all '!' have been replaced by '?'. # $str itself is not modified.
- mapval BLOCK LIST
-
Works like a combination of
map
andbyval
; i.e. it behaves likemap
, but$_
is a copy, not aliased to the current element, and the return value is taken from$_
again (it ignores the value returned by the block). Example:my @foo = mapval { chomp } @bar; # @foo contains a copy of @bar where all elements have been chomp'd. # This could also be written as chomp(my @foo = @bar); but that's not # always possible.
AUTHOR
Lukas Mai, <l.mai at web.de>
COPYRIGHT & LICENSE
Copyright 2009 Lukas Mai.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.