CINXE.COM
UNIVERSAL - base class for ALL classes (blessed references) - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>UNIVERSAL - base class for ALL classes (blessed references) - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/UNIVERSAL"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.14.4</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/UNIVERSAL">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/UNIVERSAL">5.40.1</a> <a class="dropdown-item" href="/5.40.0/UNIVERSAL">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/UNIVERSAL">5.38.3</a> <a class="dropdown-item" href="/5.38.2/UNIVERSAL">5.38.2</a> <a class="dropdown-item" href="/5.38.1/UNIVERSAL">5.38.1</a> <a class="dropdown-item" href="/5.38.0/UNIVERSAL">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/UNIVERSAL">5.36.3</a> <a class="dropdown-item" href="/5.36.2/UNIVERSAL">5.36.2</a> <a class="dropdown-item" href="/5.36.1/UNIVERSAL">5.36.1</a> <a class="dropdown-item" href="/5.36.0/UNIVERSAL">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/UNIVERSAL">5.34.3</a> <a class="dropdown-item" href="/5.34.2/UNIVERSAL">5.34.2</a> <a class="dropdown-item" href="/5.34.1/UNIVERSAL">5.34.1</a> <a class="dropdown-item" href="/5.34.0/UNIVERSAL">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/UNIVERSAL">5.32.1</a> <a class="dropdown-item" href="/5.32.0/UNIVERSAL">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/UNIVERSAL">5.30.3</a> <a class="dropdown-item" href="/5.30.2/UNIVERSAL">5.30.2</a> <a class="dropdown-item" href="/5.30.1/UNIVERSAL">5.30.1</a> <a class="dropdown-item" href="/5.30.0/UNIVERSAL">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/UNIVERSAL">5.28.3</a> <a class="dropdown-item" href="/5.28.2/UNIVERSAL">5.28.2</a> <a class="dropdown-item" href="/5.28.1/UNIVERSAL">5.28.1</a> <a class="dropdown-item" href="/5.28.0/UNIVERSAL">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/UNIVERSAL">5.26.3</a> <a class="dropdown-item" href="/5.26.2/UNIVERSAL">5.26.2</a> <a class="dropdown-item" href="/5.26.1/UNIVERSAL">5.26.1</a> <a class="dropdown-item" href="/5.26.0/UNIVERSAL">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/UNIVERSAL">5.24.4</a> <a class="dropdown-item" href="/5.24.3/UNIVERSAL">5.24.3</a> <a class="dropdown-item" href="/5.24.2/UNIVERSAL">5.24.2</a> <a class="dropdown-item" href="/5.24.1/UNIVERSAL">5.24.1</a> <a class="dropdown-item" href="/5.24.0/UNIVERSAL">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/UNIVERSAL">5.22.4</a> <a class="dropdown-item" href="/5.22.3/UNIVERSAL">5.22.3</a> <a class="dropdown-item" href="/5.22.2/UNIVERSAL">5.22.2</a> <a class="dropdown-item" href="/5.22.1/UNIVERSAL">5.22.1</a> <a class="dropdown-item" href="/5.22.0/UNIVERSAL">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/UNIVERSAL">5.20.3</a> <a class="dropdown-item" href="/5.20.2/UNIVERSAL">5.20.2</a> <a class="dropdown-item" href="/5.20.1/UNIVERSAL">5.20.1</a> <a class="dropdown-item" href="/5.20.0/UNIVERSAL">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/UNIVERSAL">5.18.4</a> <a class="dropdown-item" href="/5.18.3/UNIVERSAL">5.18.3</a> <a class="dropdown-item" href="/5.18.2/UNIVERSAL">5.18.2</a> <a class="dropdown-item" href="/5.18.1/UNIVERSAL">5.18.1</a> <a class="dropdown-item" href="/5.18.0/UNIVERSAL">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/UNIVERSAL">5.16.3</a> <a class="dropdown-item" href="/5.16.2/UNIVERSAL">5.16.2</a> <a class="dropdown-item" href="/5.16.1/UNIVERSAL">5.16.1</a> <a class="dropdown-item" href="/5.16.0/UNIVERSAL">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.14.4/UNIVERSAL">5.14.4</a> <a class="dropdown-item" href="/5.14.3/UNIVERSAL">5.14.3</a> <a class="dropdown-item" href="/5.14.2/UNIVERSAL">5.14.2</a> <a class="dropdown-item" href="/5.14.1/UNIVERSAL">5.14.1</a> <a class="dropdown-item" href="/5.14.0/UNIVERSAL">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/UNIVERSAL">5.12.5</a> <a class="dropdown-item" href="/5.12.4/UNIVERSAL">5.12.4</a> <a class="dropdown-item" href="/5.12.3/UNIVERSAL">5.12.3</a> <a class="dropdown-item" href="/5.12.2/UNIVERSAL">5.12.2</a> <a class="dropdown-item" href="/5.12.1/UNIVERSAL">5.12.1</a> <a class="dropdown-item" href="/5.12.0/UNIVERSAL">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/UNIVERSAL">5.10.1</a> <a class="dropdown-item" href="/5.10.0/UNIVERSAL">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/UNIVERSAL">5.8.9</a> <a class="dropdown-item" href="/5.8.8/UNIVERSAL">5.8.8</a> <a class="dropdown-item" href="/5.8.7/UNIVERSAL">5.8.7</a> <a class="dropdown-item" href="/5.8.6/UNIVERSAL">5.8.6</a> <a class="dropdown-item" href="/5.8.5/UNIVERSAL">5.8.5</a> <a class="dropdown-item" href="/5.8.4/UNIVERSAL">5.8.4</a> <a class="dropdown-item" href="/5.8.3/UNIVERSAL">5.8.3</a> <a class="dropdown-item" href="/5.8.2/UNIVERSAL">5.8.2</a> <a class="dropdown-item" href="/5.8.1/UNIVERSAL">5.8.1</a> <a class="dropdown-item" href="/5.8.0/UNIVERSAL">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/UNIVERSAL">5.6.2</a> <a class="dropdown-item" href="/5.6.1/UNIVERSAL">5.6.1</a> <a class="dropdown-item" href="/5.6.0/UNIVERSAL">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/UNIVERSAL">5.005_04</a> <a class="dropdown-item" href="/5.005_03/UNIVERSAL">5.005_03</a> <a class="dropdown-item" href="/5.005_02/UNIVERSAL">5.005_02</a> <a class="dropdown-item" href="/5.005_01/UNIVERSAL">5.005_01</a> <a class="dropdown-item" href="/5.005/UNIVERSAL">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/UNIVERSAL">blead</a> <a class="dropdown-item" href="/5.41.9/UNIVERSAL">5.41.9</a> <a class="dropdown-item" href="/5.41.8/UNIVERSAL">5.41.8</a> <a class="dropdown-item" href="/5.41.7/UNIVERSAL">5.41.7</a> <a class="dropdown-item" href="/5.41.6/UNIVERSAL">5.41.6</a> <a class="dropdown-item" href="/5.41.5/UNIVERSAL">5.41.5</a> <a class="dropdown-item" href="/5.41.4/UNIVERSAL">5.41.4</a> <a class="dropdown-item" href="/5.41.3/UNIVERSAL">5.41.3</a> <a class="dropdown-item" href="/5.41.2/UNIVERSAL">5.41.2</a> <a class="dropdown-item" href="/5.41.1/UNIVERSAL">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/UNIVERSAL">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/UNIVERSAL">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/UNIVERSAL">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/UNIVERSAL">5.39.10</a> <a class="dropdown-item" href="/5.39.9/UNIVERSAL">5.39.9</a> <a class="dropdown-item" href="/5.39.8/UNIVERSAL">5.39.8</a> <a class="dropdown-item" href="/5.39.7/UNIVERSAL">5.39.7</a> <a class="dropdown-item" href="/5.39.6/UNIVERSAL">5.39.6</a> <a class="dropdown-item" href="/5.39.5/UNIVERSAL">5.39.5</a> <a class="dropdown-item" href="/5.39.4/UNIVERSAL">5.39.4</a> <a class="dropdown-item" href="/5.39.3/UNIVERSAL">5.39.3</a> <a class="dropdown-item" href="/5.39.2/UNIVERSAL">5.39.2</a> <a class="dropdown-item" href="/5.39.1/UNIVERSAL">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/UNIVERSAL">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.14.4/perl">Perl</a> <a class="dropdown-item" href="/5.14.4/perlintro">Intro</a> <a class="dropdown-item" href="/5.14.4/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.14.4/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.14.4/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perlop">Operators</a> <a class="dropdown-item" href="/5.14.4/functions">Functions</a> <a class="dropdown-item" href="/5.14.4/variables">Variables</a> <a class="dropdown-item" href="/5.14.4/modules">Modules</a> <a class="dropdown-item" href="/5.14.4/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.14.4/perlcommunity">Community</a> <a class="dropdown-item" href="/5.14.4/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.14.4/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.14.4/UNIVERSAL">UNIVERSAL</a> <div id="more"> (<a href="/5.14.4/UNIVERSAL.txt">source</a>, <a href="https://metacpan.org/pod/UNIVERSAL">CPAN</a>) </div> <div id="moduleversion">version 1.08</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.14.4. <a href="/UNIVERSAL">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#WARNINGS">WARNINGS</a> </li> <li> <a class="text-decoration-none" href="#EXPORTS">EXPORTS</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>UNIVERSAL - base class for ALL classes (blessed references)</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>$is_io = $fd->isa("IO::Handle"); $is_io = Class->isa("IO::Handle"); $does_log = $obj->DOES("Logger"); $does_log = Class->DOES("Logger"); $sub = $obj->can("print"); $sub = Class->can("print"); $sub = eval { $ref->can("fandango") }; $ver = $obj->VERSION; # but never do this! $is_io = UNIVERSAL::isa($fd, "IO::Handle"); $sub = UNIVERSAL::can($obj, "print");</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p><code>UNIVERSAL</code> is the base class from which all blessed references inherit. See <a href="/5.14.4/perlobj">perlobj</a>.</p> <p><code>UNIVERSAL</code> provides the following methods:</p> <dl> <dt id="$obj->isa(-TYPE-)"><a class="permalink" href="#$obj-%3Eisa(-TYPE-)">#</a><a id="isa"></a><a id="obj-isa-TYPE"></a><code>$obj->isa( TYPE )</code></dt> <dd> </dd> <dt id="CLASS->isa(-TYPE-)"><a class="permalink" href="#CLASS-%3Eisa(-TYPE-)">#</a><a id="CLASS1"></a><a id="CLASS-isa-TYPE"></a><code>CLASS->isa( TYPE )</code></dt> <dd> </dd> <dt id="eval-{-VAL->isa(-TYPE-)-}"><a class="permalink" href="#eval-%7B-VAL-%3Eisa(-TYPE-)-%7D">#</a><a id="eval"></a><a id="eval-VAL-isa-TYPE"></a><code>eval { VAL->isa( TYPE ) }</code></dt> <dd> <p>Where</p> <dl> <dt id="TYPE"><a class="permalink" href="#TYPE">#</a><code>TYPE</code></dt> <dd> <p>is a package name</p> </dd> <dt id="$obj"><a class="permalink" href="#$obj">#</a><a id="obj"></a><code>$obj</code></dt> <dd> <p>is a blessed reference or a package name</p> </dd> <dt id="CLASS"><a class="permalink" href="#CLASS">#</a><code>CLASS</code></dt> <dd> <p>is a package name</p> </dd> <dt id="VAL"><a class="permalink" href="#VAL">#</a><code>VAL</code></dt> <dd> <p>is any of the above or an unblessed reference</p> </dd> </dl> <p>When used as an instance or class method (<code>$obj->isa( TYPE )</code>), <code>isa</code> returns <i>true</i> if $obj is blessed into package <code>TYPE</code> or inherits from package <code>TYPE</code>.</p> <p>When used as a class method (<code>CLASS->isa( TYPE )</code>, sometimes referred to as a static method), <code>isa</code> returns <i>true</i> if <code>CLASS</code> inherits from (or is itself) the name of the package <code>TYPE</code> or inherits from package <code>TYPE</code>.</p> <p>If you're not sure what you have (the <code>VAL</code> case), wrap the method call in an <code>eval</code> block to catch the exception if <code>VAL</code> is undefined.</p> <p>If you want to be sure that you're calling <code>isa</code> as a method, not a class, check the invocand with <code>blessed</code> from <a href="/5.14.4/Scalar::Util">Scalar::Util</a> first:</p> <pre><code>use Scalar::Util 'blessed'; if ( blessed( $obj ) && $obj->isa("Some::Class") { ... }</code></pre> </dd> <dt id="$obj->DOES(-ROLE-)"><a class="permalink" href="#$obj-%3EDOES(-ROLE-)">#</a><a id="DOES"></a><a id="obj-DOES-ROLE"></a><code>$obj->DOES( ROLE )</code></dt> <dd> </dd> <dt id="CLASS->DOES(-ROLE-)"><a class="permalink" href="#CLASS-%3EDOES(-ROLE-)">#</a><a id="CLASS2"></a><a id="CLASS-DOES-ROLE"></a><code>CLASS->DOES( ROLE )</code></dt> <dd> <p><code>DOES</code> checks if the object or class performs the role <code>ROLE</code>. A role is a named group of specific behavior (often methods of particular names and signatures), similar to a class, but not necessarily a complete class by itself. For example, logging or serialization may be roles.</p> <p><code>DOES</code> and <code>isa</code> are similar, in that if either is true, you know that the object or class on which you call the method can perform specific behavior. However, <code>DOES</code> is different from <code>isa</code> in that it does not care <i>how</i> the invocand performs the operations, merely that it does. (<code>isa</code> of course mandates an inheritance relationship. Other relationships include aggregation, delegation, and mocking.)</p> <p>By default, classes in Perl only perform the <code>UNIVERSAL</code> role, as well as the role of all classes in their inheritance. In other words, by default <code>DOES</code> responds identically to <code>isa</code>.</p> <p>There is a relationship between roles and classes, as each class implies the existence of a role of the same name. There is also a relationship between inheritance and roles, in that a subclass that inherits from an ancestor class implicitly performs any roles its parent performs. Thus you can use <code>DOES</code> in place of <code>isa</code> safely, as it will return true in all places where <code>isa</code> will return true (provided that any overridden <code>DOES</code> <i>and</i> <code>isa</code> methods behave appropriately).</p> </dd> <dt id="$obj->can(-METHOD-)"><a class="permalink" href="#$obj-%3Ecan(-METHOD-)">#</a><a id="can"></a><a id="obj-can-METHOD"></a><code>$obj->can( METHOD )</code></dt> <dd> </dd> <dt id="CLASS->can(-METHOD-)"><a class="permalink" href="#CLASS-%3Ecan(-METHOD-)">#</a><a id="CLASS3"></a><a id="CLASS-can-METHOD"></a><code>CLASS->can( METHOD )</code></dt> <dd> </dd> <dt id="eval-{-VAL->can(-METHOD-)-}"><a class="permalink" href="#eval-%7B-VAL-%3Ecan(-METHOD-)-%7D">#</a><a id="eval1"></a><a id="eval-VAL-can-METHOD"></a><code>eval { VAL->can( METHOD ) }</code></dt> <dd> <p><code>can</code> checks if the object or class has a method called <code>METHOD</code>. If it does, then it returns a reference to the sub. If it does not, then it returns <i>undef</i>. This includes methods inherited or imported by <code>$obj</code>, <code>CLASS</code>, or <code>VAL</code>.</p> <p><code>can</code> cannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overridden <code>can</code> appropriately), so a return value of <i>undef</i> does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see <a href="/5.14.4/perlsub">perlsub</a>) for methods they will handle via AUTOLOAD. For such 'dummy' subs, <code>can</code> will still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error.</p> <p>You may call <code>can</code> as a class (static) method or an object method.</p> <p>Again, the same rule about having a valid invocand applies -- use an <code>eval</code> block or <code>blessed</code> if you need to be extra paranoid.</p> </dd> <dt id="VERSION-(-[-REQUIRE-]-)"><a class="permalink" href="#VERSION-(-%5B-REQUIRE-%5D-)">#</a><a id="VERSION"></a><a id="VERSION-REQUIRE"></a><code>VERSION ( [ REQUIRE ] )</code></dt> <dd> <p><code>VERSION</code> will return the value of the variable <code>$VERSION</code> in the package the object is blessed into. If <code>REQUIRE</code> is given then it will do a comparison and die if the package version is not greater than or equal to <code>REQUIRE</code>. Both <code>$VERSION</code> or <code>REQUIRE</code> must be "lax" version numbers (as defined by the <a href="/5.14.4/version">version</a> module) or <code>VERSION</code> will die with an error.</p> <p><code>VERSION</code> can be called as either a class (static) method or an object method.</p> </dd> </dl> <h1 id="WARNINGS"><a class="permalink" href="#WARNINGS">#</a>WARNINGS</h1> <p><b>NOTE:</b> <code>can</code> directly uses Perl's internal code for method lookup, and <code>isa</code> uses a very similar method and cache-ing strategy. This may cause strange effects if the Perl code dynamically changes @ISA in any package.</p> <p>You may add other methods to the UNIVERSAL class via Perl or XS code. You do not need to <code>use UNIVERSAL</code> to make these methods available to your program (and you should not do so).</p> <h1 id="EXPORTS"><a class="permalink" href="#EXPORTS">#</a>EXPORTS</h1> <p>None by default.</p> <p>You may request the import of three functions (<code>isa</code>, <code>can</code>, and <code>VERSION</code>), <b>but this feature is deprecated and will be removed</b>. Please don't do this in new code.</p> <p>For example, previous versions of this documentation suggested using <code>isa</code> as a function to determine the type of a reference:</p> <pre><code>use UNIVERSAL 'isa'; $yes = isa $h, "HASH"; $yes = isa "Foo", "Bar";</code></pre> <p>The problem is that this code will <i>never</i> call an overridden <code>isa</code> method in any class. Instead, use <code>reftype</code> from <a href="/5.14.4/Scalar::Util">Scalar::Util</a> for the first case:</p> <pre><code>use Scalar::Util 'reftype'; $yes = reftype( $h ) eq "HASH";</code></pre> <p>and the method form of <code>isa</code> for the second:</p> <pre><code>$yes = Foo->isa("Bar");</code></pre> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>