CINXE.COM
Data::Munge - various utility functions - metacpan.org
<!DOCTYPE html> <html lang="en-US"> <head> <title>Data::Munge - various utility functions - metacpan.org</title> <link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" /> <link href="/assets/style-XFEQ536G.css" rel="stylesheet" type="text/css"> <script src="/assets/main-WXBSEDQJ.js" type="module"></script> <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Data-Munge - MetaCPAN" href="/dist/Data-Munge/releases.rss" /> <link rel="canonical" href="https://metacpan.org/pod/Data::Munge" /> <meta name="description" content="various utility functions" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5"> <link rel="shortcut icon" href="/static/icons/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-touch-icon.png"> <link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN"> <script async src="https://perl-ads.perlhacks.com/perl-ads.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-E82Q2V8LVD"></script> <meta property="og:site_name" content="MetaCPAN" /> <meta name="twitter:site" content="@metacpan" /> <meta property="og:type" content="article" /> <meta name="twitter:card" content="summary" /> <meta property="og:url" content="https://metacpan.org/pod/Data::Munge" /> <meta property="og:title" content="Data::Munge" /> <meta property="og:description" content="various utility functions" /> <meta property="og:image" content="https://metacpan.org/static/images/dots.png" /> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="header-logo-large hidden-xs"> <a href="/" tabindex="0"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#logo" /> </svg> </a> </div> <div class="header-logo-icon visible-xs"> <a href="/"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#dots" /> </svg> </a> </div> <ul class="nav navbar-nav menu-items hidden-xs hidden-sm"> <li><a href="/about">About</a></li> <li><a href="/about/sponsors">Sponsor</a></li> <li><a href="https://grep.metacpan.org/">grep::cpan</a></li> <li><a href="/recent">Recent</a></li> <li><a href="/about/faq">FAQ</a></li> <li><a href="/tools">Tools</a></li> <li><a href="https://fastapi.metacpan.org/">API</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <button type="button" class="searchbar-btn visible-xs visible-sm"> <i class="fa fa-search button-fa-icon"></i> </button> <form action="/search" class="searchbar-form visible-md visible-lg search-form form-horizontal"> <input type="hidden" name="size" id="metacpan_search-size" value="20"> <div class="form-group"> <div class="search-group"> <i class="fa fa-search"></i> <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value=""> </div> </div> </form> <li class="icon-slidepanel visible-xs visible-sm"> <button data-toggle="slidepanel" data-target=".slidepanel"> <span class="button-fa-icon"> <i class="fa fa-bars slidepanel-open"></i> <i class="fa fa-times slidepanel-close"></i> </span> </button> </li> <li class="dropdown login-dropdown show-logged-in"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li><a href="/account/identities">Identities</a></li> <li><a href="/account/profile">Profile</a></li> <li><a href="/account/favorite/list">Favorites</a></li> <li> <a href="#" type="button" class="logout-button"> Logout </a> </li> </ul> </li> <li class="dropdown login-dropdown show-logged-out"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon avatar-placeholder" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li> <a href="/login/github"> <i class="fab fa-github fa-fw"></i> GitHub </a> </li> <li> <a href="/login/twitter"> <i class="fab fa-twitter fa-fw"></i> Twitter </a> </li> <li> <a href="/login/google"> <i class="fab fa-google fa-fw"></i> Google </a> </li> </ul> </li> <li class="dropdown login-dropdown hide-logged-in hide-logged-out"> <button> <i class="fa fa-user button-fa-icon" aria-hidden="true"></i> </button> </li> </ul> </nav> <div class="page-content "> <div id="perl-ad-target" class="top-notify-banner perl-ad-target"> </div> <nav class="sidebar"> <div class="slidepanel"> <ul class="nav-list "> <li class="nav-header no-margin-top"> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Data-Munge was released."> <span class="relatize">08 Jul 2024 20:42:08 UTC</span> </div> </li> <li> Distribution: <a href="/dist/Data-Munge">Data-Munge</a> </li> <li> Module version: 0.111 </li> <li> <a data-keyboard-shortcut="g s" href="/dist/Data-Munge/source/lib/Data/Munge.pm">Source</a> (<a href="/dist/Data-Munge/source/lib/Data/Munge.pm?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g b" href="/dist/Data-Munge/source/lib/Data">Browse</a> (<a href="/dist/Data-Munge/source/lib/Data?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g c" href="/dist/Data-Munge/changes">Changes</a> </li> <li> <a class="nopopup" href="/dist/Data-Munge/contribute">How to Contribute</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="https://github.com/mauke/Data-Munge">Repository</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Munge">Issues</a> (0) </li> <li> <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Data-Munge+0.111" title="Matrix">Testers</a> <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Munge.html?oncpan=1&distmat=1&version=0.111&grade=2" style="color: #090">403</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Munge.html?oncpan=1&distmat=1&version=0.111&grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Munge.html?oncpan=1&distmat=1&version=0.111&grade=4">0</a>)</span> </li> <li> <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/MAUKE/Data-Munge-0.111">Kwalitee</a> </li> <li> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Data-Munge who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)"> Bus factor: 1 </div> </li> <li> License: perl_5 </li> <li class="nav-header">Activity</li> <li> <div class="activity-graph"> <img src="/dist/Data-Munge/activity.svg?res=month" /> <div class="comment">24 month</div> </div> </li> <li class="nav-header">Tools</li> <li> <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/M/MA/MAUKE/Data-Munge-0.111.tar.gz"> Download (<span itemprop="fileSize">10.34KB</span>)</a> </li> <li> <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FMAUKE%2FData-Munge-0.111%2Flib%2FData%2FMunge.pm"> MetaCPAN Explorer </a> </li> <li> <a href="/dist/Data-Munge/permissions"> Permissions </a> </li> <li> <a href="/dist/Data-Munge/releases.rss"> Subscribe to distribution </a> </li> <li> <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog"> Install Instructions </button> </li> <li> <form action="/search"> <input type="hidden" name="q" value="dist:Data-Munge"> <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li> <form action="https://grep.metacpan.org/search"> <input type="hidden" name="qd" value="Data-Munge"> <input type="hidden" name="source" value="metacpan"> <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li class="version-jump"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Jump to version</option> <option disabled value="/release/MAUKE/Data-Munge-0.111/view/lib/Data/Munge.pm" >0.111 (MAUKE on 2024-07-08)</option> <option value="/release/MAUKE/Data-Munge-0.101/view/lib/Data/Munge.pm" >0.101 (MAUKE on 2024-04-04)</option> <option value="/release/MAUKE/Data-Munge-0.10/view/lib/Data/Munge.pm" >0.10 (MAUKE on 2023-03-21)</option> <option value="/release/MAUKE/Data-Munge-0.097/view/lib/Data/Munge.pm" >0.097 (MAUKE on 2017-03-17)</option> <optgroup label="BackPAN">' <option value="/release/MAUKE/Data-Munge-0.096/view/lib/Data/Munge.pm" >0.096 (MAUKE on 2016-03-02)</option> <option value="/release/MAUKE/Data-Munge-0.095/view/lib/Data/Munge.pm" >0.095 (MAUKE on 2015-05-08)</option> <option value="/release/MAUKE/Data-Munge-0.094/view/lib/Data/Munge.pm" >0.094 (MAUKE on 2015-03-08)</option> <option value="/release/MAUKE/Data-Munge-0.093/view/lib/Data/Munge.pm" >0.093 (MAUKE on 2014-12-23)</option> <option value="/release/MAUKE/Data-Munge-0.092/view/lib/Data/Munge.pm" >0.092 (MAUKE on 2014-11-25)</option> <option value="/release/MAUKE/Data-Munge-0.091/view/lib/Data/Munge.pm" >0.091 (MAUKE on 2014-11-19)</option> <option value="/release/MAUKE/Data-Munge-0.09/view/lib/Data/Munge.pm" >0.09 (MAUKE on 2014-11-18)</option> <option value="/release/MAUKE/Data-Munge-0.08/view/lib/Data/Munge.pm" >0.08 (MAUKE on 2014-09-15)</option> <option value="/release/MAUKE/Data-Munge-0.07/view/lib/Data/Munge.pm" >0.07 (MAUKE on 2013-10-22)</option> <option value="/release/MAUKE/Data-Munge-0.06/view/lib/Data/Munge.pm" >0.06 (MAUKE on 2013-03-07)</option> <option value="/release/MAUKE/Data-Munge-0.05/view/lib/Data/Munge.pm" >0.05 (MAUKE on 2013-03-05)</option> <option value="/release/MAUKE/Data-Munge-0.04/view/lib/Data/Munge.pm" >0.04 (MAUKE on 2011-08-03)</option> <option value="/release/MAUKE/Data-Munge-0.032/view/lib/Data/Munge.pm" >0.032 (MAUKE on 2010-01-23)</option> <option value="/release/MAUKE/Data-Munge-0.031/view/lib/Data/Munge.pm" >0.031 (MAUKE on 2010-01-20)</option> <option value="/release/MAUKE/Data-Munge-0.03/view/lib/Data/Munge.pm" >0.03 (MAUKE on 2010-01-19)</option> <option value="/release/MAUKE/Data-Munge-0.02/view/lib/Data/Munge.pm" >0.02 (MAUKE on 2009-12-14)</option> </optgroup> </select> </li> <li class="version-diff"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Diff with version</option> <option disabled value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.111/lib/Data/Munge.pm" >0.111 (MAUKE on 2024-07-08)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.101/lib/Data/Munge.pm" >0.101 (MAUKE on 2024-04-04)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.10/lib/Data/Munge.pm" >0.10 (MAUKE on 2023-03-21)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.097/lib/Data/Munge.pm" >0.097 (MAUKE on 2017-03-17)</option> <optgroup label="BackPAN">' <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.096/lib/Data/Munge.pm" >0.096 (MAUKE on 2016-03-02)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.095/lib/Data/Munge.pm" >0.095 (MAUKE on 2015-05-08)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.094/lib/Data/Munge.pm" >0.094 (MAUKE on 2015-03-08)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.093/lib/Data/Munge.pm" >0.093 (MAUKE on 2014-12-23)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.092/lib/Data/Munge.pm" >0.092 (MAUKE on 2014-11-25)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.091/lib/Data/Munge.pm" >0.091 (MAUKE on 2014-11-19)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.09/lib/Data/Munge.pm" >0.09 (MAUKE on 2014-11-18)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.08/lib/Data/Munge.pm" >0.08 (MAUKE on 2014-09-15)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.07/lib/Data/Munge.pm" >0.07 (MAUKE on 2013-10-22)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.06/lib/Data/Munge.pm" >0.06 (MAUKE on 2013-03-07)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.05/lib/Data/Munge.pm" >0.05 (MAUKE on 2013-03-05)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.04/lib/Data/Munge.pm" >0.04 (MAUKE on 2011-08-03)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.032/lib/Data/Munge.pm" >0.032 (MAUKE on 2010-01-23)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.031/lib/Data/Munge.pm" >0.031 (MAUKE on 2010-01-20)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.03/lib/Data/Munge.pm" >0.03 (MAUKE on 2010-01-19)</option> <option value="/release/MAUKE/Data-Munge-0.111/diff/MAUKE/Data-Munge-0.02/lib/Data/Munge.pm" >0.02 (MAUKE on 2009-12-14)</option> </optgroup> </select> </li> <li> <ul class="dependencies"> <li class="nav-header">Dependencies</li> <li><a href="/pod/strict" title="strict" class="ellipsis">strict</a></li> <li><a href="/pod/warnings" title="warnings" class="ellipsis">warnings</a></li> <li> <hr> </li> <li> <a href="/module/Data::Munge/requires">Reverse dependencies</a> </li> <li> <a href="http://deps.cpantesters.org/?module=Data%3A%3AMunge">CPAN Testers List</a> </li> <li> <a href="https://cpandeps.grinnz.com/?dist=Data-Munge">Dependency graph</a> </li> </ul> </li> <li class="nav-header">Permalinks</li> <li> <a href="/release/MAUKE/Data-Munge-0.111/view/lib/Data/Munge.pm">This version</a> </li> <li> <a href="/pod/Data::Munge">Latest version</a> </li> <li> <div class="plussers"> <div class="nav-header">++ed by:</div> <div> <a class="display-all" href="/author/SHLOMIF"><img src="https://www.gravatar.com/avatar/072331b92b0e6e9d9bc31c3af8f8ccea?d=identicon&s=20" title="SHLOMIF" alt="SHLOMIF"></a> <a class="display-all" href="/author/EGOR"><img src="https://www.gravatar.com/avatar/d1b2873e176897abb1f26be3a92e771d?d=identicon&s=20" title="EGOR" alt="EGOR"></a> <a class="display-all" href="/author/DRAEGTUN"><img src="https://www.gravatar.com/avatar/6f1ae19bf6507a7bd1ea0d0f6abfeb01?d=identicon&s=20" title="DRAEGTUN" alt="DRAEGTUN"></a> </div> <!-- Display counts of plussers--> <div> <a href="/dist/Data-Munge/plussers">3 PAUSE users</a> </div> <div> 4 non-PAUSE users </div> </div> </li> <li> <div> <button class="contributors-show-button btn-link">and 1 contributors</button> <div id="metacpan_contributors" class="slide-out slide-out-hidden"> <div> <ul> <li class="contributor" data-contrib-email="l.mai@web.de" > <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&s=20" /> Lukas Mai </li> </ul> </div> </div> </div> </li> </ul> </div> </nav> <div class="content-navigation"> <div class="breadcrumbs"> <span> <a data-keyboard-shortcut="g a" rel="author" href="/author/MAUKE" class="author-name">Lukas Mai</a> </span> <span> / </span> <div class="release dist-release status-latest maturity-released"> <span class="dropdown"><b class="caret"></b></span> <select class="select-navigator "> <option selected value="/release/MAUKE/Data-Munge-0.111/view/lib/Data/Munge.pm" >0.111 (MAUKE on 2024-07-08)</option> <option value="/release/MAUKE/Data-Munge-0.101/view/lib/Data/Munge.pm" >0.101 (MAUKE on 2024-04-04)</option> <option value="/release/MAUKE/Data-Munge-0.10/view/lib/Data/Munge.pm" >0.10 (MAUKE on 2023-03-21)</option> <option value="/release/MAUKE/Data-Munge-0.097/view/lib/Data/Munge.pm" >0.097 (MAUKE on 2017-03-17)</option> <optgroup label="BackPAN">' <option value="/release/MAUKE/Data-Munge-0.096/view/lib/Data/Munge.pm" >0.096 (MAUKE on 2016-03-02)</option> <option value="/release/MAUKE/Data-Munge-0.095/view/lib/Data/Munge.pm" >0.095 (MAUKE on 2015-05-08)</option> <option value="/release/MAUKE/Data-Munge-0.094/view/lib/Data/Munge.pm" >0.094 (MAUKE on 2015-03-08)</option> <option value="/release/MAUKE/Data-Munge-0.093/view/lib/Data/Munge.pm" >0.093 (MAUKE on 2014-12-23)</option> <option value="/release/MAUKE/Data-Munge-0.092/view/lib/Data/Munge.pm" >0.092 (MAUKE on 2014-11-25)</option> <option value="/release/MAUKE/Data-Munge-0.091/view/lib/Data/Munge.pm" >0.091 (MAUKE on 2014-11-19)</option> <option value="/release/MAUKE/Data-Munge-0.09/view/lib/Data/Munge.pm" >0.09 (MAUKE on 2014-11-18)</option> <option value="/release/MAUKE/Data-Munge-0.08/view/lib/Data/Munge.pm" >0.08 (MAUKE on 2014-09-15)</option> <option value="/release/MAUKE/Data-Munge-0.07/view/lib/Data/Munge.pm" >0.07 (MAUKE on 2013-10-22)</option> <option value="/release/MAUKE/Data-Munge-0.06/view/lib/Data/Munge.pm" >0.06 (MAUKE on 2013-03-07)</option> <option value="/release/MAUKE/Data-Munge-0.05/view/lib/Data/Munge.pm" >0.05 (MAUKE on 2013-03-05)</option> <option value="/release/MAUKE/Data-Munge-0.04/view/lib/Data/Munge.pm" >0.04 (MAUKE on 2011-08-03)</option> <option value="/release/MAUKE/Data-Munge-0.032/view/lib/Data/Munge.pm" >0.032 (MAUKE on 2010-01-23)</option> <option value="/release/MAUKE/Data-Munge-0.031/view/lib/Data/Munge.pm" >0.031 (MAUKE on 2010-01-20)</option> <option value="/release/MAUKE/Data-Munge-0.03/view/lib/Data/Munge.pm" >0.03 (MAUKE on 2010-01-19)</option> <option value="/release/MAUKE/Data-Munge-0.02/view/lib/Data/Munge.pm" >0.02 (MAUKE on 2009-12-14)</option> </optgroup> </select> <a data-keyboard-shortcut="g d" class="release-name" href="/dist/Data-Munge">Data-Munge-0.111</a> </div> <span class="river-gauge-gauge"> <svg width="24px" height="15px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <title> River stage two • 12 direct dependents • 13 total dependents </title> <rect x="0" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="5" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="10" y="0" width="4" height="15" fill="#e4e2e2" /> <rect x="15" y="0" width="4" height="15" fill="#e4e2e2" /> <rect x="20" y="0" width="4" height="15" fill="#e4e2e2" /> </g> </svg> </span> <div id="Data-Munge-fav" class="show-logged-in"> <form action="/account/favorite/add" style="display: inline" method="POST"> <input type="hidden" name="remove" value="0"> <input type="hidden" name="release" value="Data-Munge-0.111"> <input type="hidden" name="author" value="MAUKE"> <input type="hidden" name="distribution" value="Data-Munge"> <button type="submit" class="favorite highlight"><span>7</span> ++</button> </form> </div> <div class="show-logged-out"> <button class="fav-not-logged-in favorite highlight"><span>7</span> ++</button> </div> / <span>Data::Munge</span> </div> </div> <main class="content"> <nav class="toc"> <div class="toc-header"><strong>Contents</strong></div> <ul> <li><a href="#NAME">NAME</a></li> <li><a href="#SYNOPSIS">SYNOPSIS</a></li> <li><a href="#DESCRIPTION">DESCRIPTION</a> <ul> <li><a href="#Functions">Functions</a></li> </ul> </li> <li><a href="#AUTHOR">AUTHOR</a></li> <li><a href="#COPYRIGHT-%26-LICENSE">COPYRIGHT & LICENSE</a></li> </ul></nav> <div class="pod anchors"> <h1 id="NAME">NAME</h1> <p>Data::Munge - various utility functions</p> <h1 id="SYNOPSIS">SYNOPSIS</h1> <pre><code class="language-perl">use Data::Munge; my $re = list2re qw/f ba foo bar baz/; # $re = qr/bar|baz|foo|ba|f/; print byval { s/foo/bar/ } $text; # print do { my $tmp = $text; $tmp =~ s/foo/bar/; $tmp }; foo(mapval { chomp } @lines); # foo(map { my $tmp = $_; chomp $tmp; $tmp } @lines); print replace('Apples are round, and apples are juicy.', qr/apples/i, 'oranges', 'g'); # "oranges are round, and oranges are juicy." print replace('John Smith', qr/(\w+)\s+(\w+)/, '$2, $1'); # "Smith, John" my $trimmed = trim " a b c "; # "a b c" my $x = 'bar'; if (elem $x, [qw(foo bar baz)]) { ... } # executes: $x is an element of the arrayref my $contents = slurp $fh; # or: slurp *STDIN # reads all data from a filehandle into a scalar my $t = is_callable(sub {}); # true my $f = is_callable("hello"); # false eval_string('print "hello world\\n"'); # says hello eval_string('die'); # dies eval_string('{'); # throws a syntax error my $fac = rec { my ($rec, $n) = @_; $n < 2 ? 1 : $n * $rec->($n - 1) }; print $fac->(5); # 120 if ("hello, world!" =~ /(\w+), (\w+)/) { my @captured = submatches; # @captured = ("hello", "world") }</code></pre> <h1 id="DESCRIPTION">DESCRIPTION</h1> <p>This module defines a few generally useful utility functions. I got tired of redefining or working around them, so I wrote this module.</p> <h2 id="Functions">Functions</h2> <dl> <dt id="list2re-LIST"><a id="list2re"></a>list2re LIST</dt> <dd> <p>Converts a list of strings to a regex that matches any of the strings. Especially useful in combination with <code>keys</code>. Example:</p> <pre><code class="language-perl">my $re = list2re keys %hash; $str =~ s/($re)/$hash{$1}/g;</code></pre> <p>This function takes special care to get several edge cases right:</p> <ul> <li><p>Empty list: An empty argument list results in a regex that doesn't match anything.</p> </li> <li><p>Empty string: An argument list consisting of a single empty string results in a regex that matches the empty string (and nothing else).</p> </li> <li><p>Prefixes: The input strings are sorted by descending length to ensure longer matches are tried before shorter matches. Otherwise <code>list2re('ab', 'abcd')</code> would generate <code>qr/ab|abcd/</code>, which (on its own) can never match <code>abcd</code> (because <code>ab</code> is tried first, and it always succeeds where <code>abcd</code> could).</p> </li> </ul> </dd> <dt id="byval-BLOCK-SCALAR"><a id="byval"></a>byval BLOCK SCALAR</dt> <dd> <p>Takes a code block and a value, runs the block with <code>$_</code> set to that value, and returns the final value of <code>$_</code>. The global value of <code>$_</code> is not affected. <code>$_</code> isn't aliased to the input value either, so modifying <code>$_</code> in the block will not affect the passed in value. Example:</p> <pre><code class="language-perl">foo(byval { s/!/?/g } $str); # Calls foo() with the value of $str, but all '!' have been replaced by '?'. # $str itself is not modified.</code></pre> <p>Since perl 5.14 you can also use the <code>/r</code> flag:</p> <pre><code class="language-perl">foo($str =~ s/!/?/gr);</code></pre> <p>But <code>byval</code> works on all versions of perl and is not limited to <code>s///</code>.</p> </dd> <dt id="mapval-BLOCK-LIST"><a id="mapval"></a>mapval BLOCK LIST</dt> <dd> <p>Works like a combination of <code>map</code> and <code>byval</code>; i.e. it behaves like <code>map</code>, but <code>$_</code> is a copy, not aliased to the current element, and the return value is taken from <code>$_</code> again (it ignores the value returned by the block). Example:</p> <pre><code class="language-perl">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.</code></pre> </dd> <dt id="submatches">submatches</dt> <dd> <p>Returns a list of the strings captured by the last successful pattern match. Normally you don't need this function because this is exactly what <code>m//</code> returns in list context. However, <code>submatches</code> also works in other contexts such as the RHS of <code>s//.../e</code>.</p> </dd> <dt id="replace-STRING,-REGEX,-REPLACEMENT,-FLAG"><a id="replace"></a><a id="replace-STRING-REGEX-REPLACEMENT-FLAG"></a>replace STRING, REGEX, REPLACEMENT, FLAG</dt> <dd> </dd> <dt id="replace-STRING,-REGEX,-REPLACEMENT"><a id="replace1"></a><a id="replace-STRING-REGEX-REPLACEMENT"></a>replace STRING, REGEX, REPLACEMENT</dt> <dd> <p>A clone of javascript's <code>String.prototype.replace</code>. It works almost the same as <code>byval { s/REGEX/REPLACEMENT/FLAG } STRING</code>, but with a few important differences. REGEX can be a string or a compiled <code>qr//</code> object. REPLACEMENT can be a string or a subroutine reference. If it's a string, it can contain the following replacement patterns:</p> <dl> <dt id="$$"><a id="pod"></a><code>$$</code></dt> <dd> <p>Inserts a '$'.</p> </dd> <dt id="$&"><a id="pod1"></a><code>$&</code></dt> <dd> <p>Inserts the matched substring.</p> </dd> <dt id="$`"><a id="pod2"></a><code>$`</code></dt> <dd> <p>Inserts the substring preceding the match.</p> </dd> <dt id="$'"><a id="pod3"></a><code>$'</code></dt> <dd> <p>Inserts the substring following the match.</p> </dd> <dt id="$N-(where-N-is-a-digit)"><a id="N-where-N-is-a-digit"></a><code>$N</code> (where <i>N</i> is a digit)</dt> <dd> <p>Inserts the substring matched by the <i>N</i>th capturing group.</p> </dd> <dt id="${N}-(where-N-is-one-or-more-digits)"><a id="N-where-N-is-one-or-more-digits"></a><code>${N}</code> (where <i>N</i> is one or more digits)</dt> <dd> <p>Inserts the substring matched by the <i>N</i>th capturing group.</p> </dd> </dl> <p>Note that these aren't variables; they're character sequences interpreted by <code>replace</code>.</p> <p>If REPLACEMENT is a subroutine reference, it's called with the following arguments: First the matched substring (like <code>$&</code> above), then the contents of the capture buffers (as returned by <code>submatches</code>), then the offset where the pattern matched (like <code>$-[0]</code>, see <a href="/pod/perlvar#%40-">"@-" in perlvar</a>), then the STRING. The return value will be inserted in place of the matched substring.</p> <p>Normally only the first occurrence of REGEX is replaced. If FLAG is present, it must be <code>'g'</code> and causes all occurrences to be replaced.</p> </dd> <dt id="trim-STRING"><a id="trim"></a>trim STRING</dt> <dd> <p>Returns <i>STRING</i> with all leading and trailing whitespace removed. Like <a href="/pod/perlfunc#length-EXPR"><code>length</code></a> it returns <code>undef</code> if the input is <code>undef</code>.</p> </dd> <dt id="elem-SCALAR,-ARRAYREF"><a id="elem"></a><a id="elem-SCALAR-ARRAYREF"></a>elem SCALAR, ARRAYREF</dt> <dd> <p>Returns a boolean value telling you whether <i>SCALAR</i> is an element of <i>ARRAYREF</i> or not. Two scalars are considered equal if they're both <code>undef</code>, if they're both references to the same thing, or if they're both not references and <code>eq</code> to each other.</p> <p>This is implemented as a linear search through <i>ARRAYREF</i> that terminates early if a match is found (i.e. <code>elem 'A', ['A', 1 .. 9999]</code> won't even look at elements <code>1 .. 9999</code>).</p> </dd> <dt id="is_callable-SCALAR"><a id="is_callable"></a>is_callable SCALAR</dt> <dd> <p>Returns a boolean value telling you whether <i>SCALAR</i> can be called as a function. This is the case if <i>SCALAR</i> is either a reference to a subroutine (such as <code>\&foo</code> or <code>sub { ... }</code>) or an object with an overloaded <code>'&{}'</code> operator (see <a href="/pod/overload#*-Dereferencing">"* <i>Dereferencing</i>" in overload</a>).</p> </dd> <dt id="eval_string-STRING"><a id="eval_string"></a>eval_string STRING</dt> <dd> <p>Evals <i>STRING</i> just like <code>eval</code> but doesn't catch exceptions. Caveat: Unlike with <code>eval</code> the code runs in an empty lexical scope:</p> <pre><code class="language-perl">my $foo = "Hello, world!\n"; eval_string 'print $foo'; # Dies: Global symbol "$foo" requires explicit package name</code></pre> <p>That is, the eval'd code can't see variables from the scope of the <code>eval_string</code> call.</p> </dd> <dt id="slurp-FILEHANDLE"><a id="slurp"></a>slurp FILEHANDLE</dt> <dd> <p>Reads and returns all remaining data from <i>FILEHANDLE</i> as a string, or <code>undef</code> if it hits end-of-file. (Interaction with non-blocking filehandles is currently not well defined.)</p> <p><code>slurp $handle</code> is equivalent to <code>do { local $/; scalar readline $handle }</code>.</p> </dd> <dt id="rec-BLOCK"><a id="rec"></a>rec BLOCK</dt> <dd> <p>Creates an anonymous sub as <code>sub BLOCK</code> would, but supplies the called sub with an extra argument that can be used to recurse:</p> <pre><code class="language-perl">my $code = rec { my ($rec, $n) = @_; $rec->($n - 1) if $n > 0; print $n, "\n"; }; $code->(4);</code></pre> <p>That is, when the sub is called, an implicit first argument is passed in <code>$_[0]</code> (all normal arguments are moved one up). This first argument is a reference to the sub itself. This reference could be used to recurse directly or to register the sub as a handler in an event system, for example.</p> <p>A note on defining recursive anonymous functions: Doing this right is more complicated than it may at first appear. The most straightforward solution using a lexical variable and a closure leaks memory because it creates a reference cycle. Starting with perl 5.16 there is a <code>__SUB__</code> constant that is equivalent to <code>$rec</code> above, and this is indeed what this module uses (if available).</p> <p>However, this module works even on older perls by falling back to either weak references (if available) or a "fake recursion" scheme that dynamically instantiates a new sub for each call instead of creating a cycle. This last resort is slower than weak references but works everywhere.</p> </dd> </dl> <h1 id="AUTHOR">AUTHOR</h1> <p>Lukas Mai, <code><l.mai at web.de></code></p> <h1 id="COPYRIGHT-&-LICENSE"><a id="COPYRIGHT"></a><a id="COPYRIGHT-LICENSE"></a>COPYRIGHT & LICENSE</h1> <p>Copyright 2009-2011, 2013-2015, 2023-2024 Lukas Mai.</p> <p>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.</p> <p>See <a href="https://dev.perl.org/licenses/">https://dev.perl.org/licenses/</a> for more information.</p></div> <div id="metacpan_install-instructions-dialog" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Module Install Instructions</h4> </div> <div class="modal-body"> <p>To install Data::Munge, copy and paste the appropriate command in to your terminal.</p> <p><a href="/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p> <pre><code>cpanm Data::Munge</code></pre> <p><a href="/pod/CPAN">CPAN shell</a></p> <pre><code>perl -MCPAN -e shell install Data::Munge</code></pre> <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> </div> </div> </div> </div> </main> <div class="content-pagination"> </div> </div> <footer class="footer"> <div class="footer-container"> <div class="footer-social"> <div class="footer-link footer-logo"> <a href="/"> <img src="/static/images/metacpan-logo.svg" alt="MetaCPAN" /> </a> </div> <a class="footer-social-link" href="https://github.com/metacpan"> <i class="fab fa-github-square"></i> </a> <a class="footer-social-link" href="https://fosstodon.org/@metacpan"> <i class="fab fa-mastodon"></i> </a> </div> <div class="footer-links"> <div class="footer-link"> <a href="/about">About</a> </div> <div class="footer-link"> <a href="/about/sponsors">Sponsor</a> </div> <div class="footer-link"> <a href="https://grep.metacpan.org">grep::cpan</a> </div> <div class="footer-link"> <a href="/recent">Recent</a> </div> <div class="footer-link"> <a href="/about/faq">FAQ</a> </div> <div class="footer-link"> <a href="/tools">Tools</a> </div> <div class="footer-link"> <a href="https://fastapi.metacpan.org/">API</a> </div> <div class="footer-link"> <a href="https://www.perl.org/">Perl.org</a> </div> </div> <div class="footer-sponsors"> <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener"> <img class="footer-sponsor-bytemark" src="/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener"> <img class="footer-sponsor-liquidweb" src="/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener"> <img class="footer-sponsor-deriv" src="/static/images/sponsors/deriv.svg" alt="Deriv logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener"> <img class="footer-sponsor-geocode" src="/static/images/sponsors/geocodelogo.svg" alt="Geocode logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener"> <img class="footer-sponsor-fastly" src="/static/images/sponsors/fastly_logo.svg" alt="Fastly logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener"> <img class="footer-sponsor-opencage" src="/static/images/sponsors/open-cage.svg" alt="OpenCage logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://www.elastic.co/" rel="noopener"> <img class="footer-sponsor-elastic" src="/static/images/sponsors/elastic.svg" alt="Elastic logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://route4me.com/" rel="noopener"> <img class="footer-sponsor-route4me" src="/static/images/sponsors/route4me.png" alt="Route4Me logo"> </a> </div> </div> </footer> <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Keyboard Shortcuts</h4> </div> <div class="modal-body row"> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Global</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>s</kbd> </td> <td>Focus search bar</td> </tr> <tr> <td class="keys"> <kbd>?</kbd> </td> <td>Bring up this help dialog</td> </tr> </tbody> </table> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>GitHub</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>p</kbd> </td> <td>Go to pull requests</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>go to github issues (only if github is preferred repository)</td> </tr> </tbody> </table> </div> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>POD</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>a</kbd> </td> <td>Go to author</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>c</kbd> </td> <td>Go to changes</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>Go to issues</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>d</kbd> </td> <td>Go to dist</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>r</kbd> </td> <td>Go to repository/SCM</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>s</kbd> </td> <td>Go to source</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>b</kbd> </td> <td>Go to file browse</td> </tr> </tbody> </table> </div> <div class="col-md-12"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Search terms</th> </tr> </thead> <tbody> <tr> <td><em>module:</em> (e.g. <a href="/search?q=module%3APlugin">module:Plugin</a>)</td> </tr> <tr> <td><em>distribution:</em> (e.g. <a href="/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td> </tr> <tr> <td><em>author:</em> (e.g. <a href="/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td> </tr> <tr> <td><em>version:</em> (e.g. <a href="/search?q=version%3A1.00">version:1.00</a>)</td> </tr> </tbody> </table> </div> </div> <div class="modal-footer"></div> </div> </div> </div> </body> </html>