CINXE.COM

Data::Types - Validate and convert data types. - metacpan.org

<!DOCTYPE html> <html lang="en-US"> <head> <title>Data::Types - Validate and convert data types. - 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-Types - MetaCPAN" href="/dist/Data-Types/releases.rss" /> <link rel="canonical" href="https://metacpan.org/pod/Data::Types" /> <meta name="description" content="Validate and convert data types." /> <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::Types" /> <meta property="og:title" content="Data::Types" /> <meta property="og:description" content="Validate and convert data types." /> <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-Types was released."> <span class="relatize">23 Feb 2019 12:35:02 UTC</span> </div> </li> <li> Distribution: <a href="/dist/Data-Types">Data-Types</a> </li> <li> Module version: 0.17 </li> <li> <a data-keyboard-shortcut="g s" href="/dist/Data-Types/source/lib/Data/Types.pm">Source</a> (<a href="/dist/Data-Types/source/lib/Data/Types.pm?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g b" href="/dist/Data-Types/source/lib/Data">Browse</a> (<a href="/dist/Data-Types/source/lib/Data?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g c" href="/dist/Data-Types/changes">Changes</a> </li> <li> <a class="nopopup" href="/dist/Data-Types/contribute">How to Contribute</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="https://github.com/manwar/Data-Types">Repository</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Types">Issues</a> (0) </li> <li> <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Data-Types+0.17" title="Matrix">Testers</a> <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Types.html?oncpan=1&amp;distmat=1&amp;version=0.17&amp;grade=2" style="color: #090">3045</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Types.html?oncpan=1&amp;distmat=1&amp;version=0.17&amp;grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/D/Data-Types.html?oncpan=1&amp;distmat=1&amp;version=0.17&amp;grade=4">0</a>)</span> </li> <li> <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/MANWAR/Data-Types-0.17">Kwalitee</a> </li> <li> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Data-Types 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: 2 </div> </li> <li> <a rel="noopener nofollow" href="http://cpancover.com/latest/Data-Types-0.17/index.html">98.69% Coverage </a> </li> <li> License: perl_5 </li> <li> Perl: v5.6.0 </li> <li class="nav-header">Activity</li> <li> <div class="activity-graph"> <img src="/dist/Data-Types/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/MANWAR/Data-Types-0.17.tar.gz"> Download (<span itemprop="fileSize">7.76KB</span>)</a> </li> <li> <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FMANWAR%2FData-Types-0.17%2Flib%2FData%2FTypes.pm"> MetaCPAN Explorer </a> </li> <li> <a href="/dist/Data-Types/permissions"> Permissions </a> </li> <li> <a href="/dist/Data-Types/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-Types"> <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-Types"> <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/MANWAR/Data-Types-0.17/view/lib/Data/Types.pm" >0.17 (MANWAR on 2019-02-23)</option> <option value="/release/MANWAR/Data-Types-0.16/view/lib/Data/Types.pm" >0.16 (MANWAR on 2018-12-03)</option> <option value="/release/MANWAR/Data-Types-0.15/view/lib/Data/Types.pm" >0.15 (MANWAR on 2018-11-19)</option> <option value="/release/DWHEELER/Data-Types-0.09/view/lib/Data/Types.pm" >0.09 (DWHEELER on 2011-06-20)</option> <option value="/release/DWHEELER/Data-Types-0.08/view/lib/Data/Types.pm" >0.08 (DWHEELER on 2008-05-05)</option> <option value="/release/DWHEELER/Data-Types-0.07/view/lib/Data/Types.pm" >0.07 (DWHEELER on 2008-01-22)</option> <optgroup label="BackPAN">' <option value="/release/MANWAR/Data-Types-0.14/view/lib/Data/Types.pm" >0.14 (MANWAR on 2018-10-15)</option> <option value="/release/MANWAR/Data-Types-0.13/view/lib/Data/Types.pm" >0.13 (MANWAR on 2018-05-26)</option> <option value="/release/MANWAR/Data-Types-0.12/view/lib/Data/Types.pm" >0.12 (MANWAR on 2018-05-25)</option> <option value="/release/MANWAR/Data-Types-0.11/view/lib/Data/Types.pm" >0.11 (MANWAR on 2018-05-24)</option> <option value="/release/DWHEELER/Data-Types-0.06/view/lib/Data/Types.pm" >0.06 (DWHEELER on 2006-05-23)</option> <option value="/release/DWHEELER/Data-Types-0.05/view/lib/Data/Types.pm" >0.05 (DWHEELER on 2003-08-26)</option> <option value="/release/DWHEELER/Data-Types-0.04/view/lib/Data/Types.pm" >0.04 (DWHEELER on 2002-07-25)</option> <option value="/release/DWHEELER/Data-Types-0.03/view/lib/Data/Types.pm" >0.03 (DWHEELER on 2002-07-22)</option> <option value="/release/DWHEELER/Data-Types-0.02/view/lib/Data/Types.pm" >0.02 (DWHEELER on 2002-01-04)</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/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.17/lib/Data/Types.pm" >0.17 (MANWAR on 2019-02-23)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.16/lib/Data/Types.pm" >0.16 (MANWAR on 2018-12-03)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.15/lib/Data/Types.pm" >0.15 (MANWAR on 2018-11-19)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.09/lib/Data/Types.pm" >0.09 (DWHEELER on 2011-06-20)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.08/lib/Data/Types.pm" >0.08 (DWHEELER on 2008-05-05)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.07/lib/Data/Types.pm" >0.07 (DWHEELER on 2008-01-22)</option> <optgroup label="BackPAN">' <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.14/lib/Data/Types.pm" >0.14 (MANWAR on 2018-10-15)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.13/lib/Data/Types.pm" >0.13 (MANWAR on 2018-05-26)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.12/lib/Data/Types.pm" >0.12 (MANWAR on 2018-05-25)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/MANWAR/Data-Types-0.11/lib/Data/Types.pm" >0.11 (MANWAR on 2018-05-24)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.06/lib/Data/Types.pm" >0.06 (DWHEELER on 2006-05-23)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.05/lib/Data/Types.pm" >0.05 (DWHEELER on 2003-08-26)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.04/lib/Data/Types.pm" >0.04 (DWHEELER on 2002-07-25)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.03/lib/Data/Types.pm" >0.03 (DWHEELER on 2002-07-22)</option> <option value="/release/MANWAR/Data-Types-0.17/diff/DWHEELER/Data-Types-0.02/lib/Data/Types.pm" >0.02 (DWHEELER on 2002-01-04)</option> </optgroup> </select> </li> <li> <ul class="dependencies"> <li class="nav-header">Dependencies</li> <li><i class="ttip" title="dynamic_config enabled">unknown</i></li> <li> <hr> </li> <li> <a href="/module/Data::Types/requires">Reverse dependencies</a> </li> <li> <a href="http://deps.cpantesters.org/?module=Data%3A%3ATypes">CPAN Testers List</a> </li> <li> <a href="https://cpandeps.grinnz.com/?dist=Data-Types">Dependency graph</a> </li> </ul> </li> <li class="nav-header">Permalinks</li> <li> <a href="/release/MANWAR/Data-Types-0.17/view/lib/Data/Types.pm">This version</a> </li> <li> <a href="/pod/Data::Types">Latest version</a> </li> <li> <div class="plussers"> <div class="nav-header">++ed by:</div> <div> 2 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-cpan-author="DWHEELER" data-contrib-email="dwheeler@cpan.org" > <a href="/author/DWHEELER" class="cpan-author"> <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/de2fe609ec8ae522883ea1235f1e8792?d=identicon&amp;s=20" /> David E. Wheeler </a> </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/MANWAR" class="author-name">Mohammad Sajid Anwar</a> </span> <span>&nbsp;/&nbsp;</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/MANWAR/Data-Types-0.17/view/lib/Data/Types.pm" >0.17 (MANWAR on 2019-02-23)</option> <option value="/release/MANWAR/Data-Types-0.16/view/lib/Data/Types.pm" >0.16 (MANWAR on 2018-12-03)</option> <option value="/release/MANWAR/Data-Types-0.15/view/lib/Data/Types.pm" >0.15 (MANWAR on 2018-11-19)</option> <option value="/release/DWHEELER/Data-Types-0.09/view/lib/Data/Types.pm" >0.09 (DWHEELER on 2011-06-20)</option> <option value="/release/DWHEELER/Data-Types-0.08/view/lib/Data/Types.pm" >0.08 (DWHEELER on 2008-05-05)</option> <option value="/release/DWHEELER/Data-Types-0.07/view/lib/Data/Types.pm" >0.07 (DWHEELER on 2008-01-22)</option> <optgroup label="BackPAN">' <option value="/release/MANWAR/Data-Types-0.14/view/lib/Data/Types.pm" >0.14 (MANWAR on 2018-10-15)</option> <option value="/release/MANWAR/Data-Types-0.13/view/lib/Data/Types.pm" >0.13 (MANWAR on 2018-05-26)</option> <option value="/release/MANWAR/Data-Types-0.12/view/lib/Data/Types.pm" >0.12 (MANWAR on 2018-05-25)</option> <option value="/release/MANWAR/Data-Types-0.11/view/lib/Data/Types.pm" >0.11 (MANWAR on 2018-05-24)</option> <option value="/release/DWHEELER/Data-Types-0.06/view/lib/Data/Types.pm" >0.06 (DWHEELER on 2006-05-23)</option> <option value="/release/DWHEELER/Data-Types-0.05/view/lib/Data/Types.pm" >0.05 (DWHEELER on 2003-08-26)</option> <option value="/release/DWHEELER/Data-Types-0.04/view/lib/Data/Types.pm" >0.04 (DWHEELER on 2002-07-25)</option> <option value="/release/DWHEELER/Data-Types-0.03/view/lib/Data/Types.pm" >0.03 (DWHEELER on 2002-07-22)</option> <option value="/release/DWHEELER/Data-Types-0.02/view/lib/Data/Types.pm" >0.02 (DWHEELER on 2002-01-04)</option> </optgroup> </select> <a data-keyboard-shortcut="g d" class="release-name" href="/dist/Data-Types">Data-Types-0.17</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 &#10; • 5 direct dependents &#10; • 10 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-Types-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-Types-0.17"> <input type="hidden" name="author" value="MANWAR"> <input type="hidden" name="distribution" value="Data-Types"> <button type="submit" class="favorite highlight"><span>2</span> ++</button> </form> </div> <div class="show-logged-out"> <button class="fav-not-logged-in favorite highlight"><span>2</span> ++</button> </div> / <span>Data::Types</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></li> <li><a href="#EXPORT">EXPORT</a></li> <li><a href="#FUNCTIONS">FUNCTIONS</a> <ul> <li><a href="#is_whole">is_whole</a></li> <li><a href="#to_whole">to_whole</a></li> <li><a href="#is_count">is_count</a></li> <li><a href="#to_count">to_count</a></li> <li><a href="#is_int">is_int</a></li> <li><a href="#to_int">to_int</a></li> <li><a href="#is_decimal">is_decimal</a></li> <li><a href="#to_decimal">to_decimal</a></li> <li><a href="#is_real">is_real</a></li> <li><a href="#to_real">to_real</a></li> <li><a href="#is_float">is_float</a></li> <li><a href="#to_float">to_float</a></li> <li><a href="#is_string">is_string</a></li> <li><a href="#to_string">to_string</a></li> </ul> </li> <li><a href="#SUPPORT">SUPPORT</a></li> <li><a href="#AUTHOR">AUTHOR</a></li> <li><a href="#SEE-ALSO">SEE ALSO</a></li> <li><a href="#COPYRIGHT-AND-LICENSE">COPYRIGHT AND LICENSE</a></li> </ul></nav> <div class="pod anchors"> <h1 id="NAME">NAME</h1> <p>Data::Types - Validate and convert data types.</p> <h1 id="SYNOPSIS">SYNOPSIS</h1> <pre><code>use Data::Types qw(:all); my $whole = 4.5; $whole = to_whole($whole) unless is_whole($whole); my $int = 1.2; $int = to_int($int) unless is_int($int); my $decimal = &#39;1.2foo&#39;; $decimal = to_decimal($decimal) unless is_decimal($decimal); my $real = &#39;1.2foo&#39;; $real = to_real($real) unless is_real($real); my $float = &#39;1.2foo&#39;; $float = to_float($float) unless is_float($float); my $string = []; $string = to_string($string) unless is_string($string);</code></pre> <h1 id="DESCRIPTION">DESCRIPTION</h1> <p>This module exports a number of functions that are useful for validating and converting data types. It is intended for use in applications where data types are more important than they typically are in Perl -- e.g., database applications.</p> <h1 id="EXPORT">EXPORT</h1> <p>No functions are exported by default, though each function may be exported explicitly (see <a href="#FUNCTIONS">&quot;FUNCTIONS&quot;</a> below, for a list of functions available for export). The following export tags are supported:</p> <dl> <dt id=":whole"><a id="whole"></a>:whole</dt> <dd> <p>Exports is_whole() and to_whole().</p> </dd> <dt id=":count"><a id="count"></a>:count</dt> <dd> <p>Exports is_count() and to_count().</p> </dd> <dt id=":int"><a id="int"></a>:int</dt> <dd> <p>Exports is_int() and to_int().</p> </dd> <dt id=":decimal"><a id="decimal"></a>:decimal</dt> <dd> <p>Exports is_decimal() and to_decimal().</p> </dd> <dt id=":real"><a id="real"></a>:real</dt> <dd> <p>Exports is_real() and to_real().</p> </dd> <dt id=":float"><a id="float"></a>:float</dt> <dd> <p>Exports is_float() and to_float().</p> </dd> <dt id=":string"><a id="string"></a>:string</dt> <dd> <p>Exports is_string() and to_string().</p> </dd> <dt id=":is"><a id="is"></a>:is</dt> <dd> <p>Exports all validation functions: is_whole(), is_int(), is_real(), is_decimal(), is_float(), and is_string().</p> </dd> <dt id=":to"><a id="to"></a>:to</dt> <dd> <p>Exports all conversion functions: to_whole(), to_int(), to_real(), to_decimal(), to_float(), and to_string().</p> </dd> <dt id=":all"><a id="all"></a>:all</dt> <dd> <p>Exports all functions.</p> </dd> </dl> <h1 id="FUNCTIONS">FUNCTIONS</h1> <h2 id="is_whole">is_whole</h2> <pre><code>my $bool = is_whole($val);</code></pre> <p>Returns true if $val is a whole number (including 0), and false if it is not. The regular expression used to test the wholeness of $val is <code>/^\d+$/</code>.</p> <pre><code>my $bool = is_whole(1); # Returns true. $bool = is_whole(-1); # Returns false. $bool = is_whole(0); # Returns true.</code></pre> <h2 id="to_whole">to_whole</h2> <pre><code>my $whole = to_whole($val);</code></pre> <p>Converts $val to a whole number and returns it. Numbers will be rounded to the nearest whole. If $val is a mixture of numbers and letters, to_whole() will extract the first decimal number it finds and convert that number to a whole number.</p> <pre><code>my $whole = to_whole(10); # Returns 10. $whole = to_whole(0); # Returns 0. $whole = to_whole(.22); # Returns 0. $whole = to_whole(-2); # Returns undef. $whole = to_whole(&#39;foo3.56&#39;); # Returns 4. $whole = to_whole(&#39;foo&#39;); # Returns undef.</code></pre> <h2 id="is_count">is_count</h2> <pre><code>my $bool = is_count($val);</code></pre> <p>Returns true if $val is a counting number (1, 2, 3, ...), and false if it is not. The regular expression used to test whether $val is a counting number is <code>/^\d+$/</code>.</p> <pre><code>my $bool = is_count(1); # Returns true. $bool = is_count(-1); # Returns false. $bool = is_count(0); # Returns false.</code></pre> <h2 id="to_count">to_count</h2> <pre><code>my $count = to_count($val);</code></pre> <p>Converts $val to a counting number and returns it. Numbers will be rounded to the nearest counting number. Note that since 0 (zero) is not considered a counting number by this module, it will not be returned. If $val is a mixture of numbers and letters, to_count() will extract the first decimal number it finds and convert that number to a counting number.</p> <pre><code>my $count = to_count(10); # Returns 10. $count = to_count(0); # Returns undef. $count = to_count(.22); # Returns undef (rounded down to 0). $count = to_count(-2); # Returns undef. $count = to_count(&#39;foo3.56&#39;); # Returns 4. $count = to_count(&#39;foo&#39;); # Returns undef.</code></pre> <h2 id="is_int">is_int</h2> <pre><code>my $bool = is_int($val);</code></pre> <p>Returns true if $val is an integer, and false if it is not. Numbers may be preceded by a plus or minus sign. The regular expression used to test for an integer in $val is <code>/^[+-]?\d+$/</code>.</p> <pre><code>my $bool = is_int(0); # Returns true. $bool = is_int(22); # Returns true. $bool = is_int(-22); # Returns true. $bool = is_int(3.2); # Returns false.</code></pre> <h2 id="to_int">to_int</h2> <pre><code>my $int = to_int($val);</code></pre> <p>Converts $val to an integer. If $val is a decimal number, it will be rounded to the nearest integer. If $val is a mixture of numbers and letters, to_int() will extract the first decimal number it finds and convert that number to an integer.</p> <pre><code>my $int = to_int(10.5); # Returns 10. $int = to_int(10.51); # Returns 11. $int = to_int(-0.22); # Returns 0. $int = to_int(-6.51); # Returns 7. $int = to_int(&#39;foo&#39;); # Returns undef.</code></pre> <h2 id="is_decimal">is_decimal</h2> <pre><code>my $bool = is_decimal($val);</code></pre> <p>Returns true if $val is a decimal number, and false if it is not. Numbers may be preceded by a plus or minus sign. The regular expression used to test $val is <code>/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/</code>.</p> <pre><code>my $bool = is_decimal(10) # Returns true. $bool = is_decimal(10.8) # Returns true. $bool = is_decimal(-33.48) # Returns true. $bool = is_decimal(1.23e99) # Returns false.</code></pre> <h2 id="to_decimal">to_decimal</h2> <pre><code>my $dec = to_decimal($val); $dec = to_decimal($val, $precision);</code></pre> <p>Converts $val to a decimal number. The optional second argument sets the precision of the number. The default precision is 5. If $val is a mixture of numbers and letters, to_decimal() will extract the first decimal number it finds.</p> <pre><code>my $dec = to_decimal(0); # Returns 0.00000. $dec = to_decimal(10.5); # Returns 10.5. $dec = to_decimal(10.500009); # Returns 10.50001. $dec = to_decimal(10.500009, 7); # Returns 10.5000090. $dec = to_decimal(&#39;foo10.3&#39;) # Returns 10.30000. $dec = to_decimal(&#39;foo-4.9&#39;) # Returns -4.90000. $dec = to_decimal(&#39;foo&#39;) # Returns undef.</code></pre> <h2 id="is_real">is_real</h2> <pre><code>my $bool = is_real($val);</code></pre> <p>Returns true if $val is a real number, and false if it is not.</p> <p><b>Note:</b> This function is currently equivalent to is_decimal(), since this module cannot identify non-decimal real numbers (e.g., irrational numbers). This implementation may change in the future.</p> <h2 id="to_real">to_real</h2> <pre><code>my $real = to_real($val); $real = to_real($val, $precision);</code></pre> <p>Converts $val to a real number.</p> <p><b>Note:</b> Currently, this function is the equivalent of to_decimal(), since this module cannot identify non-decimal real numbers (e.g., irrational numbers). This implementation may change in the future.</p> <h2 id="is_float">is_float</h2> <pre><code>my $bool = is_float($val);</code></pre> <p>Returns true if $val is a float, and false if it is not. The regular expression used to test $val is <code>/^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/</code>.</p> <pre><code>my $bool = is_float(30); # Returns true. $bool = is_float(1.23e99); # Returns true. $bool = is_float(&#39;foo&#39;); # Returns false.</code></pre> <h2 id="to_float">to_float</h2> <pre><code>my $dec = to_float($val); $dec = to_float($val, $precision);</code></pre> <p>Converts $val to a float. The optional second argument sets the precision of the number. The default precision is 5. If $val is a mixture of numbers and letters, to_float() will extract the first float it finds.</p> <pre><code>my $float = to_float(1.23); # Returns 1.23000. $float = to_float(1.23e99); # Returns 1.23000e+99. $float = to_float(1.23e99, 1); # Returns 1.2e+99. $float = to_float(&#39;foo-1.23&#39;); # Returns -1.23000. $float = to_float(&#39;ick_1.23e99foo&#39;); # Returns 1.23000e+99.</code></pre> <h2 id="is_string">is_string</h2> <pre><code>my $bool = is_string($val);</code></pre> <p>Returns true if $val is a string, and false if it is not. All defined non-references are considered strings.</p> <pre><code>my $bool = is_string(&#39;foo&#39;); # Returns true. $bool = is_string(20001); # Returns true. $bool = is_string([]); # Returns false. $bool = is_string(undef); # Returns false.</code></pre> <h2 id="to_string">to_string</h2> <pre><code>my $string = to_string($val); $string = to_string($val, $length);</code></pre> <p>Converts $val into a string. If $val is a reference, the string value of the reference will be returned. Such a value may be a memory address, or some other value, if the stringification operator has been overridden for the object stored in $val. If the optional second argument $length is passed, to_string() will truncate the string to that length. If $length is 0 (zero), it will not limit the length of the return string. If $val is undefined, to_string() will return undef.</p> <pre><code>my $string = to_string(&#39;foo&#39;); # Returns &#39;foo&#39;. $string = to_string([]); # Returns &#39;ARRAY(0x101bec14)&#39;. $string = to_string(undef); # Returns undef. $string = to_string(&#39;hello&#39;, 4); # Returns &#39;hell&#39;.</code></pre> <h1 id="SUPPORT">SUPPORT</h1> <p>This module is stored in an open <a href="http://github.com/manwar/data-types/">GitHub repository</a>. Feel free to fork and contribute!</p> <p>Please file bug reports via <a href="http://github.com/manwar/data-types/issues/">GitHub Issues</a> or by sending mail to <a href="mailto:bug-Data-Types@rt.cpan.org">bug-Data-Types@rt.cpan.org</a>.</p> <p>Patches against Class::Meta are welcome. Please send bug reports to &lt;bug-data-types@rt.cpan.org&gt;.</p> <h1 id="AUTHOR">AUTHOR</h1> <p>David E. Wheeler &lt;david@justatheory.com&gt;</p> <p>Currently maintained by Mohammad S Anwar <code>&lt;mohammad.anwar @ yahoo.com&gt;</code></p> <h1 id="SEE-ALSO"><a id="SEE"></a>SEE ALSO</h1> <p><a href="/pod/perlfaq4#How-do-I-determine-whether-a-scalar-is-anumber%2Fwhole%2Finteger%2Ffloat%3F">perlfaq4</a> lists the most of the regular expressions used to identify the different numeric types used in this module.</p> <p><a href="/pod/String::Checker">String::Checker</a> also does some data type validation.</p> <p><a href="/pod/String::Scanf">String::Scanf</a> reimplements the C <code>sscanf()</code> function in perl, and also does data type validation and conversion.</p> <p><a href="/pod/Regexp::Common">Regexp::Common</a> contains many useful common regular expressions (surprise!), including some that can be used to identify data types.</p> <p>Arthur Bergman&#39;s <a href="/pod/types">types</a> pragma, offers compile-time data types for Perl 5.8.0. The data types include int, float, and string. I highly recommend using this pragma for fast, static data types.</p> <h1 id="COPYRIGHT-AND-LICENSE"><a id="COPYRIGHT"></a>COPYRIGHT AND LICENSE</h1> <p>Copyright (c) 2002-2011, David E. Wheeler. Some Rights Reserved.</p> <p>This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</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">&times;</button> <h4 class="modal-title">Module Install Instructions</h4> </div> <div class="modal-body"> <p>To install Data::Types, 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::Types</code></pre> <p><a href="/pod/CPAN">CPAN shell</a></p> <pre><code>perl -MCPAN -e shell install Data::Types</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">&times;</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>

Pages: 1 2 3 4 5 6 7 8 9 10