CINXE.COM

perlbot - Bag o' Object Tricks (the BOT) - 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>perlbot - Bag o&#39; Object Tricks (the BOT) - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perlbot"> <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.2</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perlbot">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/perlbot">5.40.1</a> <a class="dropdown-item" href="/5.40.0/perlbot">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/perlbot">5.38.3</a> <a class="dropdown-item" href="/5.38.2/perlbot">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perlbot">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perlbot">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perlbot">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perlbot">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perlbot">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perlbot">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perlbot">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perlbot">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perlbot">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perlbot">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perlbot">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perlbot">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perlbot">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perlbot">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perlbot">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perlbot">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perlbot">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perlbot">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perlbot">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perlbot">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perlbot">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perlbot">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perlbot">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perlbot">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perlbot">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perlbot">5.24.3</a> <a class="dropdown-item" href="/5.24.2/perlbot">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perlbot">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perlbot">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perlbot">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perlbot">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perlbot">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perlbot">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perlbot">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perlbot">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perlbot">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perlbot">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perlbot">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perlbot">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perlbot">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perlbot">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perlbot">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perlbot">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perlbot">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perlbot">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perlbot">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perlbot">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perlbot">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perlbot">5.14.3</a> <a class="dropdown-item active" href="/5.14.2/perlbot">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perlbot">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perlbot">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perlbot">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perlbot">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perlbot">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perlbot">5.12.2</a> <a class="dropdown-item" href="/5.12.1/perlbot">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perlbot">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perlbot">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perlbot">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perlbot">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perlbot">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perlbot">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perlbot">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perlbot">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perlbot">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perlbot">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perlbot">5.8.2</a> <a class="dropdown-item" href="/5.8.1/perlbot">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perlbot">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perlbot">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perlbot">5.6.1</a> <a class="dropdown-item" href="/5.6.0/perlbot">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perlbot">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perlbot">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perlbot">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perlbot">5.005_01</a> <a class="dropdown-item" href="/5.005/perlbot">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/perlbot">blead</a> <a class="dropdown-item" href="/5.41.9/perlbot">5.41.9</a> <a class="dropdown-item" href="/5.41.8/perlbot">5.41.8</a> <a class="dropdown-item" href="/5.41.7/perlbot">5.41.7</a> <a class="dropdown-item" href="/5.41.6/perlbot">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perlbot">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perlbot">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perlbot">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perlbot">5.41.2</a> <a class="dropdown-item" href="/5.41.1/perlbot">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/perlbot">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/perlbot">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perlbot">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perlbot">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perlbot">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perlbot">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perlbot">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perlbot">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perlbot">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perlbot">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perlbot">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perlbot">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perlbot">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/perlbot">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.2/perl">Perl</a> <a class="dropdown-item" href="/5.14.2/perlintro">Intro</a> <a class="dropdown-item" href="/5.14.2/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.14.2/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.14.2/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.2/perlop">Operators</a> <a class="dropdown-item" href="/5.14.2/functions">Functions</a> <a class="dropdown-item" href="/5.14.2/variables">Variables</a> <a class="dropdown-item" href="/5.14.2/modules">Modules</a> <a class="dropdown-item" href="/5.14.2/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.2/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.14.2/perlcommunity">Community</a> <a class="dropdown-item" href="/5.14.2/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.2/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.2/perlbot">perlbot</a> <div id="more"> (<a href="/5.14.2/perlbot.txt">source</a>, <a href="https://metacpan.org/pod/perlbot">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.14.2. <a href="/perlbot">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="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#OO-SCALING-TIPS">OO SCALING TIPS</a> </li> <li> <a class="text-decoration-none" href="#INSTANCE-VARIABLES">INSTANCE VARIABLES</a> </li> <li> <a class="text-decoration-none" href="#SCALAR-INSTANCE-VARIABLES">SCALAR INSTANCE VARIABLES</a> </li> <li> <a class="text-decoration-none" href="#INSTANCE-VARIABLE-INHERITANCE">INSTANCE VARIABLE INHERITANCE</a> </li> <li> <a class="text-decoration-none" href="#OBJECT-RELATIONSHIPS">OBJECT RELATIONSHIPS</a> </li> <li> <a class="text-decoration-none" href="#OVERRIDING-SUPERCLASS-METHODS">OVERRIDING SUPERCLASS METHODS</a> </li> <li> <a class="text-decoration-none" href="#USING-RELATIONSHIP-WITH-SDBM">USING RELATIONSHIP WITH SDBM</a> </li> <li> <a class="text-decoration-none" href="#THINKING-OF-CODE-REUSE">THINKING OF CODE REUSE</a> </li> <li> <a class="text-decoration-none" href="#CLASS-CONTEXT-AND-THE-OBJECT">CLASS CONTEXT AND THE OBJECT</a> </li> <li> <a class="text-decoration-none" href="#INHERITING-A-CONSTRUCTOR">INHERITING A CONSTRUCTOR</a> </li> <li> <a class="text-decoration-none" href="#DELEGATION">DELEGATION</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perlbot - Bag o&#39; Object Tricks (the BOT)</p> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>The following collection of tricks and hints is intended to whet curious appetites about such things as the use of instance variables and the mechanics of object and class relationships. The reader is encouraged to consult relevant textbooks for discussion of Object Oriented definitions and methodology. This is not intended as a tutorial for object-oriented programming or as a comprehensive guide to Perl&#39;s object oriented features, nor should it be construed as a style guide. If you&#39;re looking for tutorials, be sure to read <a href="/5.14.2/perlboot">perlboot</a>, <a href="/5.14.2/perltoot">perltoot</a>, and <a href="/5.14.2/perltooc">perltooc</a>.</p> <p>The Perl motto still holds: There&#39;s more than one way to do it.</p> <h1 id="OO-SCALING-TIPS"><a class="permalink" href="#OO-SCALING-TIPS">#</a><a id="OO"></a>OO SCALING TIPS</h1> <ol> <li><p>Do not attempt to verify the type of $self. That&#39;ll break if the class is inherited, when the type of $self is valid but its package isn&#39;t what you expect. See rule 5.</p> </li> <li><p>If an object-oriented (OO) or indirect-object (IO) syntax was used, then the object is probably the correct type and there&#39;s no need to become paranoid about it. Perl isn&#39;t a paranoid language anyway. If people subvert the OO or IO syntax then they probably know what they&#39;re doing and you should let them do it. See rule 1.</p> </li> <li><p>Use the two-argument form of bless(). Let a subclass use your constructor. See <a href="#INHERITING-A-CONSTRUCTOR">&quot;INHERITING A CONSTRUCTOR&quot;</a>.</p> </li> <li><p>The subclass is allowed to know things about its immediate superclass, the superclass is allowed to know nothing about a subclass.</p> </li> <li><p>Don&#39;t be trigger happy with inheritance. A &quot;using&quot;, &quot;containing&quot;, or &quot;delegation&quot; relationship (some sort of aggregation, at least) is often more appropriate. See <a href="#OBJECT-RELATIONSHIPS">&quot;OBJECT RELATIONSHIPS&quot;</a>, <a href="#USING-RELATIONSHIP-WITH-SDBM">&quot;USING RELATIONSHIP WITH SDBM&quot;</a>, and <a href="#DELEGATION">&quot;DELEGATION&quot;</a>.</p> </li> <li><p>The object is the namespace. Make package globals accessible via the object. This will remove the guess work about the symbol&#39;s home package. See <a href="#CLASS-CONTEXT-AND-THE-OBJECT">&quot;CLASS CONTEXT AND THE OBJECT&quot;</a>.</p> </li> <li><p>IO syntax is certainly less noisy, but it is also prone to ambiguities that can cause difficult-to-find bugs. Allow people to use the sure-thing OO syntax, even if you don&#39;t like it.</p> </li> <li><p>Do not use function-call syntax on a method. You&#39;re going to be bitten someday. Someone might move that method into a superclass and your code will be broken. On top of that you&#39;re feeding the paranoia in rule 2.</p> </li> <li><p>Don&#39;t assume you know the home package of a method. You&#39;re making it difficult for someone to override that method. See <a href="#THINKING-OF-CODE-REUSE">&quot;THINKING OF CODE REUSE&quot;</a>.</p> </li> </ol> <h1 id="INSTANCE-VARIABLES"><a class="permalink" href="#INSTANCE-VARIABLES">#</a><a id="INSTANCE"></a>INSTANCE VARIABLES</h1> <p>An anonymous array or anonymous hash can be used to hold instance variables. Named parameters are also demonstrated.</p> <pre><code>package Foo; sub new { my $type = shift; my %params = @_; my $self = {}; $self-&gt;{&#39;High&#39;} = $params{&#39;High&#39;}; $self-&gt;{&#39;Low&#39;} = $params{&#39;Low&#39;}; bless $self, $type; } package Bar; sub new { my $type = shift; my %params = @_; my $self = []; $self-&gt;[0] = $params{&#39;Left&#39;}; $self-&gt;[1] = $params{&#39;Right&#39;}; bless $self, $type; } package main; $a = Foo-&gt;new( &#39;High&#39; =&gt; 42, &#39;Low&#39; =&gt; 11 ); print &quot;High=$a-&gt;{&#39;High&#39;}\n&quot;; print &quot;Low=$a-&gt;{&#39;Low&#39;}\n&quot;; $b = Bar-&gt;new( &#39;Left&#39; =&gt; 78, &#39;Right&#39; =&gt; 40 ); print &quot;Left=$b-&gt;[0]\n&quot;; print &quot;Right=$b-&gt;[1]\n&quot;;</code></pre> <h1 id="SCALAR-INSTANCE-VARIABLES"><a class="permalink" href="#SCALAR-INSTANCE-VARIABLES">#</a><a id="SCALAR"></a>SCALAR INSTANCE VARIABLES</h1> <p>An anonymous scalar can be used when only one instance variable is needed.</p> <pre><code>package Foo; sub new { my $type = shift; my $self; $self = shift; bless \$self, $type; } package main; $a = Foo-&gt;new( 42 ); print &quot;a=$$a\n&quot;;</code></pre> <h1 id="INSTANCE-VARIABLE-INHERITANCE"><a class="permalink" href="#INSTANCE-VARIABLE-INHERITANCE">#</a><a id="INSTANCE1"></a>INSTANCE VARIABLE INHERITANCE</h1> <p>This example demonstrates how one might inherit instance variables from a superclass for inclusion in the new class. This requires calling the superclass&#39;s constructor and adding one&#39;s own instance variables to the new object.</p> <pre><code>package Bar; sub new { my $type = shift; my $self = {}; $self-&gt;{&#39;buz&#39;} = 42; bless $self, $type; } package Foo; @ISA = qw( Bar ); sub new { my $type = shift; my $self = Bar-&gt;new; $self-&gt;{&#39;biz&#39;} = 11; bless $self, $type; } package main; $a = Foo-&gt;new; print &quot;buz = &quot;, $a-&gt;{&#39;buz&#39;}, &quot;\n&quot;; print &quot;biz = &quot;, $a-&gt;{&#39;biz&#39;}, &quot;\n&quot;;</code></pre> <h1 id="OBJECT-RELATIONSHIPS"><a class="permalink" href="#OBJECT-RELATIONSHIPS">#</a><a id="OBJECT"></a>OBJECT RELATIONSHIPS</h1> <p>The following demonstrates how one might implement &quot;containing&quot; and &quot;using&quot; relationships between objects.</p> <pre><code>package Bar; sub new { my $type = shift; my $self = {}; $self-&gt;{&#39;buz&#39;} = 42; bless $self, $type; } package Foo; sub new { my $type = shift; my $self = {}; $self-&gt;{&#39;Bar&#39;} = Bar-&gt;new; $self-&gt;{&#39;biz&#39;} = 11; bless $self, $type; } package main; $a = Foo-&gt;new; print &quot;buz = &quot;, $a-&gt;{&#39;Bar&#39;}-&gt;{&#39;buz&#39;}, &quot;\n&quot;; print &quot;biz = &quot;, $a-&gt;{&#39;biz&#39;}, &quot;\n&quot;;</code></pre> <h1 id="OVERRIDING-SUPERCLASS-METHODS"><a class="permalink" href="#OVERRIDING-SUPERCLASS-METHODS">#</a><a id="OVERRIDING"></a>OVERRIDING SUPERCLASS METHODS</h1> <p>The following example demonstrates how to override a superclass method and then call the overridden method. The <b>SUPER</b> pseudo-class allows the programmer to call an overridden superclass method without actually knowing where that method is defined.</p> <pre><code>package Buz; sub goo { print &quot;here&#39;s the goo\n&quot; } package Bar; @ISA = qw( Buz ); sub google { print &quot;google here\n&quot; } package Baz; sub mumble { print &quot;mumbling\n&quot; } package Foo; @ISA = qw( Bar Baz ); sub new { my $type = shift; bless [], $type; } sub grr { print &quot;grumble\n&quot; } sub goo { my $self = shift; $self-&gt;SUPER::goo(); } sub mumble { my $self = shift; $self-&gt;SUPER::mumble(); } sub google { my $self = shift; $self-&gt;SUPER::google(); } package main; $foo = Foo-&gt;new; $foo-&gt;mumble; $foo-&gt;grr; $foo-&gt;goo; $foo-&gt;google;</code></pre> <p>Note that <code>SUPER</code> refers to the superclasses of the current package (<code>Foo</code>), not to the superclasses of <code>$self</code>.</p> <h1 id="USING-RELATIONSHIP-WITH-SDBM"><a class="permalink" href="#USING-RELATIONSHIP-WITH-SDBM">#</a><a id="USING"></a>USING RELATIONSHIP WITH SDBM</h1> <p>This example demonstrates an interface for the SDBM class. This creates a &quot;using&quot; relationship between the SDBM class and the new class Mydbm.</p> <pre><code>package Mydbm; require SDBM_File; require Tie::Hash; @ISA = qw( Tie::Hash ); sub TIEHASH { my $type = shift; my $ref = SDBM_File-&gt;new(@_); bless {&#39;dbm&#39; =&gt; $ref}, $type; } sub FETCH { my $self = shift; my $ref = $self-&gt;{&#39;dbm&#39;}; $ref-&gt;FETCH(@_); } sub STORE { my $self = shift; if (defined $_[0]){ my $ref = $self-&gt;{&#39;dbm&#39;}; $ref-&gt;STORE(@_); } else { die &quot;Cannot STORE an undefined key in Mydbm\n&quot;; } } package main; use Fcntl qw( O_RDWR O_CREAT ); tie %foo, &quot;Mydbm&quot;, &quot;Sdbm&quot;, O_RDWR|O_CREAT, 0640; $foo{&#39;bar&#39;} = 123; print &quot;foo-bar = $foo{&#39;bar&#39;}\n&quot;; tie %bar, &quot;Mydbm&quot;, &quot;Sdbm2&quot;, O_RDWR|O_CREAT, 0640; $bar{&#39;Cathy&#39;} = 456; print &quot;bar-Cathy = $bar{&#39;Cathy&#39;}\n&quot;;</code></pre> <h1 id="THINKING-OF-CODE-REUSE"><a class="permalink" href="#THINKING-OF-CODE-REUSE">#</a><a id="THINKING"></a>THINKING OF CODE REUSE</h1> <p>One strength of Object-Oriented languages is the ease with which old code can use new code. The following examples will demonstrate first how one can hinder code reuse and then how one can promote code reuse.</p> <p>This first example illustrates a class which uses a fully-qualified method call to access the &quot;private&quot; method BAZ(). The second example will show that it is impossible to override the BAZ() method.</p> <pre><code>package FOO; sub new { my $type = shift; bless {}, $type; } sub bar { my $self = shift; $self-&gt;FOO::private::BAZ; } package FOO::private; sub BAZ { print &quot;in BAZ\n&quot;; } package main; $a = FOO-&gt;new; $a-&gt;bar;</code></pre> <p>Now we try to override the BAZ() method. We would like FOO::bar() to call GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls FOO::private::BAZ().</p> <pre><code>package FOO; sub new { my $type = shift; bless {}, $type; } sub bar { my $self = shift; $self-&gt;FOO::private::BAZ; } package FOO::private; sub BAZ { print &quot;in BAZ\n&quot;; } package GOOP; @ISA = qw( FOO ); sub new { my $type = shift; bless {}, $type; } sub BAZ { print &quot;in GOOP::BAZ\n&quot;; } package main; $a = GOOP-&gt;new; $a-&gt;bar;</code></pre> <p>To create reusable code we must modify class FOO, flattening class FOO::private. The next example shows a reusable class FOO which allows the method GOOP::BAZ() to be used in place of FOO::BAZ().</p> <pre><code>package FOO; sub new { my $type = shift; bless {}, $type; } sub bar { my $self = shift; $self-&gt;BAZ; } sub BAZ { print &quot;in BAZ\n&quot;; } package GOOP; @ISA = qw( FOO ); sub new { my $type = shift; bless {}, $type; } sub BAZ { print &quot;in GOOP::BAZ\n&quot;; } package main; $a = GOOP-&gt;new; $a-&gt;bar;</code></pre> <h1 id="CLASS-CONTEXT-AND-THE-OBJECT"><a class="permalink" href="#CLASS-CONTEXT-AND-THE-OBJECT">#</a><a id="CLASS"></a>CLASS CONTEXT AND THE OBJECT</h1> <p>Use the object to solve package and class context problems. Everything a method needs should be available via the object or should be passed as a parameter to the method.</p> <p>A class will sometimes have static or global data to be used by the methods. A subclass may want to override that data and replace it with new data. When this happens the superclass may not know how to find the new copy of the data.</p> <p>This problem can be solved by using the object to define the context of the method. Let the method look in the object for a reference to the data. The alternative is to force the method to go hunting for the data (&quot;Is it in my class, or in a subclass? Which subclass?&quot;), and this can be inconvenient and will lead to hackery. It is better just to let the object tell the method where that data is located.</p> <pre><code>package Bar; %fizzle = ( &#39;Password&#39; =&gt; &#39;XYZZY&#39; ); sub new { my $type = shift; my $self = {}; $self-&gt;{&#39;fizzle&#39;} = \%fizzle; bless $self, $type; } sub enter { my $self = shift; # Don&#39;t try to guess if we should use %Bar::fizzle # or %Foo::fizzle. The object already knows which # we should use, so just ask it. # my $fizzle = $self-&gt;{&#39;fizzle&#39;}; print &quot;The word is &quot;, $fizzle-&gt;{&#39;Password&#39;}, &quot;\n&quot;; } package Foo; @ISA = qw( Bar ); %fizzle = ( &#39;Password&#39; =&gt; &#39;Rumple&#39; ); sub new { my $type = shift; my $self = Bar-&gt;new; $self-&gt;{&#39;fizzle&#39;} = \%fizzle; bless $self, $type; } package main; $a = Bar-&gt;new; $b = Foo-&gt;new; $a-&gt;enter; $b-&gt;enter;</code></pre> <h1 id="INHERITING-A-CONSTRUCTOR"><a class="permalink" href="#INHERITING-A-CONSTRUCTOR">#</a><a id="INHERITING"></a>INHERITING A CONSTRUCTOR</h1> <p>An inheritable constructor should use the second form of bless() which allows blessing directly into a specified class. Notice in this example that the object will be a BAR not a FOO, even though the constructor is in class FOO.</p> <pre><code>package FOO; sub new { my $type = shift; my $self = {}; bless $self, $type; } sub baz { print &quot;in FOO::baz()\n&quot;; } package BAR; @ISA = qw(FOO); sub baz { print &quot;in BAR::baz()\n&quot;; } package main; $a = BAR-&gt;new; $a-&gt;baz;</code></pre> <h1 id="DELEGATION"><a class="permalink" href="#DELEGATION">#</a>DELEGATION</h1> <p>Some classes, such as SDBM_File, cannot be effectively subclassed because they create foreign objects. Such a class can be extended with some sort of aggregation technique such as the &quot;using&quot; relationship mentioned earlier or by delegation.</p> <p>The following example demonstrates delegation using an AUTOLOAD() function to perform message-forwarding. This will allow the Mydbm object to behave exactly like an SDBM_File object. The Mydbm class could now extend the behavior by adding custom FETCH() and STORE() methods, if this is desired.</p> <pre><code>package Mydbm; require SDBM_File; require Tie::Hash; @ISA = qw(Tie::Hash); sub TIEHASH { my $type = shift; my $ref = SDBM_File-&gt;new(@_); bless {&#39;delegate&#39; =&gt; $ref}; } sub AUTOLOAD { my $self = shift; # The Perl interpreter places the name of the # message in a variable called $AUTOLOAD. # DESTROY messages should never be propagated. return if $AUTOLOAD =~ /::DESTROY$/; # Remove the package name. $AUTOLOAD =~ s/^Mydbm:://; # Pass the message to the delegate. $self-&gt;{&#39;delegate&#39;}-&gt;$AUTOLOAD(@_); } package main; use Fcntl qw( O_RDWR O_CREAT ); tie %foo, &quot;Mydbm&quot;, &quot;adbm&quot;, O_RDWR|O_CREAT, 0640; $foo{&#39;bar&#39;} = 123; print &quot;foo-bar = $foo{&#39;bar&#39;}\n&quot;;</code></pre> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.14.2/perlboot">perlboot</a>, <a href="/5.14.2/perltoot">perltoot</a>, <a href="/5.14.2/perltooc">perltooc</a>.</p> </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>

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