File: perlootut.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (1085 lines) | stat: -rw-r--r-- 65,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlootut - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlootut</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-tutorials.html">Tutorials</a> &gt;
      
    
    perlootut
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlootut</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DATE">DATE</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OBJECT-ORIENTED-FUNDAMENTALS">OBJECT-ORIENTED FUNDAMENTALS</a><ul><li><a href="#Object">Object</a><li><a href="#Class">Class</a><li><a href="#Methods">Methods</a><li><a href="#Attributes">Attributes</a><li><a href="#Polymorphism">Polymorphism</a><li><a href="#Inheritance">Inheritance</a><li><a href="#Encapsulation">Encapsulation</a><li><a href="#Composition">Composition</a><li><a href="#Roles">Roles</a><li><a href="#When-to-Use-OO">When to Use OO</a></ul><li><a href="#PERL-OO-SYSTEMS">PERL OO SYSTEMS</a><ul><li><a href="#Moose">Moose</a><li><a href="#Class%3a%3aAccessor">Class::Accessor</a><li><a href="#Class%3a%3aTiny">Class::Tiny</a><li><a href="#Role%3a%3aTiny">Role::Tiny</a><li><a href="#OO-System-Summary">OO System Summary</a><li><a href="#Other-OO-Systems">Other OO Systems</a></ul><li><a href="#CONCLUSION">CONCLUSION</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlootut - Object-Oriented Programming in Perl Tutorial</p>
<a name="DATE"></a><h1>DATE</h1>
<p>This document was created in February, 2011, and the last major
revision was in February, 2013.</p>
<p>If you are reading this in the future then it's possible that the state
of the art has changed. We recommend you start by reading the perlootut
document in the latest stable release of Perl, rather than this
version.</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document provides an introduction to object-oriented programming
in Perl. It begins with a brief overview of the concepts behind object
oriented design. Then it introduces several different OO systems from
<a href="http://search.cpan.org/perldoc/http:#%2fsearch.cpan.org">CPAN</a> which build on top of what Perl
provides.</p>
<p>By default, Perl's built-in OO system is very minimal, leaving you to
do most of the work. This minimalism made a lot of sense in 1994, but
in the years since Perl 5.0 we've seen a number of common patterns
emerge in Perl OO. Fortunately, Perl's flexibility has allowed a rich
ecosystem of Perl OO systems to flourish.</p>
<p>If you want to know how Perl OO works under the hood, the <a href="perlobj.html">perlobj</a>
document explains the nitty gritty details.</p>
<p>This document assumes that you already understand the basics of Perl
syntax, variable types, operators, and subroutine calls. If you don't
understand these concepts yet, please read <a href="perlintro.html">perlintro</a> first. You
should also read the <a href="perlsyn.html">perlsyn</a>, <a href="perlop.html">perlop</a>, and <a href="perlsub.html">perlsub</a> documents.</p>
<a name="OBJECT-ORIENTED-FUNDAMENTALS"></a><h1>OBJECT-ORIENTED FUNDAMENTALS</h1>
<p>Most object systems share a number of common concepts. You've probably
heard terms like "class", "object, "method", and "attribute" before.
Understanding the concepts will make it much easier to read and write
object-oriented code. If you're already familiar with these terms, you
should still skim this section, since it explains each concept in terms
of Perl's OO implementation.</p>
<p>Perl's OO system is class-based. Class-based OO is fairly common. It's
used by Java, C++, C#, Python, Ruby, and many other languages. There
are other object orientation paradigms as well. JavaScript is the most
popular language to use another paradigm. JavaScript's OO system is
prototype-based.</p>
<a name="Object"></a><h2>Object</h2>
<p>An <b>object</b> is a data structure that bundles together data and
subroutines which operate on that data. An object's data is called
<b>attributes</b>, and its subroutines are called <b>methods</b>. An object can
be thought of as a noun (a person, a web service, a computer).</p>
<p>An object represents a single discrete thing. For example, an object
might represent a file. The attributes for a file object might include
its path, content, and last modification time. If we created an object
to represent <i>/etc/hostname</i> on a machine named "foo.example.com",
that object's path would be "/etc/hostname", its content would be
"foo\n", and it's last modification time would be 1304974868 seconds
since the beginning of the epoch.</p>
<p>The methods associated with a file might include <code class="inline"><a class="l_k" href="functions/rename.html">rename()</a></code> and
<code class="inline"><a class="l_k" href="functions/write.html">write()</a></code>.</p>
<p>In Perl most objects are hashes, but the OO systems we recommend keep
you from having to worry about this. In practice, it's best to consider
an object's internal data structure opaque.</p>
<a name="Class"></a><h2>Class</h2>
<p>A <b>class</b> defines the behavior of a category of objects. A class is a
name for a category (like "File"), and a class also defines the
behavior of objects in that category.</p>
<p>All objects belong to a specific class. For example, our
<i>/etc/hostname</i> object belongs to the <code class="inline"><span class="w">File</span></code>
 class. When we want to
create a specific object, we start with its class, and <b>construct</b> or
<b>instantiate</b> an object. A specific object is often referred to as an
<b>instance</b> of a class.</p>
<p>In Perl, any package can be a class. The difference between a package
which is a class and one which isn't is based on how the package is
used. Here's our "class declaration" for the <code class="inline"><span class="w">File</span></code>
 class:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li></ol></pre><p>In Perl, there is no special keyword for constructing an object.
However, most OO modules on CPAN use a method named <code class="inline"><span class="i">new</span><span class="s">(</span><span class="s">)</span></code>
 to
construct a new object:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$hostname</span> = <span class="w">File</span><span class="w">-&gt;new</span><span class="s">(</span></li><li>      <span class="w">path</span>          <span class="cm">=&gt;</span> <span class="q">&#39;/etc/hostname&#39;</span><span class="cm">,</span></li><li>      <span class="w">content</span>       <span class="cm">=&gt;</span> <span class="q">&quot;foo\n&quot;</span><span class="cm">,</span></li><li>      <span class="w">last_mod_time</span> <span class="cm">=&gt;</span> <span class="n">1304974868</span><span class="cm">,</span></li><li>  <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(Don't worry about that <code class="inline">-&gt;</code>
 operator, it will be explained
later.)</p>
<a name="Blessing"></a><h3>Blessing</h3>
<p>As we said earlier, most Perl objects are hashes, but an object can be
an instance of any Perl data type (scalar, array, etc.). Turning a
plain data structure into an object is done by <b>blessing</b> that data
structure using Perl's <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> function.</p>
<p>While we strongly suggest you don't build your objects from scratch,
you should know the term <b>bless</b>. A <b>blessed</b> data structure (aka "a
referent") is an object. We sometimes say that an object has been
"blessed into a class".</p>
<p>Once a referent has been blessed, the <code class="inline"><span class="w">blessed</span></code>
 function from the
<a href="Scalar/Util.html">Scalar::Util</a> core module can tell us its class name. This subroutine
returns an object's class when passed an object, and false otherwise.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Scalar::Util</span> <span class="q">&#39;blessed&#39;</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="i">blessed</span><span class="s">(</span><span class="i">$hash</span><span class="s">)</span><span class="sc">;</span>      <span class="c"># undef</span></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="i">blessed</span><span class="s">(</span><span class="i">$hostname</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># File</span></li></ol></pre><a name="Constructor"></a><h3>Constructor</h3>
<p>A <b>constructor</b> creates a new object. In Perl, a class's constructor
is just another method, unlike some other languages, which provide
syntax for constructors. Most Perl classes use <code class="inline"><span class="w">new</span></code>
 as the name for
their constructor:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="w">File</span><span class="w">-&gt;new</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Methods"></a><h2>Methods</h2>
<p>You already learned that a <b>method</b> is a subroutine that operates on
an object. You can think of a method as the things that an object can
<i>do</i>. If an object is a noun, then methods are its verbs (save, print,
open).</p>
<p>In Perl, methods are simply subroutines that live in a class's package.
Methods are always written to receive the object as their first
argument:</p>
<pre class="verbatim"><ol><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This file is at &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <span class="i">$file</span><span class="i">-&gt;print_info</span><span class="sc">;</span></li><li>  <span class="c"># The file is at /etc/hostname</span></li></ol></pre><p>What makes a method special is <i>how it's called</i>. The arrow operator
(<code class="inline">-&gt;</code>
) tells Perl that we are calling a method.</p>
<p>When we make a method call, Perl arranges for the method's <b>invocant</b>
to be passed as the first argument. <b>Invocant</b> is a fancy name for the
thing on the left side of the arrow. The invocant can either be a class
name or an object. We can also pass additional arguments to the method:</p>
<pre class="verbatim"><ol><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>   = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$prefix</span> = <a class="l_k" href="functions/shift.html">shift</a> // <span class="q">&quot;This file is at &quot;</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="i">$prefix</span><span class="cm">,</span> <span class="q">&quot;, &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <span class="i">$file</span><span class="i">-&gt;print_info</span><span class="s">(</span><span class="q">&quot;The file is located at &quot;</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="c"># The file is located at /etc/hostname</span></li></ol></pre><a name="Attributes"></a><h2>Attributes</h2>
<p>Each class can define its <b>attributes</b>. When we instantiate an object,
we assign values to those attributes. For example, every <code class="inline"><span class="w">File</span></code>
 object
has a path. Attributes are sometimes called <b>properties</b>.</p>
<p>Perl has no special syntax for attributes. Under the hood, attributes
are often stored as keys in the object's underlying hash, but don't
worry about this.</p>
<p>We recommend that you only access attributes via <b>accessor</b> methods.
These are methods that can get or set the value of each attribute. We
saw this earlier in the <code class="inline"><span class="i">print_info</span><span class="s">(</span><span class="s">)</span></code>
 example, which calls <code class="inline"><span class="i">$self</span><span class="i">-&gt;path</span></code>
.</p>
<p>You might also see the terms <b>getter</b> and <b>setter</b>. These are two
types of accessors. A getter gets the attribute's value, while a setter
sets it. Another term for a setter is <b>mutator</b></p>
<p>Attributes are typically defined as read-only or read-write. Read-only
attributes can only be set when the object is first created, while
read-write attributes can be altered at any time.</p>
<p>The value of an attribute may itself be another object. For example,
instead of returning its last mod time as a number, the <code class="inline"><span class="w">File</span></code>
 class
could return a <a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> object representing that value.</p>
<p>It's possible to have a class that does not expose any publicly
settable attributes. Not every class has attributes and methods.</p>
<a name="Polymorphism"></a><h2>Polymorphism</h2>
<p><b>Polymorphism</b> is a fancy way of saying that objects from two
different classes share an API. For example, we could have <code class="inline"><span class="w">File</span></code>
 and
<code class="inline"><span class="w">WebPage</span></code>
 classes which both have a <code class="inline"><span class="i">print_content</span><span class="s">(</span><span class="s">)</span></code>
 method. This
method might produce different output for each class, but they share a
common interface.</p>
<p>While the two classes may differ in many ways, when it comes to the
<code class="inline"><span class="i">print_content</span><span class="s">(</span><span class="s">)</span></code>
 method, they are the same. This means that we can
try to call the <code class="inline"><span class="i">print_content</span><span class="s">(</span><span class="s">)</span></code>
 method on an object of either class,
and <b>we don't have to know what class the object belongs to!</b></p>
<p>Polymorphism is one of the key concepts of object-oriented design.</p>
<a name="Inheritance"></a><h2>Inheritance</h2>
<p><b>Inheritance</b> lets you create a specialized version of an existing
class. Inheritance lets the new class reuse the methods and attributes
of another class.</p>
<p>For example, we could create an <code class="inline"><span class="w">File::MP3</span></code>
 class which <b>inherits</b>
from <code class="inline"><span class="w">File</span></code>
. An <code class="inline"><span class="w">File::MP3</span></code>
 <b>is-a</b> <i>more specific</i> type of <code class="inline"><span class="w">File</span></code>
.
All mp3 files are files, but not all files are mp3 files.</p>
<p>We often refer to inheritance relationships as <b>parent-child</b> or
<code class="inline"><span class="w">superclass</span></code>
/<code class="inline"><span class="w">subclass</span></code>
 relationships. Sometimes we say that the
child has an <b>is-a</b> relationship with its parent class.</p>
<p><code class="inline"><span class="w">File</span></code>
 is a <b>superclass</b> of <code class="inline"><span class="w">File::MP3</span></code>
, and <code class="inline"><span class="w">File::MP3</span></code>
 is a
<b>subclass</b> of <code class="inline"><span class="w">File</span></code>
.</p>
<pre class="verbatim"><ol><li><a name="package-File::MP3"></a>  package <span class="i">File::MP3</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">parent</span> <span class="q">&#39;File&#39;</span><span class="sc">;</span></li></ol></pre><p>The <a href="parent.html">parent</a> module is one of several ways that Perl lets you define
inheritance relationships.</p>
<p>Perl allows multiple inheritance, which means that a class can inherit
from multiple parents. While this is possible, we strongly recommend
against it. Generally, you can use <b>roles</b> to do everything you can do
with multiple inheritance, but in a cleaner way.</p>
<p>Note that there's nothing wrong with defining multiple subclasses of a
given class. This is both common and safe. For example, we might define
<code class="inline"><span class="w">File::MP3::FixedBitrate</span></code>
 and <code class="inline"><span class="w">File::MP3::VariableBitrate</span></code>
 classes to
distinguish between different types of mp3 file.</p>
<a name="Overriding-methods-and-method-resolution"></a><h3>Overriding methods and method resolution</h3>
<p>Inheritance allows two classes to share code. By default, every method
in the parent class is also available in the child. The child can
explicitly <b>override</b> a parent's method to provide its own
implementation. For example, if we have an <code class="inline"><span class="w">File::MP3</span></code>
 object, it has
the <code class="inline"><span class="i">print_info</span><span class="s">(</span><span class="s">)</span></code>
 method from <code class="inline"><span class="w">File</span></code>
:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$cage</span> = <span class="w">File::MP3</span><span class="w">-&gt;new</span><span class="s">(</span></li><li>      <span class="w">path</span>          <span class="cm">=&gt;</span> <span class="q">&#39;mp3s/My-Body-Is-a-Cage.mp3&#39;</span><span class="cm">,</span></li><li>      <span class="w">content</span>       <span class="cm">=&gt;</span> <span class="i">$mp3_data</span><span class="cm">,</span></li><li>      <span class="w">last_mod_time</span> <span class="cm">=&gt;</span> <span class="n">1304974868</span><span class="cm">,</span></li><li>      <span class="w">title</span>         <span class="cm">=&gt;</span> <span class="q">&#39;My Body Is a Cage&#39;</span><span class="cm">,</span></li><li>  <span class="s">)</span><span class="sc">;</span></li><li></li><li>  <span class="i">$cage</span><span class="i">-&gt;print_info</span><span class="sc">;</span></li><li>  <span class="c"># The file is at mp3s/My-Body-Is-a-Cage.mp3</span></li></ol></pre><p>If we wanted to include the mp3's title in the greeting, we could
override the method:</p>
<pre class="verbatim"><ol><li><a name="package-File::MP3"></a>  package <span class="i">File::MP3</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">parent</span> <span class="q">&#39;File&#39;</span><span class="sc">;</span></li><li></li><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This file is at &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Its title is &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;title</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <span class="i">$cage</span><span class="i">-&gt;print_info</span><span class="sc">;</span></li><li>  <span class="c"># The file is at mp3s/My-Body-Is-a-Cage.mp3</span></li><li>  <span class="c"># Its title is My Body Is a Cage</span></li></ol></pre><p>The process of determining what method should be used is called
<b>method resolution</b>. What Perl does is look at the object's class
first (<code class="inline"><span class="w">File::MP3</span></code>
 in this case). If that class defines the method,
then that class's version of the method is called. If not, Perl looks
at each parent class in turn. For <code class="inline"><span class="w">File::MP3</span></code>
, its only parent is
<code class="inline"><span class="w">File</span></code>
. If <code class="inline"><span class="w">File::MP3</span></code>
 does not define the method, but <code class="inline"><span class="w">File</span></code>
 does,
then Perl calls the method in <code class="inline"><span class="w">File</span></code>
.</p>
<p>If <code class="inline"><span class="w">File</span></code>
 inherited from <code class="inline"><span class="w">DataSource</span></code>
, which inherited from <code class="inline"><span class="w">Thing</span></code>
,
then Perl would keep looking "up the chain" if necessary.</p>
<p>It is possible to explicitly call a parent method from a child:</p>
<pre class="verbatim"><ol><li><a name="package-File::MP3"></a>  package <span class="i">File::MP3</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">parent</span> <span class="q">&#39;File&#39;</span><span class="sc">;</span></li><li></li><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <span class="i">$self</span><span class="i">-&gt;SUPER::print_info</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Its title is &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;title</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>The <code class="inline"><span class="w">SUPER::</span></code>
 bit tells Perl to look for the <code class="inline"><span class="i">print_info</span><span class="s">(</span><span class="s">)</span></code>
 in the
<code class="inline"><span class="w">File::MP3</span></code>
 class's inheritance chain. When it finds the parent class
that implements this method, the method is called.</p>
<p>We mentioned multiple inheritance earlier. The main problem with
multiple inheritance is that it greatly complicates method resolution.
See <a href="perlobj.html">perlobj</a> for more details.</p>
<a name="Encapsulation"></a><h2>Encapsulation</h2>
<p><b>Encapsulation</b> is the idea that an object is opaque. When another
developer uses your class, they don't need to know <i>how</i> it is
implemented, they just need to know <i>what</i> it does.</p>
<p>Encapsulation is important for several reasons. First, it allows you to
separate the public API from the private implementation. This means you
can change that implementation without breaking the API.</p>
<p>Second, when classes are well encapsulated, they become easier to
subclass. Ideally, a subclass uses the same APIs to access object data
that its parent class uses. In reality, subclassing sometimes involves
violating encapsulation, but a good API can minimize the need to do
this.</p>
<p>We mentioned earlier that most Perl objects are implemented as hashes
under the hood. The principle of encapsulation tells us that we should
not rely on this. Instead, we should use accessor methods to access the
data in that hash. The object systems that we recommend below all
automate the generation of accessor methods. If you use one of them,
you should never have to access the object as a hash directly.</p>
<a name="Composition"></a><h2>Composition</h2>
<p>In object-oriented code, we often find that one object references
another object. This is called <b>composition</b>, or a <b>has-a</b>
relationship.</p>
<p>Earlier, we mentioned that the <code class="inline"><span class="w">File</span></code>
 class's <code class="inline"><span class="w">last_mod_time</span></code>

accessor could return a <a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> object. This is a perfect example
of composition. We could go even further, and make the <code class="inline"><span class="w">path</span></code>
 and
<code class="inline"><span class="w">content</span></code>
 accessors return objects as well. The <code class="inline"><span class="w">File</span></code>
 class would
then be <b>composed</b> of several other objects.</p>
<a name="Roles"></a><h2>Roles</h2>
<p><b>Roles</b> are something that a class <i>does</i>, rather than something that
it <i>is</i>. Roles are relatively new to Perl, but have become rather
popular. Roles are <b>applied</b> to classes. Sometimes we say that classes
<b>consume</b> roles.</p>
<p>Roles are an alternative to inheritance for providing polymorphism.
Let's assume we have two classes, <code class="inline"><span class="w">Radio</span></code>
 and <code class="inline"><span class="w">Computer</span></code>
. Both of
these things have on/off switches. We want to model that in our class
definitions.</p>
<p>We could have both classes inherit from a common parent, like
<code class="inline"><span class="w">Machine</span></code>
, but not all machines have on/off switches. We could create
a parent class called <code class="inline"><span class="w">HasOnOffSwitch</span></code>
, but that is very artificial.
Radios and computers are not specializations of this parent. This
parent is really a rather ridiculous creation.</p>
<p>This is where roles come in. It makes a lot of sense to create a
<code class="inline"><span class="w">HasOnOffSwitch</span></code>
 role and apply it to both classes. This role would
define a known API like providing <code class="inline"><span class="i">turn_on</span><span class="s">(</span><span class="s">)</span></code>
 and <code class="inline"><span class="i">turn_off</span><span class="s">(</span><span class="s">)</span></code>

methods.</p>
<p>Perl does not have any built-in way to express roles. In the past,
people just bit the bullet and used multiple inheritance. Nowadays,
there are several good choices on CPAN for using roles.</p>
<a name="When-to-Use-OO"></a><h2>When to Use OO</h2>
<p>Object Orientation is not the best solution to every problem. In <i>Perl
Best Practices</i> (copyright 2004, Published by O'Reilly Media, Inc.),
Damian Conway provides a list of criteria to use when deciding if OO is
the right fit for your problem:</p>
<ul>
<li>
<p>The system being designed is large, or is likely to become large.</p>
</li>
<li>
<p>The data can be aggregated into obvious structures, especially if
there's a large amount of data in each aggregate.</p>
</li>
<li>
<p>The various types of data aggregate form a natural hierarchy that
facilitates the use of inheritance and polymorphism.</p>
</li>
<li>
<p>You have a piece of data on which many different operations are
applied.</p>
</li>
<li>
<p>You need to perform the same general operations on related types of
data, but with slight variations depending on the specific type of data
the operations are applied to.</p>
</li>
<li>
<p>It's likely you'll have to add new data types later.</p>
</li>
<li>
<p>The typical interactions between pieces of data are best represented by
operators.</p>
</li>
<li>
<p>The implementation of individual components of the system is likely to
change over time.</p>
</li>
<li>
<p>The system design is already object-oriented.</p>
</li>
<li>
<p>Large numbers of other programmers will be using your code modules.</p>
</li>
</ul>
<a name="PERL-OO-SYSTEMS"></a><h1>PERL OO SYSTEMS</h1>
<p>As we mentioned before, Perl's built-in OO system is very minimal, but
also quite flexible. Over the years, many people have developed systems
which build on top of Perl's built-in system to provide more features
and convenience.</p>
<p>We strongly recommend that you use one of these systems. Even the most
minimal of them eliminates a lot of repetitive boilerplate. There's
really no good reason to write your classes from scratch in Perl.</p>
<p>If you are interested in the guts underlying these systems, check out
<a href="perlobj.html">perlobj</a>.</p>
<a name="Moose"></a><h2>Moose</h2>
<p><a href="http://search.cpan.org/perldoc/Moose">Moose</a> bills itself as a "postmodern object system for Perl 5". Don't
be scared, the "postmodern" label is a callback to Larry's description
of Perl as "the first postmodern computer language".</p>
<p><code class="inline"><span class="w">Moose</span></code>
 provides a complete, modern OO system. Its biggest influence
is the Common Lisp Object System, but it also borrows ideas from
Smalltalk and several other languages. <code class="inline"><span class="w">Moose</span></code>
 was created by Stevan
Little, and draws heavily from his work on the Perl 6 OO design.</p>
<p>Here is our <code class="inline"><span class="w">File</span></code>
 class using <code class="inline"><span class="w">Moose</span></code>
:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Moose</span><span class="sc">;</span></li><li></li><li>  <span class="w">has</span> <span class="w">path</span>          <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">has</span> <span class="w">content</span>       <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">has</span> <span class="w">last_mod_time</span> <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This file is at &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p><code class="inline"><span class="w">Moose</span></code>
 provides a number of features:</p>
<ul>
<li><a name="*-Declarative-sugar"></a><b>Declarative sugar</b>
<p><code class="inline"><span class="w">Moose</span></code>
 provides a layer of declarative "sugar" for defining classes.
That sugar is just a set of exported functions that make declaring how
your class works simpler and more palatable.  This lets you describe
<i>what</i> your class is, rather than having to tell Perl <i>how</i> to
implement your class.</p>
<p>The <code class="inline"><span class="i">has</span><span class="s">(</span><span class="s">)</span></code>
 subroutine declares an attribute, and <code class="inline"><span class="w">Moose</span></code>

automatically creates accessors for these attributes. It also takes
care of creating a <code class="inline"><span class="i">new</span><span class="s">(</span><span class="s">)</span></code>
 method for you. This constructor knows
about the attributes you declared, so you can set them when creating a
new <code class="inline"><span class="w">File</span></code>
.</p>
</li>
<li><a name="*-Roles-built-in"></a><b>Roles built-in</b>
<p><code class="inline"><span class="w">Moose</span></code>
 lets you define roles the same way you define classes:</p>
<pre class="verbatim"><ol><li><a name="package-HasOnOffSwitch"></a>  package <span class="i">HasOnOffSwitch</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Moose::Role</span><span class="sc">;</span></li><li></li><li>  <span class="w">has</span> <span class="w">is_on</span> <span class="cm">=&gt;</span> <span class="s">(</span></li><li>      <span class="w">is</span>  <span class="cm">=&gt;</span> <span class="q">&#39;rw&#39;</span><span class="cm">,</span></li><li>      <span class="w">isa</span> <span class="cm">=&gt;</span> <span class="q">&#39;Bool&#39;</span><span class="cm">,</span></li><li>  <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="turn_on"></a>  sub <span class="m">turn_on</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;is_on</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="turn_off"></a>  sub <span class="m">turn_off</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;is_on</span><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre></li>
<li><a name="*-A-miniature-type-system"></a><b>A miniature type system</b>
<p>In the example above, you can see that we passed <code class="inline"><span class="w">isa</span> <span class="cm">=&gt;</span> <span class="q">&#39;Bool&#39;</span></code>

to <code class="inline"><span class="i">has</span><span class="s">(</span><span class="s">)</span></code>
 when creating our <code class="inline"><span class="w">is_on</span></code>
 attribute. This tells <code class="inline"><span class="w">Moose</span></code>

that this attribute must be a boolean value. If we try to set it to an
invalid value, our code will throw an error.</p>
</li>
<li><a name="*-Full-introspection-and-manipulation"></a><b>Full introspection and manipulation</b>
<p>Perl's built-in introspection features are fairly minimal. <code class="inline"><span class="w">Moose</span></code>

builds on top of them and creates a full introspection layer for your
classes. This lets you ask questions like "what methods does the File
class implement?" It also lets you modify your classes
programmatically.</p>
</li>
<li><a name="*-Self-hosted-and-extensible"></a><b>Self-hosted and extensible</b>
<p><code class="inline"><span class="w">Moose</span></code>
 describes itself using its own introspection API. Besides
being a cool trick, this means that you can extend <code class="inline"><span class="w">Moose</span></code>
 using
<code class="inline"><span class="w">Moose</span></code>
 itself.</p>
</li>
<li><a name="*-Rich-ecosystem"></a><b>Rich ecosystem</b>
<p>There is a rich ecosystem of <code class="inline"><span class="w">Moose</span></code>
 extensions on CPAN under the
<a href="http://search.cpan.org/perldoc/http:#%2fsearch.cpan.org%2fsearch%3fquery%3dMooseX%26mode%3ddist">MooseX</a>
namespace. In addition, many modules on CPAN already use <code class="inline"><span class="w">Moose</span></code>
,
providing you with lots of examples to learn from.</p>
</li>
<li><a name="*-Many-more-features"></a><b>Many more features</b>
<p><code class="inline"><span class="w">Moose</span></code>
 is a very powerful tool, and we can't cover all of its
features here. We encourage you to learn more by reading the <code class="inline"><span class="w">Moose</span></code>

documentation, starting with
<a href="http://search.cpan.org/perldoc/http:#%2fsearch.cpan.org%2fperldoc%3fMoose%3a%3aManual">Moose::Manual</a>.</p>
</li>
</ul>
<p>Of course, <code class="inline"><span class="w">Moose</span></code>
 isn't perfect.</p>
<p><code class="inline"><span class="w">Moose</span></code>
 can make your code slower to load. <code class="inline"><span class="w">Moose</span></code>
 itself is not
small, and it does a <i>lot</i> of code generation when you define your
class. This code generation means that your runtime code is as fast as
it can be, but you pay for this when your modules are first loaded.</p>
<p>This load time hit can be a problem when startup speed is important,
such as with a command-line script or a "plain vanilla" CGI script that
must be loaded each time it is executed.</p>
<p>Before you panic, know that many people do use <code class="inline"><span class="w">Moose</span></code>
 for
command-line tools and other startup-sensitive code. We encourage you
to try <code class="inline"><span class="w">Moose</span></code>
 out first before worrying about startup speed.</p>
<p><code class="inline"><span class="w">Moose</span></code>
 also has several dependencies on other modules. Most of these
are small stand-alone modules, a number of which have been spun off
from <code class="inline"><span class="w">Moose</span></code>
. <code class="inline"><span class="w">Moose</span></code>
 itself, and some of its dependencies, require a
compiler. If you need to install your software on a system without a
compiler, or if having <i>any</i> dependencies is a problem, then <code class="inline"><span class="w">Moose</span></code>

may not be right for you.</p>
<a name="Moo"></a><h3>Moo</h3>
<p>If you try <code class="inline"><span class="w">Moose</span></code>
 and find that one of these issues is preventing you
from using <code class="inline"><span class="w">Moose</span></code>
, we encourage you to consider <a href="http://search.cpan.org/perldoc/Moo">Moo</a> next. <code class="inline"><span class="w">Moo</span></code>

implements a subset of <code class="inline"><span class="w">Moose</span></code>
's functionality in a simpler package.
For most features that it does implement, the end-user API is
<i>identical</i> to <code class="inline"><span class="w">Moose</span></code>
, meaning you can switch from <code class="inline"><span class="w">Moo</span></code>
 to
<code class="inline"><span class="w">Moose</span></code>
 quite easily.</p>
<p><code class="inline"><span class="w">Moo</span></code>
 does not implement most of <code class="inline"><span class="w">Moose</span></code>
's introspection API, so it's
often faster when loading your modules. Additionally, none of its
dependencies require XS, so it can be installed on machines without a
compiler.</p>
<p>One of <code class="inline"><span class="w">Moo</span></code>
's most compelling features is its interoperability with
<code class="inline"><span class="w">Moose</span></code>
. When someone tries to use <code class="inline"><span class="w">Moose</span></code>
's introspection API on a
<code class="inline"><span class="w">Moo</span></code>
 class or role, it is transparently inflated into a <code class="inline"><span class="w">Moose</span></code>

class or role. This makes it easier to incorporate <code class="inline"><span class="w">Moo</span></code>
-using code
into a <code class="inline"><span class="w">Moose</span></code>
 code base and vice versa.</p>
<p>For example, a <code class="inline"><span class="w">Moose</span></code>
 class can subclass a <code class="inline"><span class="w">Moo</span></code>
 class using
<code class="inline"><span class="w">extends</span></code>
 or consume a <code class="inline"><span class="w">Moo</span></code>
 role using <code class="inline"><span class="w">with</span></code>
.</p>
<p>The <code class="inline"><span class="w">Moose</span></code>
 authors hope that one day <code class="inline"><span class="w">Moo</span></code>
 can be made obsolete by
improving <code class="inline"><span class="w">Moose</span></code>
 enough, but for now it provides a worthwhile
alternative to <code class="inline"><span class="w">Moose</span></code>
.</p>
<a name="Class%3a%3aAccessor"></a><h2>Class::Accessor</h2>
<p><a href="http://search.cpan.org/perldoc/Class::Accessor">Class::Accessor</a> is the polar opposite of <code class="inline"><span class="w">Moose</span></code>
. It provides very
few features, nor is it self-hosting.</p>
<p>It is, however, very simple, pure Perl, and it has no non-core
dependencies. It also provides a "Moose-like" API on demand for the
features it supports.</p>
<p>Even though it doesn't do much, it is still preferable to writing your
own classes from scratch.</p>
<p>Here's our <code class="inline"><span class="w">File</span></code>
 class with <code class="inline"><span class="w">Class::Accessor</span></code>
:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Class::Accessor</span> <span class="q">&#39;antlers&#39;</span><span class="sc">;</span></li><li></li><li>  <span class="w">has</span> <span class="w">path</span>          <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">has</span> <span class="w">content</span>       <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">has</span> <span class="w">last_mod_time</span> <span class="cm">=&gt;</span> <span class="s">(</span> <span class="w">is</span> <span class="cm">=&gt;</span> <span class="q">&#39;ro&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This file is at &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>The <code class="inline"><span class="w">antlers</span></code>
 import flag tells <code class="inline"><span class="w">Class::Accessor</span></code>
 that you want to
define your attributes using <code class="inline"><span class="w">Moose</span></code>
-like syntax. The only parameter
that you can pass to <code class="inline"><span class="w">has</span></code>
 is <code class="inline"><span class="w">is</span></code>
. We recommend that you use this
Moose-like syntax if you choose <code class="inline"><span class="w">Class::Accessor</span></code>
 since it means you
will have a smoother upgrade path if you later decide to move to
<code class="inline"><span class="w">Moose</span></code>
.</p>
<p>Like <code class="inline"><span class="w">Moose</span></code>
, <code class="inline"><span class="w">Class::Accessor</span></code>
 generates accessor methods and a
constructor for your class.</p>
<a name="Class%3a%3aTiny"></a><h2>Class::Tiny</h2>
<p>Finally, we have <a href="http://search.cpan.org/perldoc/Class::Tiny">Class::Tiny</a>. This module truly lives up to its
name. It has an incredibly minimal API and absolutely no dependencies
on any recent Perl. Still, we think it's a lot easier to use than
writing your own OO code from scratch.</p>
<p>Here's our <code class="inline"><span class="w">File</span></code>
 class once more:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Class::Tiny</span> <span class="q">qw( path content last_mod_time )</span><span class="sc">;</span></li><li></li><li><a name="print_info"></a>  sub <span class="m">print_info</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This file is at &quot;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>That's it!</p>
<p>With <code class="inline"><span class="w">Class::Tiny</span></code>
, all accessors are read-write. It generates a
constructor for you, as well as the accessors you define.</p>
<p>You can also use <a href="http://search.cpan.org/perldoc/Class::Tiny::Antlers">Class::Tiny::Antlers</a> for <code class="inline"><span class="w">Moose</span></code>
-like syntax.</p>
<a name="Role%3a%3aTiny"></a><h2>Role::Tiny</h2>
<p>As we mentioned before, roles provide an alternative to inheritance,
but Perl does not have any built-in role support. If you choose to use
Moose, it comes with a full-fledged role implementation. However, if
you use one of our other recommended OO modules, you can still use
roles with <a href="http://search.cpan.org/perldoc/Role::Tiny">Role::Tiny</a></p>
<p><code class="inline"><span class="w">Role::Tiny</span></code>
 provides some of the same features as Moose's role
system, but in a much smaller package. Most notably, it doesn't support
any sort of attribute declaration, so you have to do that by hand.
Still, it's useful, and works well with <code class="inline"><span class="w">Class::Accessor</span></code>
 and
<code class="inline"><span class="w">Class::Tiny</span></code>
</p>
<a name="OO-System-Summary"></a><h2>OO System Summary</h2>
<p>Here's a brief recap of the options we covered:</p>
<ul>
<li><a name="*-the-Moose-manpage"></a><b><a href="http://search.cpan.org/perldoc/Moose">Moose</a></b>
<p><code class="inline"><span class="w">Moose</span></code>
 is the maximal option. It has a lot of features, a big
ecosystem, and a thriving user base. We also covered <a href="http://search.cpan.org/perldoc/Moo">Moo</a> briefly.
<code class="inline"><span class="w">Moo</span></code>
 is <code class="inline"><span class="w">Moose</span></code>
 lite, and a reasonable alternative when Moose
doesn't work for your application.</p>
</li>
<li><a name="*-the-Class%3a%3aAccessor-manpage"></a><b><a href="http://search.cpan.org/perldoc/Class::Accessor">Class::Accessor</a></b>
<p><code class="inline"><span class="w">Class::Accessor</span></code>
 does a lot less than <code class="inline"><span class="w">Moose</span></code>
, and is a nice
alternative if you find <code class="inline"><span class="w">Moose</span></code>
 overwhelming. It's been around a long
time and is well battle-tested. It also has a minimal <code class="inline"><span class="w">Moose</span></code>

compatibility mode which makes moving from <code class="inline"><span class="w">Class::Accessor</span></code>
 to
<code class="inline"><span class="w">Moose</span></code>
 easy.</p>
</li>
<li><a name="*-the-Class%3a%3aTiny-manpage"></a><b><a href="http://search.cpan.org/perldoc/Class::Tiny">Class::Tiny</a></b>
<p><code class="inline"><span class="w">Class::Tiny</span></code>
 is the absolute minimal option. It has no dependencies,
and almost no syntax to learn. It's a good option for a super minimal
environment and for throwing something together quickly without having
to worry about details.</p>
</li>
<li><a name="*-the-Role%3a%3aTiny-manpage"></a><b><a href="http://search.cpan.org/perldoc/Role::Tiny">Role::Tiny</a></b>
<p>Use <code class="inline"><span class="w">Role::Tiny</span></code>
 with <code class="inline"><span class="w">Class::Accessor</span></code>
 or <code class="inline"><span class="w">Class::Tiny</span></code>
 if you find
yourself considering multiple inheritance. If you go with <code class="inline"><span class="w">Moose</span></code>
, it
comes with its own role implementation.</p>
</li>
</ul>
<a name="Other-OO-Systems"></a><h2>Other OO Systems</h2>
<p>There are literally dozens of other OO-related modules on CPAN besides
those covered here, and you're likely to run across one or more of them
if you work with other people's code.</p>
<p>In addition, plenty of code in the wild does all of its OO "by hand",
using just the Perl built-in OO features. If you need to maintain such
code, you should read <a href="perlobj.html">perlobj</a> to understand exactly how Perl's
built-in OO works.</p>
<a name="CONCLUSION"></a><h1>CONCLUSION</h1>
<p>As we said before, Perl's minimal OO system has led to a profusion of
OO systems on CPAN. While you can still drop down to the bare metal and
write your classes by hand, there's really no reason to do that with
modern Perl.</p>
<p>For small systems, <a href="http://search.cpan.org/perldoc/Class::Tiny">Class::Tiny</a> and <a href="http://search.cpan.org/perldoc/Class::Accessor">Class::Accessor</a> both provide
minimal object systems that take care of basic boilerplate for you.</p>
<p>For bigger projects, <a href="http://search.cpan.org/perldoc/Moose">Moose</a> provides a rich set of features that will
let you focus on implementing your business logic. <a href="http://search.cpan.org/perldoc/Moo">Moo</a> provides a
nice alternative to <a href="http://search.cpan.org/perldoc/Moose">Moose</a> when you want a lot of features but need
faster compile time or to avoid XS.</p>
<p>We encourage you to play with and evaluate <a href="http://search.cpan.org/perldoc/Moose">Moose</a>, <a href="http://search.cpan.org/perldoc/Moo">Moo</a>,
<a href="http://search.cpan.org/perldoc/Class::Accessor">Class::Accessor</a>, and <a href="http://search.cpan.org/perldoc/Class::Tiny">Class::Tiny</a> to see which OO system is right
for you.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DATE">DATE</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OBJECT-ORIENTED-FUNDAMENTALS">OBJECT-ORIENTED FUNDAMENTALS</a><ul><li><a href="#Object">Object</a><li><a href="#Class">Class</a><li><a href="#Methods">Methods</a><li><a href="#Attributes">Attributes</a><li><a href="#Polymorphism">Polymorphism</a><li><a href="#Inheritance">Inheritance</a><li><a href="#Encapsulation">Encapsulation</a><li><a href="#Composition">Composition</a><li><a href="#Roles">Roles</a><li><a href="#When-to-Use-OO">When to Use OO</a></ul><li><a href="#PERL-OO-SYSTEMS">PERL OO SYSTEMS</a><ul><li><a href="#Moose">Moose</a><li><a href="#Class%3a%3aAccessor">Class::Accessor</a><li><a href="#Class%3a%3aTiny">Class::Tiny</a><li><a href="#Role%3a%3aTiny">Role::Tiny</a><li><a href="#OO-System-Summary">OO System Summary</a><li><a href="#Other-OO-Systems">Other OO Systems</a></ul><li><a href="#CONCLUSION">CONCLUSION</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlootut';
  perldoc.pageAddress = 'perlootut.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>