File: perlobj.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 (1216 lines) | stat: -rw-r--r-- 93,689 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
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlobj - 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>perlobj</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-language.html">Language reference</a> &gt;
      
    
    perlobj
  

            </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>perlobj</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME
 </a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#An-Object-is-Simply-a-Data-Structure">An Object is Simply a Data Structure
   </a><li><a href="#A-Class-is-Simply-a-Package">A Class is Simply a Package
   </a><li><a href="#A-Method-is-Simply-a-Subroutine">A Method is Simply a Subroutine
</a><li><a href="#Method-Invocation">Method Invocation
   </a><li><a href="#Inheritance">Inheritance
</a><li><a href="#Writing-Constructors">Writing Constructors
</a><li><a href="#Attributes">Attributes
</a><li><a href="#An-Aside-About-Smarter-and-Safer-Code">An Aside About Smarter and Safer Code</a><li><a href="#Method-Call-Variations">Method Call Variations
</a><li><a href="#Invoking-Class-Methods">Invoking Class Methods
</a><li><a href="#bless%2c-blessed%2c-and-ref">bless, blessed, and ref</a><li><a href="#The-UNIVERSAL-Class">The UNIVERSAL Class
</a><li><a href="#AUTOLOAD">AUTOLOAD
</a><li><a href="#Destructors">Destructors
 </a><li><a href="#Non-Hash-Objects">Non-Hash Objects</a><li><a href="#Inside-Out-objects">Inside-Out objects</a><li><a href="#Pseudo-hashes">Pseudo-hashes</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME
 </h1>
<p>perlobj - Perl object reference</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document provides a reference for Perl's object orientation
features. If you're looking for an introduction to object-oriented
programming in Perl, please see <a href="perlootut.html">perlootut</a>.</p>
<p>In order to understand Perl objects, you first need to understand
references in Perl. See <a href="perlref.html">perlref</a> for details.</p>
<p>This document describes all of Perl's object-oriented (OO) features
from the ground up. If you're just looking to write some
object-oriented code of your own, you are probably better served by
using one of the object systems from CPAN described in <a href="perlootut.html">perlootut</a>.</p>
<p>If you're looking to write your own object system, or you need to
maintain code which implements objects from scratch then this document
will help you understand exactly how Perl does object orientation.</p>
<p>There are a few basic principles which define object oriented Perl:</p>
<dl>
<dt>1.</dt><dd>
<p>An object is simply a data structure that knows to which class it
belongs.</p>
</dd>
<dt>2.</dt><dd>
<p>A class is simply a package. A class provides methods that expect to
operate on objects.</p>
</dd>
<dt>3.</dt><dd>
<p>A method is simply a subroutine that expects a reference to an object
(or a package name, for class methods) as the first argument.</p>
</dd>
</dl>
<p>Let's look at each of these principles in depth.</p>
<a name="An-Object-is-Simply-a-Data-Structure"></a><h2>An Object is Simply a Data Structure
   </h2>
<p>Unlike many other languages which support object orientation, Perl does
not provide any special syntax for constructing an object. Objects are
merely Perl data structures (hashes, arrays, scalars, filehandles,
etc.) that have been explicitly associated with a particular class.</p>
<p>That explicit association is created by the built-in <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> function,
which is typically used within the <i>constructor</i> subroutine of the
class.</p>
<p>Here is a simple constructor:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/return.html">return</a> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="s">}</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>The name <code class="inline"><span class="w">new</span></code>
 isn't special. We could name our constructor something
else:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li></li><li><a name="load"></a>  sub <span class="m">load</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/return.html">return</a> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="s">}</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>The modern convention for OO modules is to always use <code class="inline"><span class="w">new</span></code>
 as the
name for the constructor, but there is no requirement to do so. Any
subroutine that blesses a data structure into a class is a valid
constructor in Perl.</p>
<p>In the previous examples, the <code class="inline"><span class="s">{</span><span class="s">}</span></code>
 code creates a reference to an
empty anonymous hash. The <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> function then takes that reference
and associates the hash with the class in <code class="inline"><span class="i">$class</span></code>
. In the simplest
case, the <code class="inline"><span class="i">$class</span></code>
 variable will end up containing the string "File".</p>
<p>We can also use a variable to store a reference to the data structure
that is being blessed as our object:</p>
<pre class="verbatim"><ol><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/my.html">my</a> <span class="i">$self</span> = <span class="s">{</span><span class="s">}</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/bless.html">bless</a> <span class="i">$self</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Once we've blessed the hash referred to by <code class="inline"><span class="i">$self</span></code>
 we can start
calling methods on it. This is useful if you want to put object
initialization in its own separate method:</p>
<pre class="verbatim"><ol><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/my.html">my</a> <span class="i">$self</span> = <span class="s">{</span><span class="s">}</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/bless.html">bless</a> <span class="i">$self</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li></li><li>      <span class="i">$self</span><span class="i">-&gt;_initialize</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Since the object is also a hash, you can treat it as one, using it to
store data associated with the object. Typically, code inside the class
can treat the hash as an accessible data structure, while code outside
the class should always treat the object as opaque. This is called
<b>encapsulation</b>. Encapsulation means that the user of an object does
not have to know how it is implemented. The user simply calls
documented methods on the object.</p>
<p>Note, however, that (unlike most other OO languages) Perl does not
ensure or enforce encapsulation in any way. If you want objects to
actually <i>be</i> opaque you need to arrange for that yourself. This can
be done in a variety of ways, including using <a href="#Inside-Out-objects">Inside-Out objects</a>
or modules from CPAN.</p>
<a name="Objects-Are-Blessed%3b-Variables-Are-Not"></a><h3>Objects Are Blessed; Variables Are Not</h3>
<p>When we bless something, we are not blessing the variable which
contains a reference to that thing, nor are we blessing the reference
that the variable stores; we are blessing the thing that the variable
refers to (sometimes known as the <i>referent</i>). This is best
demonstrated with this code:</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/my.html">my</a> <span class="i">$foo</span> = <span class="s">{</span><span class="s">}</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$bar</span> = <span class="i">$foo</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/bless.html">bless</a> <span class="i">$foo</span><span class="cm">,</span> <span class="q">&#39;Class&#39;</span><span class="sc">;</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">$bar</span> <span class="s">)</span> // <span class="q">&#39;not blessed&#39;</span><span class="sc">;</span>    <span class="c"># prints &quot;Class&quot;</span></li><li></li><li>  <span class="i">$bar</span> = <span class="q">&quot;some other value&quot;</span><span class="sc">;</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">$bar</span> <span class="s">)</span> // <span class="q">&#39;not blessed&#39;</span><span class="sc">;</span>    <span class="c"># prints &quot;not blessed&quot;</span></li></ol></pre><p>When we call <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> on a variable, we are actually blessing the
underlying data structure that the variable refers to. We are not
blessing the reference itself, nor the variable that contains that
reference. That's why the second call to <code class="inline"><span class="i">blessed</span><span class="s">(</span> <span class="i">$bar</span> <span class="s">)</span></code>
 returns
false. At that point <code class="inline"><span class="i">$bar</span></code>
 is no longer storing a reference to an
object.</p>
<p>You will sometimes see older books or documentation mention "blessing a
reference" or describe an object as a "blessed reference", but this is
incorrect. It isn't the reference that is blessed as an object; it's
the thing the reference refers to (i.e. the referent).</p>
<a name="A-Class-is-Simply-a-Package"></a><h2>A Class is Simply a Package
   </h2>
<p>Perl does not provide any special syntax for class definitions. A
package is simply a namespace containing variables and subroutines. The
only difference is that in a class, the subroutines may expect a
reference to an object or the name of a class as the first argument.
This is purely a matter of convention, so a class may contain both
methods and subroutines which <i>don't</i> operate on an object or class.</p>
<p>Each package contains a special array called <code class="inline"><span class="i">@ISA</span></code>
. The <code class="inline"><span class="i">@ISA</span></code>
 array
contains a list of that class's parent classes, if any. This array is
examined when Perl does method resolution, which we will cover later.</p>
<p>Calling methods from a package means it must be loaded, of course, so
you will often want to load a module and add it to <code class="inline"><span class="i">@ISA</span></code>
 at the same
time. You can do so in a single step using the <a href="parent.html">parent</a> pragma.
(In older code you may encounter the <a href="base.html">base</a> pragma, which is nowadays
discouraged except when you have to work with the equally discouraged
<a href="fields.html">fields</a> pragma.)</p>
<p>However the parent classes are set, the package's <code class="inline"><span class="i">@ISA</span></code>
 variable will
contain a list of those parents. This is simply a list of scalars, each
of which is a string that corresponds to a package name.</p>
<p>All classes inherit from the <a href="UNIVERSAL.html">UNIVERSAL</a> class implicitly. The
<a href="UNIVERSAL.html">UNIVERSAL</a> class is implemented by the Perl core, and provides
several default methods, such as <code class="inline"><span class="i">isa</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><span class="i">can</span><span class="s">(</span><span class="s">)</span></code>
, and <code class="inline"><span class="i">VERSION</span><span class="s">(</span><span class="s">)</span></code>
.
The <code class="inline"><span class="w">UNIVERSAL</span></code>
 class will <i>never</i> appear in a package's <code class="inline"><span class="i">@ISA</span></code>

variable.</p>
<p>Perl <i>only</i> provides method inheritance as a built-in feature.
Attribute inheritance is left up the class to implement. See the
<a href="#Writing-Accessors">Writing Accessors</a> section for details.</p>
<a name="A-Method-is-Simply-a-Subroutine"></a><h2>A Method is Simply a Subroutine
</h2>
<p>Perl does not provide any special syntax for defining a method. A
method is simply a regular subroutine, and is declared with <code class="inline"><a class="l_k" href="functions/sub.html">sub</a></code>.
What makes a method special is that it expects to receive either an
object or a class name as its first argument.</p>
<p>Perl <i>does</i> provide special syntax for method invocation, the <code class="inline">-&gt;</code>
 operator. We will cover this in more detail later.</p>
<p>Most methods you write will expect to operate on objects:</p>
<pre class="verbatim"><ol><li><a name="save"></a>  sub <span class="m">save</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/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="i">$self</span><span class="i">-&gt;path</span><span class="s">(</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/print.html">print</a> <span class="s">{</span><span class="i">$fh</span><span class="s">}</span> <span class="i">$self</span><span class="i">-&gt;data</span><span class="s">(</span><span class="s">)</span>       <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/close.html">close</a> <span class="i">$fh</span>                       <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><a name="Method-Invocation"></a><h2>Method Invocation
   </h2>
<p>Calling a method on an object is written as <code class="inline"><span class="i">$object</span><span class="i">-&gt;method</span></code>
.</p>
<p>The left hand side of the method invocation (or arrow) operator is the
object (or class name), and the right hand side is the method name.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$pod</span> = <span class="w">File</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="q">&#39;perlobj.pod&#39;</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$pod</span><span class="i">-&gt;save</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline">-&gt;</code>
 syntax is also used when dereferencing a reference. It
looks like the same operator, but these are two different operations.</p>
<p>When you call a method, the thing on the left side of the arrow is
passed as the first argument to the method. That means when we call <code class="inline"><span class="w">Critter</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
, the <code class="inline"><span class="i">new</span><span class="s">(</span><span class="s">)</span></code>
 method receives the string <code class="inline"><span class="q">&quot;Critter&quot;</span></code>

as its first argument. When we call <code class="inline"><span class="i">$fred</span><span class="i">-&gt;speak</span><span class="s">(</span><span class="s">)</span></code>
, the <code class="inline"><span class="i">$fred</span></code>

variable is passed as the first argument to <code class="inline"><span class="i">speak</span><span class="s">(</span><span class="s">)</span></code>
.</p>
<p>Just as with any Perl subroutine, all of the arguments passed in <code class="inline"><span class="i">@_</span></code>

are aliases to the original argument. This includes the object itself.
If you assign directly to <code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
 you will change the contents of the
variable that holds the reference to the object. We recommend that you
don't do this unless you know exactly what you're doing.</p>
<p>Perl knows what package the method is in by looking at the left side of
the arrow. If the left hand side is a package name, it looks for the
method in that package. If the left hand side is an object, then Perl
looks for the method in the package that the object has been blessed
into.</p>
<p>If the left hand side is neither a package name nor an object, then the
method call will cause an error, but see the section on <a href="#Method-Call-Variations">Method Call Variations</a> for more nuances.</p>
<a name="Inheritance"></a><h2>Inheritance
</h2>
<p>We already talked about the special <code class="inline"><span class="i">@ISA</span></code>
 array and the <a href="parent.html">parent</a>
pragma.</p>
<p>When a class inherits from another class, any methods defined in the
parent class are available to the child class. If you attempt to call a
method on an object that isn't defined in its own class, Perl will also
look for that method in any parent classes it may have.</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>  <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>    <span class="c"># sets @File::MP3::ISA = (&#39;File&#39;);</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$mp3</span> = <span class="w">File::MP3</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="q">&#39;Andvari.mp3&#39;</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$mp3</span><span class="i">-&gt;save</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Since we didn't define a <code class="inline"><span class="i">save</span><span class="s">(</span><span class="s">)</span></code>
 method in the <code class="inline"><span class="w">File::MP3</span></code>
 class,
Perl will look at the <code class="inline"><span class="w">File::MP3</span></code>
 class's parent classes to find the
<code class="inline"><span class="i">save</span><span class="s">(</span><span class="s">)</span></code>
 method. If Perl cannot find a <code class="inline"><span class="i">save</span><span class="s">(</span><span class="s">)</span></code>
 method anywhere in
the inheritance hierarchy, it will die.</p>
<p>In this case, it finds a <code class="inline"><span class="i">save</span><span class="s">(</span><span class="s">)</span></code>
 method in the <code class="inline"><span class="w">File</span></code>
 class. Note
that the object passed to <code class="inline"><span class="i">save</span><span class="s">(</span><span class="s">)</span></code>
 in this case is still a
<code class="inline"><span class="w">File::MP3</span></code>
 object, even though the method is found in the <code class="inline"><span class="w">File</span></code>

class.</p>
<p>We can override a parent's method in a child class. When we do so, we
can still call the parent class's method with the <code class="inline"><span class="w">SUPER</span></code>

pseudo-class.</p>
<pre class="verbatim"><ol><li><a name="save"></a>  sub <span class="m">save</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/say.html">say</a> <span class="q">&#39;Prepare to rock&#39;</span><span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;SUPER::save</span><span class="s">(</span><span class="s">)</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>
 modifier can <i>only</i> be used for method calls. You can't
use it for regular subroutine calls or class methods:</p>
<pre class="verbatim"><ol><li>  <span class="i">SUPER::save</span><span class="s">(</span><span class="i">$thing</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># FAIL: looks for save() sub in package SUPER</span></li><li></li><li>  <span class="w">SUPER</span><span class="w">-&gt;save</span><span class="s">(</span><span class="i">$thing</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># FAIL: looks for save() method in class</span></li><li>                           <span class="c">#       SUPER</span></li><li></li><li>  <span class="i">$thing</span><span class="i">-&gt;SUPER::save</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># Okay: looks for save() method in parent</span></li><li>                           <span class="c">#       classes</span></li></ol></pre><a name="How-SUPER-is-Resolved"></a><h3>How SUPER is Resolved
</h3>
<p>The <code class="inline"><span class="w">SUPER</span></code>
 pseudo-class is resolved from the package where the call
is made. It is <i>not</i> resolved based on the object's class. This is
important, because it lets methods at different levels within a deep
inheritance hierarchy each correctly call their respective parent
methods.</p>
<pre class="verbatim"><ol><li><a name="package-A"></a>  package <span class="i">A</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="s">}</span><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="speak"></a>  sub <span class="m">speak</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/say.html">say</a> <span class="q">&#39;A&#39;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="package-B"></a>  package <span class="i">B</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="w">norequire</span><span class="cm">,</span> <span class="q">&#39;A&#39;</span><span class="sc">;</span></li><li></li><li><a name="speak-1"></a>  sub <span class="m">speak</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::speak</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;B&#39;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="package-C"></a>  package <span class="i">C</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="w">norequire</span><span class="cm">,</span> <span class="q">&#39;B&#39;</span><span class="sc">;</span></li><li></li><li><a name="speak-2"></a>  sub <span class="m">speak</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::speak</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;C&#39;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <span class="w">C</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$c</span><span class="i">-&gt;speak</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In this example, we will get the following output:</p>
<pre class="verbatim"><ol><li>  <span class="w">A</span></li><li>  <span class="w">B</span></li><li>  <span class="w">C</span></li></ol></pre><p>This demonstrates how <code class="inline"><span class="w">SUPER</span></code>
 is resolved. Even though the object is
blessed into the <code class="inline"><span class="w">C</span></code>
 class, the <code class="inline"><span class="i">speak</span><span class="s">(</span><span class="s">)</span></code>
 method in the <code class="inline"><span class="w">B</span></code>
 class
can still call <code class="inline"><span class="i">SUPER::speak</span><span class="s">(</span><span class="s">)</span></code>
 and expect it to correctly look in the
parent class of <code class="inline"><span class="w">B</span></code>
 (i.e the class the method call is in), not in the
parent class of <code class="inline"><span class="w">C</span></code>
 (i.e. the class the object belongs to).</p>
<p>There are rare cases where this package-based resolution can be a
problem. If you copy a subroutine from one package to another, <code class="inline"><span class="w">SUPER</span></code>

resolution will be done based on the original package.</p>
<a name="Multiple-Inheritance"></a><h3>Multiple Inheritance
</h3>
<p>Multiple inheritance often indicates a design problem, but Perl always
gives you enough rope to hang yourself with if you ask for it.</p>
<p>To declare multiple parents, you simply need to pass multiple class
names to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">parent</span></code>
:</p>
<pre class="verbatim"><ol><li><a name="package-MultiChild"></a>  package <span class="i">MultiChild</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;Parent1&#39;</span><span class="cm">,</span> <span class="q">&#39;Parent2&#39;</span><span class="sc">;</span></li></ol></pre><a name="Method-Resolution-Order"></a><h3>Method Resolution Order
 </h3>
<p>Method resolution order only matters in the case of multiple
inheritance. In the case of single inheritance, Perl simply looks up
the inheritance chain to find a method:</p>
<pre class="verbatim"><ol><li>  <span class="w">Grandparent</span></li><li>    |</li><li>  <span class="w">Parent</span></li><li>    |</li><li>  <span class="w">Child</span></li></ol></pre><p>If we call a method on a <code class="inline"><span class="w">Child</span></code>
 object and that method is not defined
in the <code class="inline"><span class="w">Child</span></code>
 class, Perl will look for that method in the <code class="inline"><span class="w">Parent</span></code>

class and then, if necessary, in the <code class="inline"><span class="w">Grandparent</span></code>
 class.</p>
<p>If Perl cannot find the method in any of these classes, it will die
with an error message.</p>
<p>When a class has multiple parents, the method lookup order becomes more
complicated.</p>
<p>By default, Perl does a depth-first left-to-right search for a method.
That means it starts with the first parent in the <code class="inline"><span class="i">@ISA</span></code>
 array, and
then searches all of its parents, grandparents, etc. If it fails to
find the method, it then goes to the next parent in the original
class's <code class="inline"><span class="i">@ISA</span></code>
 array and searches from there.</p>
<pre class="verbatim"><ol><li>            <span class="w">SharedGreatGrandParent</span></li><li>            <span class="q">/                    \</span></li><li>  <span class="q">  PaternalGrandparent       MaternalGrandparent</span></li><li>            <span class="q">            \                    /</span></li><li>             <span class="w">Father</span>        <span class="w">Mother</span></li><li>                   \      <span class="q">/</span></li><li>                    <span class="q">                    Child</span></li></ol></pre><p>So given the diagram above, Perl will search <code class="inline"><span class="w">Child</span></code>
, <code class="inline"><span class="w">Father</span></code>
,
<code class="inline"><span class="w">PaternalGrandparent</span></code>
, <code class="inline"><span class="w">SharedGreatGrandParent</span></code>
, <code class="inline"><span class="w">Mother</span></code>
, and
finally <code class="inline"><span class="w">MaternalGrandparent</span></code>
. This may be a problem because now we're
looking in <code class="inline"><span class="w">SharedGreatGrandParent</span></code>
 <i>before</i> we've checked all its
derived classes (i.e. before we tried <code class="inline"><span class="w">Mother</span></code>
 and
<code class="inline"><span class="w">MaternalGrandparent</span></code>
).</p>
<p>It is possible to ask for a different method resolution order with the
<a href="mro.html">mro</a> pragma.</p>
<pre class="verbatim"><ol><li><a name="package-Child"></a>  package <span class="i">Child</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">mro</span> <span class="q">&#39;c3&#39;</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">parent</span> <span class="q">&#39;Father&#39;</span><span class="cm">,</span> <span class="q">&#39;Mother&#39;</span><span class="sc">;</span></li></ol></pre><p>This pragma lets you switch to the "C3" resolution order. In simple
terms, "C3" order ensures that shared parent classes are never searched
before child classes, so Perl will now search: <code class="inline"><span class="w">Child</span></code>
, <code class="inline"><span class="w">Father</span></code>
,
<code class="inline"><span class="w">PaternalGrandparent</span></code>
, <code class="inline"><span class="w">Mother</span></code>
 <code class="inline"><span class="w">MaternalGrandparent</span></code>
, and finally
<code class="inline"><span class="w">SharedGreatGrandParent</span></code>
. Note however that this is not
"breadth-first" searching: All the <code class="inline"><span class="w">Father</span></code>
 ancestors (except the
common ancestor) are searched before any of the <code class="inline"><span class="w">Mother</span></code>
 ancestors are
considered.</p>
<p>The C3 order also lets you call methods in sibling classes with the
<code class="inline"><a class="l_k" href="functions/next.html">next</a></code> pseudo-class. See the <a href="mro.html">mro</a> documentation for more details on
this feature.</p>
<a name="Method-Resolution-Caching"></a><h3>Method Resolution Caching</h3>
<p>When Perl searches for a method, it caches the lookup so that future
calls to the method do not need to search for it again. Changing a
class's parent class or adding subroutines to a class will invalidate
the cache for that class.</p>
<p>The <a href="mro.html">mro</a> pragma provides some functions for manipulating the method
cache directly.</p>
<a name="Writing-Constructors"></a><h2>Writing Constructors
</h2>
<p>As we mentioned earlier, Perl provides no special constructor syntax.
This means that a class must implement its own constructor. A
constructor is simply a class method that returns a reference to a new
object.</p>
<p>The constructor can also accept additional parameters that define the
object. Let's write a real constructor for the <code class="inline"><span class="w">File</span></code>
 class we used
earlier:</p>
<pre class="verbatim"><ol><li><a name="package-File"></a>  package <span class="i">File</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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="s">(</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span></li><li>          <span class="w">path</span> <span class="cm">=&gt;</span> <span class="i">$path</span><span class="cm">,</span></li><li>          <span class="w">data</span> <span class="cm">=&gt;</span> <span class="i">$data</span><span class="cm">,</span></li><li>      <span class="s">}</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>As you can see, we've stored the path and file data in the object
itself. Remember, under the hood, this object is still just a hash.
Later, we'll write accessors to manipulate this data.</p>
<p>For our <code class="inline"><span class="w">File::MP3</span></code>
 class, we can check to make sure that the path
we're given ends with ".mp3":</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 name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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="s">(</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;You cannot create a File::MP3 without an mp3 extension\n&quot;</span></li><li>          <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$path</span> =~ <span class="q">/\.mp3\z/</span><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$class</span><span class="i">-&gt;SUPER::new</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>This constructor lets its parent class do the actual object
construction.</p>
<a name="Attributes"></a><h2>Attributes
</h2>
<p>An attribute is a piece of data belonging to a particular object.
Unlike most object-oriented languages, Perl provides no special syntax
or support for declaring and manipulating attributes.</p>
<p>Attributes are often stored in the object itself. For example, if the
object is an anonymous hash, we can store the attribute values in the
hash using the attribute name as the key.</p>
<p>While it's possible to refer directly to these hash keys outside of the
class, it's considered a best practice to wrap all access to the
attribute with accessor methods.</p>
<p>This has several advantages. Accessors make it easier to change the
implementation of an object later while still preserving the original
API.</p>
<p>An accessor lets you add additional code around attribute access. For
example, you could apply a default to an attribute that wasn't set in
the constructor, or you could validate that a new value for the
attribute is acceptable.</p>
<p>Finally, using accessors makes inheritance much simpler. Subclasses can
use the accessors rather than having to know how a parent class is
implemented internally.</p>
<a name="Writing-Accessors"></a><h3>Writing Accessors
</h3>
<p>As with constructors, Perl provides no special accessor declaration
syntax, so classes must provide explicitly written accessor methods.
There are two common types of accessors, read-only and read-write.</p>
<p>A simple read-only accessor simply gets the value of a single
attribute:</p>
<pre class="verbatim"><ol><li><a name="path"></a>  sub <span class="m">path</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/return.html">return</a> <span class="i">$self</span>-&gt;{<span class="w">path</span>}<span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>A read-write accessor will allow the caller to set the value as well as
get it:</p>
<pre class="verbatim"><ol><li><a name="path"></a>  sub <span class="m">path</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/if.html">if</a> <span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span>-&gt;{<span class="w">path</span>} = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <span class="s">}</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span>-&gt;{<span class="w">path</span>}<span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><a name="An-Aside-About-Smarter-and-Safer-Code"></a><h2>An Aside About Smarter and Safer Code</h2>
<p>Our constructor and accessors are not very smart. They don't check that
a <code class="inline"><span class="i">$path</span></code>
 is defined, nor do they check that a <code class="inline"><span class="i">$path</span></code>
 is a valid
filesystem path.</p>
<p>Doing these checks by hand can quickly become tedious. Writing a bunch
of accessors by hand is also incredibly tedious. There are a lot of
modules on CPAN that can help you write safer and more concise code,
including the modules we recommend in <a href="perlootut.html">perlootut</a>.</p>
<a name="Method-Call-Variations"></a><h2>Method Call Variations
</h2>
<p>Perl supports several other ways to call methods besides the <code class="inline"><span class="i">$object</span><span class="i">-&gt;method</span><span class="s">(</span><span class="s">)</span></code>
 usage we've seen so far.</p>
<a name="Method-Names-with-a-Fully-Qualified-Name"></a><h3>Method Names with a Fully Qualified Name</h3>
<p>Perl allows you to call methods using their fully qualified name (the
package and method name):</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$mp3</span> = <span class="w">File::MP3</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="q">&#39;Regin.mp3&#39;</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$mp3</span><span class="i">-&gt;File::save</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When you a fully qualified method name like <code class="inline"><span class="w">File::save</span></code>
, the method
resolution search for the <code class="inline"><span class="w">save</span></code>
 method starts in the <code class="inline"><span class="w">File</span></code>
 class,
skipping any <code class="inline"><span class="w">save</span></code>
 method the <code class="inline"><span class="w">File::MP3</span></code>
 class may have defined. It
still searches the <code class="inline"><span class="w">File</span></code>
 class's parents if necessary.</p>
<p>While this feature is most commonly used to explicitly call methods
inherited from an ancestor class, there is no technical restriction
that enforces this:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$obj</span> = <span class="w">Tree</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$obj</span><span class="i">-&gt;Dog::bark</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This calls the <code class="inline"><span class="w">bark</span></code>
 method from class <code class="inline"><span class="w">Dog</span></code>
 on an object of class
<code class="inline"><span class="w">Tree</span></code>
, even if the two classes are completely unrelated. Use this
with great care.</p>
<p>The <code class="inline"><span class="w">SUPER</span></code>
 pseudo-class that was described earlier is <i>not</i> the same
as calling a method with a fully-qualified name. See the earlier
<a href="#Inheritance">Inheritance</a> section for details.</p>
<a name="Method-Names-as-Strings"></a><h3>Method Names as Strings</h3>
<p>Perl lets you use a scalar variable containing a string as a method
name:</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="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$method</span> = <span class="q">&#39;save&#39;</span><span class="sc">;</span></li><li>  <span class="i">$file</span><span class="i">-&gt;$method</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This works exactly like calling <code class="inline"><span class="i">$file</span><span class="i">-&gt;save</span><span class="s">(</span><span class="s">)</span></code>
. This can be very
useful for writing dynamic code. For example, it allows you to pass a
method name to be called as a parameter to another method.</p>
<a name="Class-Names-as-Strings"></a><h3>Class Names as Strings</h3>
<p>Perl also lets you use a scalar containing a string as a class name:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</span> = <span class="q">&#39;File&#39;</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="i">$class</span><span class="i">-&gt;new</span><span class="s">(</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Again, this allows for very dynamic code.</p>
<a name="Subroutine-References-as-Methods"></a><h3>Subroutine References as Methods</h3>
<p>You can also use a subroutine reference as a method:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$sub</span> = <a class="l_k" href="functions/sub.html">sub</a> <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;save</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span><span class="sc">;</span></li><li></li><li>  <span class="i">$file</span><span class="i">-&gt;$sub</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This is exactly equivalent to writing <code class="inline"><span class="i">$sub</span>-&gt;<span class="s">(</span><span class="i">$file</span><span class="s">)</span></code>
. You may see
this idiom in the wild combined with a call to <code class="inline"><span class="w">can</span></code>
:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$meth</span> = <span class="i">$object</span><span class="i">-&gt;can</span><span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>      <span class="i">$object</span><span class="i">-&gt;$meth</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><a name="Dereferencing-Method-Call"></a><h3>Dereferencing Method Call</h3>
<p>Perl also lets you use a dereferenced scalar reference in a method
call. That's a mouthful, so let's look at some code:</p>
<pre class="verbatim"><ol><li>  <span class="i">$file</span><span class="i">-&gt;$</span>{ \<span class="q">&#39;save&#39;</span> }<span class="sc">;</span></li><li>  <span class="i">$file</span><span class="i">-&gt;$</span>{ <span class="i">returns_scalar_ref</span><span class="s">(</span><span class="s">)</span> }<span class="sc">;</span></li><li>  <span class="i">$file</span><span class="i">-&gt;$</span>{ \<span class="s">(</span> <span class="i">returns_scalar</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> }<span class="sc">;</span></li><li>  <span class="i">$file</span><span class="i">-&gt;$</span>{ <span class="i">returns_ref_to_sub_ref</span><span class="s">(</span><span class="s">)</span> }<span class="sc">;</span></li></ol></pre><p>This works if the dereference produces a string <i>or</i> a subroutine
reference.</p>
<a name="Method-Calls-on-Filehandles"></a><h3>Method Calls on Filehandles</h3>
<p>Under the hood, Perl filehandles are instances of the <code class="inline"><span class="w">IO::Handle</span></code>
 or
<code class="inline"><span class="w">IO::File</span></code>
 class. Once you have an open filehandle, you can call
methods on it. Additionally, you can call methods on the <code class="inline"><span class="w">STDIN</span></code>
,
<code class="inline"><span class="w">STDOUT</span></code>
, and <code class="inline"><span class="w">STDERR</span></code>
 filehandles.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&#39;path/to/file&#39;</span><span class="sc">;</span></li><li>  <span class="i">$fh</span><span class="i">-&gt;autoflush</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="i">$fh</span><span class="i">-&gt;print</span><span class="s">(</span><span class="q">&#39;content&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>  <span class="w">STDOUT</span><span class="w">-&gt;autoflush</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Invoking-Class-Methods"></a><h2>Invoking Class Methods
</h2>
<p>Because Perl allows you to use barewords for package names and
subroutine names, it sometimes interprets a bareword's meaning
incorrectly. For example, the construct <code class="inline"><span class="w">Class</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
 can be
interpreted as either <code class="inline"><span class="q">&#39;Class&#39;</span><span class="i">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
 or <code class="inline"><span class="i">Class</span><span class="s">(</span><span class="s">)</span><span class="i">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
.
In English, that second interpretation reads as "call a subroutine
named Class(), then call new() as a method on the return value of
Class()". If there is a subroutine named <code class="inline"><span class="i">Class</span><span class="s">(</span><span class="s">)</span></code>
 in the current
namespace, Perl will always interpret <code class="inline"><span class="w">Class</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
 as the second
alternative: a call to <code class="inline"><span class="i">new</span><span class="s">(</span><span class="s">)</span></code>
 on the object  returned by a call to
<code class="inline"><span class="i">Class</span><span class="s">(</span><span class="s">)</span></code>
</p>
<p>You can force Perl to use the first interpretation (i.e. as a method
call on the class named "Class") in two ways. First, you can append a
<code class="inline"><span class="w">::</span></code>
 to the class name:</p>
<pre class="verbatim"><ol><li>    <span class="i">Class::-&gt;new</span><span class="s">(</span><span class="s">)</span></li></ol></pre><p>Perl will always interpret this as a method call.</p>
<p>Alternatively, you can quote the class name:</p>
<pre class="verbatim"><ol><li>    <span class="q">&#39;Class&#39;</span><span class="i">-&gt;new</span><span class="s">(</span><span class="s">)</span></li></ol></pre><p>Of course, if the class name is in a scalar Perl will do the right
thing as well:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</span> = <span class="q">&#39;Class&#39;</span><span class="sc">;</span></li><li>    <span class="i">$class</span><span class="i">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Indirect-Object-Syntax"></a><h3>Indirect Object Syntax
</h3>
<p><b>Outside of the file handle case, use of this syntax is discouraged as
it can confuse the Perl interpreter. See below for more details.</b></p>
<p>Perl supports another method invocation syntax called "indirect object"
notation. This syntax is called "indirect" because the method comes
before the object it is being invoked on.</p>
<p>This syntax can be used with any class or object method:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="w">new</span> <span class="w">File</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span><span class="sc">;</span></li><li>    <span class="w">save</span> <span class="i">$file</span><span class="sc">;</span></li></ol></pre><p>We recommend that you avoid this syntax, for several reasons.</p>
<p>First, it can be confusing to read. In the above example, it's not
clear if <code class="inline"><span class="w">save</span></code>
 is a method provided by the <code class="inline"><span class="w">File</span></code>
 class or simply a
subroutine that expects a file object as its first argument.</p>
<p>When used with class methods, the problem is even worse. Because Perl
allows subroutine names to be written as barewords, Perl has to guess
whether the bareword after the method is a class name or subroutine
name. In other words, Perl can resolve the syntax as either <code class="inline"><span class="w">File</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span></code>
 <b>or</b> <code class="inline"><span class="i">new</span><span class="s">(</span> <span class="i">File</span><span class="s">(</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span> <span class="s">)</span> <span class="s">)</span></code>
.</p>
<p>To parse this code, Perl uses a heuristic based on what package names
it has seen, what subroutines exist in the current package, what
barewords it has previously seen, and other input. Needless to say,
heuristics can produce very surprising results!</p>
<p>Older documentation (and some CPAN modules) encouraged this syntax,
particularly for constructors, so you may still find it in the wild.
However, we encourage you to avoid using it in new code.</p>
<p>You can force Perl to interpret the bareword as a class name by
appending "::" to it, like we saw earlier:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="w">new</span> <span class="w">File::</span> <span class="i">$path</span><span class="cm">,</span> <span class="i">$data</span><span class="sc">;</span></li></ol></pre><a name="bless%2c-blessed%2c-and-ref"></a><h2><code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code>, <code class="inline"><span class="w">blessed</span></code>
, and <code class="inline"><a class="l_k" href="functions/ref.html">ref</a></code></h2>
<p>As we saw earlier, an object is simply a data structure that has been
blessed into a class via the <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> function. The <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code> function
can take either one or two arguments:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$object</span> = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="s">}</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$object</span> = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="s">}</span><span class="sc">;</span></li></ol></pre><p>In the first form, the anonymous hash is being blessed into the class
in <code class="inline"><span class="i">$class</span></code>
. In the second form, the anonymous hash is blessed into
the current package.</p>
<p>The second form is strongly discouraged, because it breaks the ability
of a subclass to reuse the parent's constructor, but you may still run
across it in existing code.</p>
<p>If you want to know whether a particular scalar refers to an object,
you can use the <code class="inline"><span class="w">blessed</span></code>
 function exported by <a href="Scalar/Util.html">Scalar::Util</a>, which
is shipped with the Perl core.</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/if.html">if</a> <span class="s">(</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">blessed</span><span class="s">(</span><span class="i">$thing</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>If <code class="inline"><span class="i">$thing</span></code>
 refers to an object, then this function returns the name
of the package the object has been blessed into. If <code class="inline"><span class="i">$thing</span></code>
 doesn't
contain a reference to a blessed object, the <code class="inline"><span class="w">blessed</span></code>
 function
returns <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.</p>
<p>Note that <code class="inline"><span class="i">blessed</span><span class="s">(</span><span class="i">$thing</span><span class="s">)</span></code>
 will also return false if <code class="inline"><span class="i">$thing</span></code>
 has
been blessed into a class named "0". This is a possible, but quite
pathological. Don't create a class named "0" unless you know what
you're doing.</p>
<p>Similarly, Perl's built-in <code class="inline"><a class="l_k" href="functions/ref.html">ref</a></code> function treats a reference to a
blessed object specially. If you call <code class="inline"><a class="l_k" href="functions/ref.html">ref($thing)</a></code> and <code class="inline"><span class="i">$thing</span></code>

holds a reference to an object, it will return the name of the class
that the object has been blessed into.</p>
<p>If you simply want to check that a variable contains an object
reference, we recommend that you use <code class="inline"><a class="l_k" href="functions/defined.html">defined</a> <span class="i">blessed</span><span class="s">(</span><span class="i">$object</span><span class="s">)</span></code>
, since
<code class="inline"><a class="l_k" href="functions/ref.html">ref</a></code> returns true values for all references, not just objects.</p>
<a name="The-UNIVERSAL-Class"></a><h2>The UNIVERSAL Class
</h2>
<p>All classes automatically inherit from the <a href="UNIVERSAL.html">UNIVERSAL</a> class, which is
built-in to the Perl core. This class provides a number of methods, all
of which can be called on either a class or an object. You can also
choose to override some of these methods in your class. If you do so,
we recommend that you follow the built-in semantics described below.</p>
<ul>
<li><a name="isa(%24class)"></a><b>isa($class)
</b>
<p>The <code class="inline"><span class="w">isa</span></code>
 method returns <i>true</i> if the object is a member of the
class in <code class="inline"><span class="i">$class</span></code>
, or a member of a subclass of <code class="inline"><span class="i">$class</span></code>
.</p>
<p>If you override this method, it should never throw an exception.</p>
</li>
<li><a name="DOES(%24role)"></a><b>DOES($role)
</b>
<p>The <code class="inline"><span class="w">DOES</span></code>
 method returns <i>true</i> if its object claims to perform the
role <code class="inline"><span class="i">$role</span></code>
. By default, this is equivalent to <code class="inline"><span class="w">isa</span></code>
. This method is
provided for use by object system extensions that implement roles, like
<code class="inline"><span class="w">Moose</span></code>
 and <code class="inline"><span class="w">Role::Tiny</span></code>
.</p>
<p>You can also override <code class="inline"><span class="w">DOES</span></code>
 directly in your own classes. If you
override this method, it should never throw an exception.</p>
</li>
<li><a name="can(%24method)"></a><b>can($method)
</b>
<p>The <code class="inline"><span class="w">can</span></code>
 method checks to see if the class or object it was called on
has a method named <code class="inline"><span class="i">$method</span></code>
. This checks for the method in the class
and all of its parents. If the method exists, then a reference to the
subroutine is returned. If it does not then <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned.</p>
<p>If your class responds to method calls via <code class="inline">AUTOLOAD</code>
, you may want to
overload <code class="inline"><span class="w">can</span></code>
 to return a subroutine reference for methods which your
<code class="inline">AUTOLOAD</code>
 method handles.</p>
<p>If you override this method, it should never throw an exception.</p>
</li>
<li><a name="VERSION(%24need)"></a><b>VERSION($need)
</b>
<p>The <code class="inline"><span class="w">VERSION</span></code>
 method returns the version number of the class
(package).</p>
<p>If the <code class="inline"><span class="i">$need</span></code>
 argument is given then it will check that the current
version (as defined by the $VERSION variable in the package) is greater
than or equal to <code class="inline"><span class="i">$need</span></code>
; it will die if this is not the case. This
method is called automatically by the <code class="inline"><span class="w">VERSION</span></code>
 form of <code class="inline"><a class="l_k" href="functions/use.html">use</a></code>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Package</span> <span class="n">1.2</span> <span class="q">qw(some imported subs)</span><span class="sc">;</span></li><li>    <span class="c"># implies:</span></li><li>    <span class="w">Package</span><span class="w">-&gt;VERSION</span><span class="s">(</span><span class="n">1.2</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>We recommend that you use this method to access another package's
version, rather than looking directly at <code class="inline"><span class="i">$Package::VERSION</span></code>
. The
package you are looking at could have overridden the <code class="inline"><span class="w">VERSION</span></code>
 method.</p>
<p>We also recommend using this method to check whether a module has a
sufficient version. The internal implementation uses the <a href="version.html">version</a>
module to make sure that different types of version numbers are
compared correctly.</p>
</li>
</ul>
<a name="AUTOLOAD"></a><h2>AUTOLOAD
</h2>
<p>If you call a method that doesn't exist in a class, Perl will throw an
error. However, if that class or any of its parent classes defines an
<code class="inline">AUTOLOAD</code>
 method, that <code class="inline">AUTOLOAD</code>
 method is called instead.</p>
<p><code class="inline">AUTOLOAD</code>
 is called as a regular method, and the caller will not know
the difference. Whatever value your <code class="inline">AUTOLOAD</code>
 method returns is
returned to the caller.</p>
<p>The fully qualified method name that was called is available in the
<code class="inline"><span class="i">$AUTOLOAD</span></code>
 package global for your class. Since this is a global, if
you want to refer to do it without a package name prefix under <code class="inline"><span class="w">strict</span>
<span class="q">&#39;vars&#39;</span></code>
, you need to declare it.</p>
<pre class="verbatim"><ol><li>  <span class="c"># XXX - this is a terrible way to implement accessors, but it makes</span></li><li>  <span class="c"># for a simple example.</span></li><li>  <a class="l_k" href="functions/our.html">our</a> <span class="i">$AUTOLOAD</span><span class="sc">;</span></li><li><a name="AUTOLOAD"></a>  sub <span class="m">AUTOLOAD</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="c"># Remove qualifier from original method name...</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$called</span> =  <span class="i">$AUTOLOAD</span> =~ <span class="q">s/.*:://</span><span class="w">r</span><span class="sc">;</span></li><li></li><li>      <span class="c"># Is there an attribute of that name?</span></li><li>      <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;No such attribute: $called&quot;</span></li><li>          <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$self</span>-&gt;{<span class="i">$called</span>}<span class="sc">;</span></li><li></li><li>      <span class="c"># If so, return it...</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span>-&gt;{<span class="i">$called</span>}<span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="DESTROY"></a>  sub <span class="m">DESTROY</span> <span class="s">{</span> <span class="s">}</span> <span class="c"># see below</span></li></ol></pre><p>Without the <code class="inline"><a class="l_k" href="functions/our.html">our</a> <span class="i">$AUTOLOAD</span></code>
 declaration, this code will not compile
under the <a href="strict.html">strict</a> pragma.</p>
<p>As the comment says, this is not a good way to implement accessors.
It's slow and too clever by far. However, you may see this as a way to
provide accessors in older Perl code. See <a href="perlootut.html">perlootut</a> for
recommendations on OO coding in Perl.</p>
<p>If your class does have an <code class="inline">AUTOLOAD</code>
 method, we strongly recommend
that you override <code class="inline"><span class="w">can</span></code>
 in your class as well. Your overridden <code class="inline"><span class="w">can</span></code>

method should return a subroutine reference for any method that your
<code class="inline">AUTOLOAD</code>
 responds to.</p>
<a name="Destructors"></a><h2>Destructors
 </h2>
<p>When the last reference to an object goes away, the object is
destroyed. If you only have one reference to an object stored in a
lexical scalar, the object is destroyed when that scalar goes out of
scope. If you store the object in a package global, that object may not
go out of scope until the program exits.</p>
<p>If you want to do something when the object is destroyed, you can
define a <code class="inline">DESTROY</code>
 method in your class. This method will always be
called by Perl at the appropriate time, unless the method is empty.</p>
<p>This is called just like any other method, with the object as the first
argument. It does not receive any additional arguments. However, the
<code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
 variable will be read-only in the destructor, so you cannot
assign a value to it.</p>
<p>If your <code class="inline">DESTROY</code>
 method throws an error, this error will be ignored.
It will not be sent to <code class="inline"><span class="w">STDERR</span></code>
 and it will not cause the program to
die. However, if your destructor is running inside an <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
 block,
then the error will change the value of <code class="inline"><span class="i">$@</span></code>
.</p>
<p>Because <code class="inline">DESTROY</code>
 methods can be called at any time, you should
localize any global variables you might update in your <code class="inline">DESTROY</code>
. In
particular, if you use <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
 you should localize <code class="inline"><span class="i">$@</span></code>
, and if you
use <code class="inline"><a class="l_k" href="functions/system.html">system</a></code> or backticks you should localize <code class="inline"><span class="i">$?</span></code>
.</p>
<p>If you define an <code class="inline">AUTOLOAD</code>
 in your class, then Perl will call your
<code class="inline">AUTOLOAD</code>
 to handle the <code class="inline">DESTROY</code>
 method. You can prevent this by
defining an empty <code class="inline">DESTROY</code>
, like we did in the autoloading example.
You can also check the value of <code class="inline"><span class="i">$AUTOLOAD</span></code>
 and return without doing
anything when called to handle <code class="inline">DESTROY</code>
.</p>
<a name="Global-Destruction"></a><h3>Global Destruction</h3>
<p>The order in which objects are destroyed during the global destruction
before the program exits is unpredictable. This means that any objects
contained by your object may already have been destroyed. You should
check that a contained object is defined before calling a method on it:</p>
<pre class="verbatim"><ol><li><a name="DESTROY"></a>  sub <span class="m">DESTROY</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>-&gt;{<span class="w">handle</span>}<span class="i">-&gt;close</span><span class="s">(</span><span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$self</span>-&gt;{<span class="w">handle</span>}<span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>You can use the <code class="inline"><span class="i">$</span>{<span class="w">^GLOBAL_PHASE</span>}</code>
 variable to detect if you are
currently in the global destruction phase:</p>
<pre class="verbatim"><ol><li><a name="DESTROY"></a>  sub <span class="m">DESTROY</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/return.html">return</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$</span>{<span class="w">^GLOBAL_PHASE</span>} <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;DESTRUCT&#39;</span><span class="sc">;</span></li><li></li><li>      <span class="i">$self</span>-&gt;{<span class="w">handle</span>}<span class="i">-&gt;close</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Note that this variable was added in Perl 5.14.0. If you want to detect
the global destruction phase on older versions of Perl, you can use the
<code class="inline"><span class="w">Devel::GlobalDestruction</span></code>
 module on CPAN.</p>
<p>If your <code class="inline">DESTROY</code>
 method issues a warning during global destruction,
the Perl interpreter will append the string " during global
destruction" to the warning.</p>
<p>During global destruction, Perl will always garbage collect objects
before unblessed references. See <a href="perlhacktips.html#PERL_DESTRUCT_LEVEL">PERL_DESTRUCT_LEVEL in perlhacktips</a>
for more information about global destruction.</p>
<a name="Non-Hash-Objects"></a><h2>Non-Hash Objects</h2>
<p>All the examples so far have shown objects based on a blessed hash.
However, it's possible to bless any type of data structure or referent,
including scalars, globs, and subroutines. You may see this sort of
thing when looking at code in the wild.</p>
<p>Here's an example of a module as a blessed scalar:</p>
<pre class="verbatim"><ol><li><a name="package-Time"></a>  package <span class="i">Time</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/my.html">my</a> <span class="i">$time</span> = <a class="l_k" href="functions/time.html">time</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$time</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="epoch"></a>  sub <span class="m">epoch</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/return.html">return</a> <span class="i">$</span>{ <span class="i">$self</span> }<span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$time</span> = <span class="w">Time</span><span class="w">-&gt;new</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="i">$time</span><span class="i">-&gt;epoch</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Inside-Out-objects"></a><h2>Inside-Out objects</h2>
<p>In the past, the Perl community experimented with a technique called
"inside-out objects". An inside-out object stores its data outside of
the object's reference, indexed on a unique property of the object,
such as its memory address, rather than in the object itself. This has
the advantage of enforcing the encapsulation of object attributes,
since their data is not stored in the object itself.</p>
<p>This technique was popular for a while (and was recommended in Damian
Conway's <i>Perl Best Practices</i>), but never achieved universal
adoption. The <a href="http://search.cpan.org/perldoc/Object::InsideOut">Object::InsideOut</a> module on CPAN provides a
comprehensive implementation of this technique, and you may see it or
other inside-out modules in the wild.</p>
<p>Here is a simple example of the technique, using the
<a href="Hash/Util/FieldHash.html">Hash::Util::FieldHash</a> core module. This module was added to the core
to support inside-out object implementations.</p>
<pre class="verbatim"><ol><li><a name="package-Time"></a>  package <span class="i">Time</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Hash::Util::FieldHash</span> <span class="q">&#39;fieldhash&#39;</span><span class="sc">;</span></li><li></li><li>  <span class="w">fieldhash</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">%time_for</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</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/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/bless.html">bless</a> \<span class="s">(</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$object</span> <span class="s">)</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li></li><li>      <span class="i">$time_for</span>{<span class="i">$self</span>} = <a class="l_k" href="functions/time.html">time</a><span class="sc">;</span></li><li></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="epoch"></a>  sub <span class="m">epoch</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/return.html">return</a> <span class="i">$time_for</span>{<span class="i">$self</span>}<span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$time</span> = <span class="w">Time</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="i">$time</span><span class="i">-&gt;epoch</span><span class="sc">;</span></li></ol></pre><a name="Pseudo-hashes"></a><h2>Pseudo-hashes</h2>
<p>The pseudo-hash feature was an experimental feature introduced in
earlier versions of Perl and removed in 5.10.0. A pseudo-hash is an
array reference which can be accessed using named keys like a hash. You
may run in to some code in the wild which uses it. See the <a href="fields.html">fields</a>
pragma for more information.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>A kinder, gentler tutorial on object-oriented programming in Perl can
be found in <a href="perlootut.html">perlootut</a>. You should also check out <a href="perlmodlib.html">perlmodlib</a> for
some style guides on constructing both modules and classes.</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="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#An-Object-is-Simply-a-Data-Structure">An Object is Simply a Data Structure
   </a><li><a href="#A-Class-is-Simply-a-Package">A Class is Simply a Package
   </a><li><a href="#A-Method-is-Simply-a-Subroutine">A Method is Simply a Subroutine
</a><li><a href="#Method-Invocation">Method Invocation
   </a><li><a href="#Inheritance">Inheritance
</a><li><a href="#Writing-Constructors">Writing Constructors
</a><li><a href="#Attributes">Attributes
</a><li><a href="#An-Aside-About-Smarter-and-Safer-Code">An Aside About Smarter and Safer Code</a><li><a href="#Method-Call-Variations">Method Call Variations
</a><li><a href="#Invoking-Class-Methods">Invoking Class Methods
</a><li><a href="#bless%2c-blessed%2c-and-ref">bless, blessed, and ref</a><li><a href="#The-UNIVERSAL-Class">The UNIVERSAL Class
</a><li><a href="#AUTOLOAD">AUTOLOAD
</a><li><a href="#Destructors">Destructors
 </a><li><a href="#Non-Hash-Objects">Non-Hash Objects</a><li><a href="#Inside-Out-objects">Inside-Out objects</a><li><a href="#Pseudo-hashes">Pseudo-hashes</a></ul><li><a href="#SEE-ALSO">SEE ALSO</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    = 'perlobj';
  perldoc.pageAddress = 'perlobj.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>