File: perltie.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 (1112 lines) | stat: -rw-r--r-- 115,637 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perltie - 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>perltie</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;
      
    
    perltie
  

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


  <!--    -->
<ul><li><a href="#NAME">NAME
</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Tying-Scalars">Tying Scalars
</a><li><a href="#Tying-Arrays">Tying Arrays
</a><li><a href="#Tying-Hashes">Tying Hashes
</a><li><a href="#Tying-FileHandles">Tying FileHandles
</a><li><a href="#UNTIE-this">UNTIE this
</a><li><a href="#The-untie-Gotcha">The untie Gotcha
</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#BUGS">BUGS</a><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME
</h1>
<p>perltie - how to hide an object class in a simple variable</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/tie.html">tie</a> <span class="w">VARIABLE</span><span class="cm">,</span> <span class="w">CLASSNAME</span><span class="cm">,</span> <span class="w">LIST</span></li><li></li><li> <span class="i">$object</span> = <a class="l_k" href="functions/tied.html">tied</a> <span class="w">VARIABLE</span></li><li></li><li> <a class="l_k" href="functions/untie.html">untie</a> <span class="w">VARIABLE</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>Prior to release 5.0 of Perl, a programmer could use dbmopen()
to connect an on-disk database in the standard Unix dbm(3x)
format magically to a %HASH in their program.  However, their Perl was either
built with one particular dbm library or another, but not both, and
you couldn't extend this mechanism to other packages or types of variables.</p>
<p>Now you can.</p>
<p>The tie() function binds a variable to a class (package) that will provide
the implementation for access methods for that variable.  Once this magic
has been performed, accessing a tied variable automatically triggers
method calls in the proper class.  The complexity of the class is
hidden behind magic methods calls.  The method names are in ALL CAPS,
which is a convention that Perl uses to indicate that they're called
implicitly rather than explicitly--just like the BEGIN() and END()
functions.</p>
<p>In the tie() call, <code class="inline"><span class="w">VARIABLE</span></code>
 is the name of the variable to be
enchanted.  <code class="inline"><span class="w">CLASSNAME</span></code>
 is the name of a class implementing objects of
the correct type.  Any additional arguments in the <code class="inline"><span class="w">LIST</span></code>
 are passed to
the appropriate constructor method for that class--meaning TIESCALAR(),
TIEARRAY(), TIEHASH(), or TIEHANDLE().  (Typically these are arguments
such as might be passed to the dbminit() function of C.) The object
returned by the "new" method is also returned by the tie() function,
which would be useful if you wanted to access other methods in
<code class="inline"><span class="w">CLASSNAME</span></code>
. (You don't actually have to return a reference to a right
"type" (e.g., HASH or <code class="inline"><span class="w">CLASSNAME</span></code>
) so long as it's a properly blessed
object.)  You can also retrieve a reference to the underlying object
using the tied() function.</p>
<p>Unlike dbmopen(), the tie() function will not <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> or <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> a module
for you--you need to do that explicitly yourself.</p>
<a name="Tying-Scalars"></a><h2>Tying Scalars
</h2>
<p>A class implementing a tied scalar should define the following methods:
TIESCALAR, FETCH, STORE, and possibly UNTIE and/or DESTROY.</p>
<p>Let's look at each in turn, using as an example a tie class for
scalars that allows the user to do something like:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$his_speed</span><span class="cm">,</span> <span class="q">&#39;Nice&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/getppid.html">getppid</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$my_speed</span><span class="cm">,</span>  <span class="q">&#39;Nice&#39;</span><span class="cm">,</span> <span class="i">$$</span><span class="sc">;</span></li></ol></pre><p>And now whenever either of those variables is accessed, its current
system priority is retrieved and returned.  If those variables are set,
then the process's priority is changed!</p>
<p>We'll use Jarkko Hietaniemi &lt;<i>jhi@iki.fi</i>&gt;'s BSD::Resource class (not
included) to access the PRIO_PROCESS, PRIO_MIN, and PRIO_MAX constants
from your system, as well as the getpriority() and setpriority() system
calls.  Here's the preamble of the class.</p>
<pre class="verbatim"><ol><li><a name="package-Nice"></a>    package <span class="i">Nice</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Carp</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">BSD::Resource</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li>    <span class="i">$Nice::DEBUG</span> = <span class="n">0</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$Nice::DEBUG</span><span class="sc">;</span></li></ol></pre><ul>
<li><a name="TIESCALAR-classname%2c-LIST"></a><b>TIESCALAR classname, LIST
</b>
<p>This is the constructor for the class.  That means it is
expected to return a blessed reference to a new scalar
(probably anonymous) that it's creating.  For example:</p>
<pre class="verbatim"><ol><li><a name="TIESCALAR"></a> sub <span class="m">TIESCALAR</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="i">$pid</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="i">$$</span><span class="sc">;</span> <span class="c"># 0 means me</span></li><li></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$pid</span> !~ <span class="q">/^\d+$/</span><span class="s">)</span> <span class="s">{</span></li><li>         <span class="w">carp</span> <span class="q">&quot;Nice::Tie::Scalar got non-numeric pid $pid&quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$^W</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>     <span class="s">}</span></li><li></li><li>     <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><a class="l_k" href="functions/kill.html">kill</a> <span class="n">0</span><span class="cm">,</span> <span class="i">$pid</span><span class="s">)</span> <span class="s">{</span> <span class="c"># EPERM or ERSCH, no doubt</span></li><li>         <span class="w">carp</span> <span class="q">&quot;Nice::Tie::Scalar got bad pid $pid: $!&quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$^W</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/undef.html">undef</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> <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$pid</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>This tie class has chosen to return an error rather than raising an
exception if its constructor should fail.  While this is how dbmopen() works,
other classes may well not wish to be so forgiving.  It checks the global
variable <code class="inline"><span class="i">$^W</span></code>
 to see whether to emit a bit of noise anyway.</p>
</li>
<li><a name="FETCH-this"></a><b>FETCH this
</b>
<p>This method will be triggered every time the tied variable is accessed
(read).  It takes no arguments beyond its self reference, which is the
object representing the scalar we're dealing with.  Because in this case
we're using just a SCALAR ref for the tied scalar object, a simple $$self
allows the method to get at the real value stored there.  In our example
below, that real value is the process ID to which we've tied our variable.</p>
<pre class="verbatim"><ol><li><a name="FETCH"></a>    sub <span class="m">FETCH</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <span class="w">confess</span> <span class="q">&quot;wrong type&quot;</span> unless <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="sc">;</span></li><li>        <span class="w">croak</span> <span class="q">&quot;usage error&quot;</span> if <span class="i">@_</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$nicety</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">$!</span><span class="s">)</span> = <span class="n">0</span><span class="sc">;</span></li><li>        <span class="i">$nicety</span> = <a class="l_k" href="functions/getpriority.html">getpriority</a><span class="s">(</span><span class="w">PRIO_PROCESS</span><span class="cm">,</span> <span class="i">$$self</span><span class="s">)</span><span class="sc">;</span></li><li>        if <span class="s">(</span><span class="i">$!</span><span class="s">)</span> <span class="s">{</span> <span class="w">croak</span> <span class="q">&quot;getpriority failed: $!&quot;</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="i">$nicety</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>This time we've decided to blow up (raise an exception) if the renice
fails--there's no place for us to return an error otherwise, and it's
probably the right thing to do.</p>
</li>
<li><a name="STORE-this%2c-value"></a><b>STORE this, value
</b>
<p>This method will be triggered every time the tied variable is set
(assigned).  Beyond its self reference, it also expects one (and only one)
argument: the new value the user is trying to assign. Don't worry about
returning a value from STORE; the semantic of assignment returning the
assigned value is implemented with FETCH.</p>
<pre class="verbatim"><ol><li><a name="STORE"></a> sub <span class="m">STORE</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <span class="w">confess</span> <span class="q">&quot;wrong type&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$new_nicety</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <span class="w">croak</span> <span class="q">&quot;usage error&quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">@_</span><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">$new_nicety</span> &lt; <span class="w">PRIO_MIN</span><span class="s">)</span> <span class="s">{</span></li><li>         <span class="w">carp</span> <a class="l_k" href="functions/sprintf.html">sprintf</a></li><li>           <span class="q">&quot;WARNING: priority %d less than minimum system priority %d&quot;</span><span class="cm">,</span></li><li>               <span class="i">$new_nicety</span><span class="cm">,</span> <span class="w">PRIO_MIN</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$^W</span><span class="sc">;</span></li><li>         <span class="i">$new_nicety</span> = <span class="w">PRIO_MIN</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$new_nicety</span> &gt; <span class="w">PRIO_MAX</span><span class="s">)</span> <span class="s">{</span></li><li>         <span class="w">carp</span> <a class="l_k" href="functions/sprintf.html">sprintf</a></li><li>           <span class="q">&quot;WARNING: priority %d greater than maximum system priority %d&quot;</span><span class="cm">,</span></li><li>               <span class="i">$new_nicety</span><span class="cm">,</span> <span class="w">PRIO_MAX</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$^W</span><span class="sc">;</span></li><li>         <span class="i">$new_nicety</span> = <span class="w">PRIO_MAX</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li></li><li>     <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <a class="l_k" href="functions/setpriority.html">setpriority</a><span class="s">(</span><span class="w">PRIO_PROCESS</span><span class="cm">,</span></li><li>                                 <span class="i">$$self</span><span class="cm">,</span></li><li>                                 <span class="i">$new_nicety</span><span class="s">)</span><span class="s">)</span></li><li>     <span class="s">{</span></li><li>         <span class="w">confess</span> <span class="q">&quot;setpriority failed: $!&quot;</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="UNTIE-this"></a><b>UNTIE this
</b>
<p>This method will be triggered when the <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> occurs. This can be useful
if the class needs to know when no further calls will be made. (Except DESTROY
of course.) See <a href="#The-untie-Gotcha">The untie Gotcha</a> below for more details.</p>
</li>
<li><a name="DESTROY-this"></a><b>DESTROY this
</b>
<p>This method will be triggered when the tied variable needs to be destructed.
As with other object classes, such a method is seldom necessary, because Perl
deallocates its moribund object's memory for you automatically--this isn't
C++, you know.  We'll use a DESTROY method here for debugging purposes only.</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>        <span class="w">confess</span> <span class="q">&quot;wrong type&quot;</span> unless <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="sc">;</span></li><li>        <span class="w">carp</span> <span class="q">&quot;[ Nice::DESTROY pid $$self ]&quot;</span> if <span class="i">$Nice::DEBUG</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
</ul>
<p>That's about all there is to it.  Actually, it's more than all there
is to it, because we've done a few nice things here for the sake
of completeness, robustness, and general aesthetics.  Simpler
TIESCALAR classes are certainly possible.</p>
<a name="Tying-Arrays"></a><h2>Tying Arrays
</h2>
<p>A class implementing a tied ordinary array should define the following
methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE, CLEAR
and perhaps UNTIE and/or DESTROY.</p>
<p>FETCHSIZE and STORESIZE are used to provide <code class="inline"><span class="i">$#array</span></code>
 and
equivalent <code class="inline"><a class="l_k" href="functions/scalar.html">scalar(@array)</a></code> access.</p>
<p>The methods POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, and EXISTS are
required if the perl operator with the corresponding (but lowercase) name
is to operate on the tied array. The <b>Tie::Array</b> class can be used as a
base class to implement the first five of these in terms of the basic
methods above.  The default implementations of DELETE and EXISTS in
<b>Tie::Array</b> simply <code class="inline"><span class="w">croak</span></code>
.</p>
<p>In addition EXTEND will be called when perl would have pre-extended
allocation in a real array.</p>
<p>For this discussion, we'll implement an array whose elements are a fixed
size at creation.  If you try to create an element larger than the fixed
size, you'll take an exception.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">FixedElem_Array</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">@array</span><span class="cm">,</span> <span class="q">&#39;FixedElem_Array&#39;</span><span class="cm">,</span> <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$array</span>[<span class="n">0</span>] = <span class="q">&#39;cat&#39;</span><span class="sc">;</span>  <span class="c"># ok.</span></li><li>    <span class="i">$array</span>[<span class="n">1</span>] = <span class="q">&#39;dogs&#39;</span><span class="sc">;</span> <span class="c"># exception, length(&#39;dogs&#39;) &gt; 3.</span></li></ol></pre><p>The preamble code for the class is as follows:</p>
<pre class="verbatim"><ol><li><a name="package-FixedElem_Array"></a>    package <span class="i">FixedElem_Array</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Carp</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li></ol></pre><ul>
<li><a name="TIEARRAY-classname%2c-LIST"></a><b>TIEARRAY classname, LIST
</b>
<p>This is the constructor for the class.  That means it is expected to
return a blessed reference through which the new array (probably an
anonymous ARRAY ref) will be accessed.</p>
<p>In our example, just to show you that you don't <i>really</i> have to return an
ARRAY reference, we'll choose a HASH reference to represent our object.
A HASH works out well as a generic record type: the <code class="inline"><span class="s">{</span><span class="w">ELEMSIZE</span><span class="s">}</span></code>
 field will
store the maximum element size allowed, and the <code class="inline"><span class="s">{</span><span class="w">ARRAY</span><span class="s">}</span></code>
 field will hold the
true ARRAY ref.  If someone outside the class tries to dereference the
object returned (doubtless thinking it an ARRAY ref), they'll blow up.
This just goes to show you that you should respect an object's privacy.</p>
<pre class="verbatim"><ol><li><a name="TIEARRAY"></a>    sub <span class="m">TIEARRAY</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="i">$elemsize</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      if <span class="s">(</span> <span class="i">@_</span> || <span class="i">$elemsize</span> =~ <span class="q">/\D/</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="w">croak</span> <span class="q">&quot;usage: tie ARRAY, &#39;&quot;</span> . <span class="w">__PACKAGE__</span> . <span class="q">&quot;&#39;, elem_size&quot;</span><span class="sc">;</span></li><li>      <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></li><li>        <span class="w">ELEMSIZE</span> <span class="cm">=&gt;</span> <span class="i">$elemsize</span><span class="cm">,</span></li><li>        <span class="w">ARRAY</span>    <span class="cm">=&gt;</span> <span class="s">[</span><span class="s">]</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>    <span class="s">}</span></li></ol></pre></li>
<li><a name="FETCH-this%2c-index"></a><b>FETCH this, index
</b>
<p>This method will be triggered every time an individual element the tied array
is accessed (read).  It takes one argument beyond its self reference: the
index whose value we're trying to fetch.</p>
<pre class="verbatim"><ol><li><a name="FETCH"></a>    sub <span class="m">FETCH</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$index</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">$self</span>-&gt;{<span class="w">ARRAY</span>}-&gt;[<span class="i">$index</span>]<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>If a negative array index is used to read from an array, the index
will be translated to a positive one internally by calling FETCHSIZE
before being passed to FETCH.  You may disable this feature by
assigning a true value to the variable <code class="inline"><span class="i">$NEGATIVE_INDICES</span></code>
 in the
tied array class.</p>
<p>As you may have noticed, the name of the FETCH method (et al.) is the same
for all accesses, even though the constructors differ in names (TIESCALAR
vs TIEARRAY).  While in theory you could have the same class servicing
several tied types, in practice this becomes cumbersome, and it's easiest
to keep them at simply one tie type per class.</p>
</li>
<li><a name="STORE-this%2c-index%2c-value"></a><b>STORE this, index, value
</b>
<p>This method will be triggered every time an element in the tied array is set
(written).  It takes two arguments beyond its self reference: the index at
which we're trying to store something and the value we're trying to put
there.</p>
<p>In our example, <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is really <code class="inline"><span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}</code>
 number of
spaces so we have a little more work to do here:</p>
<pre class="verbatim"><ol><li><a name="STORE"></a> sub <span class="m">STORE</span> <span class="s">{</span></li><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>   <a class="l_k" href="functions/my.html">my</a><span class="s">(</span> <span class="i">$index</span><span class="cm">,</span> <span class="i">$value</span> <span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>   <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <a class="l_k" href="functions/length.html">length</a> <span class="i">$value</span> &gt; <span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>} <span class="s">)</span> <span class="s">{</span></li><li>     <span class="w">croak</span> <span class="q">&quot;length of $value is greater than $self-&gt;{ELEMSIZE}&quot;</span><span class="sc">;</span></li><li>   <span class="s">}</span></li><li>   <span class="c"># fill in the blanks</span></li><li>   <span class="i">$self</span><span class="i">-&gt;EXTEND</span><span class="s">(</span> <span class="i">$index</span> <span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$index</span> &gt; <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>   <span class="c"># right justify to keep element size for smaller elements</span></li><li>   <span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}-&gt;[<span class="i">$index</span>] = <a class="l_k" href="functions/sprintf.html">sprintf</a> <span class="q">&quot;%$self-&gt;{ELEMSIZE}s&quot;</span><span class="cm">,</span> <span class="i">$value</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Negative indexes are treated the same as with FETCH.</p>
</li>
<li><a name="FETCHSIZE-this"></a><b>FETCHSIZE this
</b>
<p>Returns the total number of items in the tied array associated with
object <i>this</i>. (Equivalent to <code class="inline"><a class="l_k" href="functions/scalar.html">scalar(@array)</a></code>).  For example:</p>
<pre class="verbatim"><ol><li><a name="FETCHSIZE"></a>    sub <span class="m">FETCHSIZE</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> <a class="l_k" href="functions/scalar.html">scalar</a> <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="STORESIZE-this%2c-count"></a><b>STORESIZE this, count
</b>
<p>Sets the total number of items in the tied array associated with
object <i>this</i> to be <i>count</i>. If this makes the array larger then
class's mapping of <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> should be returned for new positions.
If the array becomes smaller then entries beyond count should be
deleted.</p>
<p>In our example, 'undef' is really an element containing
<code class="inline"><span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}</code>
 number of spaces.  Observe:</p>
<pre class="verbatim"><ol><li><a name="STORESIZE"></a>    sub <span class="m">STORESIZE</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      if <span class="s">(</span> <span class="i">$count</span> &gt; <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        foreach <span class="s">(</span> <span class="i">$count</span> - <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span> .. <span class="i">$count</span> <span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span><span class="i">-&gt;STORE</span><span class="s">(</span> <span class="i">$_</span><span class="cm">,</span> <span class="q">&#39;&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>      <span class="s">}</span> elsif <span class="s">(</span> <span class="i">$count</span> &lt; <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        foreach <span class="s">(</span> <span class="n">0</span> .. <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span> - <span class="i">$count</span> - <span class="n">2</span> <span class="s">)</span> <span class="s">{</span></li><li>          <span class="i">$self</span><span class="i">-&gt;POP</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>      <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="EXTEND-this%2c-count"></a><b>EXTEND this, count
</b>
<p>Informative call that array is likely to grow to have <i>count</i> entries.
Can be used to optimize allocation. This method need do nothing.</p>
<p>In our example, we want to make sure there are no blank (<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>)
entries, so <code class="inline"><span class="w">EXTEND</span></code>
 will make use of <code class="inline"><span class="w">STORESIZE</span></code>
 to fill elements
as needed:</p>
<pre class="verbatim"><ol><li><a name="EXTEND"></a>    sub <span class="m">EXTEND</span> <span class="s">{</span>   </li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;STORESIZE</span><span class="s">(</span> <span class="i">$count</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="EXISTS-this%2c-key"></a><b>EXISTS this, key
</b>
<p>Verify that the element at index <i>key</i> exists in the tied array <i>this</i>.</p>
<p>In our example, we will determine that if an element consists of
<code class="inline"><span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}</code>
 spaces only, it does not exist:</p>
<pre class="verbatim"><ol><li><a name="EXISTS"></a> sub <span class="m">EXISTS</span> <span class="s">{</span></li><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">$index</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="n">0</span> <a class="l_k" href="functions/if.html">if</a> ! <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}-&gt;[<span class="i">$index</span>] ||</li><li>               <span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}-&gt;[<span class="i">$index</span>] <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39; &#39;</span> x <span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}<span class="sc">;</span></li><li>   <a class="l_k" href="functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="DELETE-this%2c-key"></a><b>DELETE this, key
</b>
<p>Delete the element at index <i>key</i> from the tied array <i>this</i>.</p>
<p>In our example, a deleted item is <code class="inline"><span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}</code>
 spaces:</p>
<pre class="verbatim"><ol><li><a name="DELETE"></a>    sub <span class="m">DELETE</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$index</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">$self</span><span class="i">-&gt;STORE</span><span class="s">(</span> <span class="i">$index</span><span class="cm">,</span> <span class="q">&#39;&#39;</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="CLEAR-this"></a><b>CLEAR this
</b>
<p>Clear (remove, delete, ...) all values from the tied array associated with
object <i>this</i>.  For example:</p>
<pre class="verbatim"><ol><li><a name="CLEAR"></a>    sub <span class="m">CLEAR</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">$self</span>-&gt;{<span class="w">ARRAY</span>} = <span class="s">[</span><span class="s">]</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="PUSH-this%2c-LIST"></a><b>PUSH this, LIST 
</b>
<p>Append elements of <i>LIST</i> to the array.  For example:</p>
<pre class="verbatim"><ol><li><a name="PUSH"></a>    sub <span class="m">PUSH</span> <span class="s">{</span>  </li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span> = <span class="i">@_</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$last</span> = <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;STORE</span><span class="s">(</span> <span class="i">$last</span> + <span class="i">$_</span><span class="cm">,</span> <span class="i">$list</span>[<span class="i">$_</span>] <span class="s">)</span> foreach <span class="n">0</span> .. <span class="i">$#list</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="POP-this"></a><b>POP this
</b>
<p>Remove last element of the array and return it.  For example:</p>
<pre class="verbatim"><ol><li><a name="POP"></a>    sub <span class="m">POP</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> <a class="l_k" href="functions/pop.html">pop</a> <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="SHIFT-this"></a><b>SHIFT this
</b>
<p>Remove the first element of the array (shifting other elements down)
and return it.  For example:</p>
<pre class="verbatim"><ol><li><a name="SHIFT"></a>    sub <span class="m">SHIFT</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> <a class="l_k" href="functions/shift.html">shift</a> <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="UNSHIFT-this%2c-LIST"></a><b>UNSHIFT this, LIST 
</b>
<p>Insert LIST elements at the beginning of the array, moving existing elements
up to make room.  For example:</p>
<pre class="verbatim"><ol><li><a name="UNSHIFT"></a>    sub <span class="m">UNSHIFT</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span> = <span class="i">@_</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$size</span> = <a class="l_k" href="functions/scalar.html">scalar</a><span class="s">(</span> <span class="i">@list</span> <span class="s">)</span><span class="sc">;</span></li><li>      <span class="c"># make room for our list</span></li><li>      <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}[ <span class="i">$size</span> .. <span class="i">$#</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}} + <span class="i">$size</span> ]</li><li>       = <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}<span class="sc">;</span></li><li>      <span class="i">$self</span><span class="i">-&gt;STORE</span><span class="s">(</span> <span class="i">$_</span><span class="cm">,</span> <span class="i">$list</span>[<span class="i">$_</span>] <span class="s">)</span> foreach <span class="n">0</span> .. <span class="i">$#list</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="SPLICE-this%2c-offset%2c-length%2c-LIST"></a><b>SPLICE this, offset, length, LIST
</b>
<p>Perform the equivalent of <code class="inline"><a class="l_k" href="functions/splice.html">splice</a></code> on the array.</p>
<p><i>offset</i> is optional and defaults to zero, negative values count back 
from the end of the array.</p>
<p><i>length</i> is optional and defaults to rest of the array.</p>
<p><i>LIST</i> may be empty.</p>
<p>Returns a list of the original <i>length</i> elements at <i>offset</i>.</p>
<p>In our example, we'll use a little shortcut if there is a <i>LIST</i>:</p>
<pre class="verbatim"><ol><li><a name="SPLICE"></a>    sub <span class="m">SPLICE</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span>   = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$offset</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="n">0</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$length</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="i">$self</span><span class="i">-&gt;FETCHSIZE</span><span class="s">(</span><span class="s">)</span> - <span class="i">$offset</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span>   = <span class="s">(</span><span class="s">)</span><span class="sc">;</span> </li><li>      if <span class="s">(</span> <span class="i">@_</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/tie.html">tie</a> <span class="i">@list</span><span class="cm">,</span> <span class="w">__PACKAGE__</span><span class="cm">,</span> <span class="i">$self</span>-&gt;{<span class="w">ELEMSIZE</span>}<span class="sc">;</span></li><li>        <span class="i">@list</span>   = <span class="i">@_</span><span class="sc">;</span></li><li>      <span class="s">}</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/splice.html">splice</a> <span class="i">@</span>{<span class="i">$self</span>-&gt;{<span class="w">ARRAY</span>}}<span class="cm">,</span> <span class="i">$offset</span><span class="cm">,</span> <span class="i">$length</span><span class="cm">,</span> <span class="i">@list</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="UNTIE-this"></a><b>UNTIE this
</b>
<p>Will be called when <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> happens. (See <a href="#The-untie-Gotcha">The untie Gotcha</a> below.)</p>
</li>
<li><a name="DESTROY-this"></a><b>DESTROY this
</b>
<p>This method will be triggered when the tied variable needs to be destructed.
As with the scalar tie class, this is almost never needed in a
language that does its own garbage collection, so this time we'll
just leave it out.</p>
</li>
</ul>
<a name="Tying-Hashes"></a><h2>Tying Hashes
</h2>
<p>Hashes were the first Perl data type to be tied (see dbmopen()).  A class
implementing a tied hash should define the following methods: TIEHASH is
the constructor.  FETCH and STORE access the key and value pairs.  EXISTS
reports whether a key is present in the hash, and DELETE deletes one.
CLEAR empties the hash by deleting all the key and value pairs.  FIRSTKEY
and NEXTKEY implement the keys() and each() functions to iterate over all
the keys. SCALAR is triggered when the tied hash is evaluated in scalar 
context. UNTIE is called when <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> happens, and DESTROY is called when
the tied variable is garbage collected.</p>
<p>If this seems like a lot, then feel free to inherit from merely the
standard Tie::StdHash module for most of your methods, redefining only the
interesting ones.  See <a href="Tie/Hash.html">Tie::Hash</a> for details.</p>
<p>Remember that Perl distinguishes between a key not existing in the hash,
and the key existing in the hash but having a corresponding value of
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.  The two possibilities can be tested with the <code class="inline"><a class="l_k" href="functions/exists.html">exists()</a></code> and
<code class="inline"><a class="l_k" href="functions/defined.html">defined()</a></code> functions.</p>
<p>Here's an example of a somewhat interesting tied hash class:  it gives you
a hash representing a particular user's dot files.  You index into the hash
with the name of the file (minus the dot) and you get back that dot file's
contents.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">DotFiles</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%dot</span><span class="cm">,</span> <span class="q">&#39;DotFiles&#39;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="i">$dot</span>{<span class="w">profile</span>} =~ <span class="q">/MANPATH/</span> ||</li><li>         <span class="i">$dot</span>{<span class="w">login</span>}   =~ <span class="q">/MANPATH/</span> ||</li><li>         <span class="i">$dot</span>{<span class="w">cshrc</span>}   =~ <span class="q">/MANPATH/</span>    <span class="s">)</span></li><li>    <span class="s">{</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;you seem to set your MANPATH\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Or here's another sample of using our tied class:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%him</span><span class="cm">,</span> <span class="q">&#39;DotFiles&#39;</span><span class="cm">,</span> <span class="q">&#39;daemon&#39;</span><span class="sc">;</span></li><li>    foreach <span class="i">$f</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%him</span> <span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;daemon dot file %s is size %d\n&quot;</span><span class="cm">,</span></li><li>	    <span class="i">$f</span><span class="cm">,</span> <a class="l_k" href="functions/length.html">length</a> <span class="i">$him</span>{<span class="i">$f</span>}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>In our tied hash DotFiles example, we use a regular
hash for the object containing several important
fields, of which only the <code class="inline"><span class="s">{</span><span class="w">LIST</span><span class="s">}</span></code>
 field will be what the
user thinks of as the real hash.</p>
<ul>
<li><a name="USER"></a><b>USER</b>
<p>whose dot files this object represents</p>
</li>
<li><a name="HOME"></a><b>HOME</b>
<p>where those dot files live</p>
</li>
<li><a name="CLOBBER"></a><b>CLOBBER</b>
<p>whether we should try to change or remove those dot files</p>
</li>
<li><a name="LIST"></a><b>LIST</b>
<p>the hash of dot file names and content mappings</p>
</li>
</ul>
<p>Here's the start of <i>Dotfiles.pm</i>:</p>
<pre class="verbatim"><ol><li><a name="package-DotFiles"></a>    package <span class="i">DotFiles</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Carp</span><span class="sc">;</span></li><li><a name="whowasi"></a>    sub <span class="m">whowasi</span> <span class="s">{</span> <span class="s">(</span><a class="l_k" href="functions/caller.html">caller</a><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="s">)</span>[<span class="n">3</span>] . <span class="q">&#39;()&#39;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$DEBUG</span> = <span class="n">0</span><span class="sc">;</span></li><li><a name="debug"></a>    sub <span class="m">debug</span> <span class="s">{</span> <span class="i">$DEBUG</span> = <span class="i">@_</span> ? <a class="l_k" href="functions/shift.html">shift</a> <span class="co">:</span> <span class="n">1</span> <span class="s">}</span></li></ol></pre><p>For our example, we want to be able to emit debugging info to help in tracing
during development.  We keep also one convenience function around
internally to help print out warnings; whowasi() returns the function name
that calls it.</p>
<p>Here are the methods for the DotFiles tied hash.</p>
<ul>
<li><a name="TIEHASH-classname%2c-LIST"></a><b>TIEHASH classname, LIST
</b>
<p>This is the constructor for the class.  That means it is expected to
return a blessed reference through which the new object (probably but not
necessarily an anonymous hash) will be accessed.</p>
<p>Here's the constructor:</p>
<pre class="verbatim"><ol><li><a name="TIEHASH"></a>    sub <span class="m">TIEHASH</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$user</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="i">$&gt;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dotdir</span> = <a class="l_k" href="functions/shift.html">shift</a> || <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>	<span class="w">croak</span> <span class="q">&quot;usage: @{[&amp;whowasi]} [USER [DOTDIR]]&quot;</span> if <span class="i">@_</span><span class="sc">;</span></li><li>	<span class="i">$user</span> = <a class="l_k" href="functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$user</span><span class="s">)</span> if <span class="i">$user</span> =~ <span class="q">/^\d+$/</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dir</span> = <span class="s">(</span><a class="l_k" href="functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$user</span><span class="s">)</span><span class="s">)</span>[<span class="n">7</span>]</li><li>		|| <span class="w">croak</span> <span class="q">&quot;@{[&amp;whowasi]}: no user $user&quot;</span><span class="sc">;</span></li><li>	<span class="i">$dir</span> .= <span class="q">&quot;/$dotdir&quot;</span> if <span class="i">$dotdir</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$node</span> = <span class="s">{</span></li><li>	    <span class="w">USER</span>    <span class="cm">=&gt;</span> <span class="i">$user</span><span class="cm">,</span></li><li>	    <span class="w">HOME</span>    <span class="cm">=&gt;</span> <span class="i">$dir</span><span class="cm">,</span></li><li>	    <span class="w">LIST</span>    <span class="cm">=&gt;</span> <span class="s">{</span><span class="s">}</span><span class="cm">,</span></li><li>	    <span class="w">CLOBBER</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="cm">,</span></li><li>	<span class="s">}</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/opendir.html">opendir</a><span class="s">(</span><span class="w">DIR</span><span class="cm">,</span> <span class="i">$dir</span><span class="s">)</span></li><li>		|| <span class="w">croak</span> <span class="q">&quot;@{[&amp;whowasi]}: can&#39;t opendir $dir: $!&quot;</span><span class="sc">;</span></li><li>	foreach <span class="i">$dot</span> <span class="s">(</span> <a class="l_k" href="functions/grep.html">grep</a> <span class="q">/^\./</span> &amp;&amp; -f <span class="q">&quot;$dir/$_&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/readdir.html">readdir</a><span class="s">(</span><span class="w">DIR</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	    <span class="i">$dot</span> =~ <span class="q">s/^\.//</span><span class="sc">;</span></li><li>	    <span class="i">$node</span>-&gt;{<span class="w">LIST</span>}{<span class="i">$dot</span>} = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="functions/closedir.html">closedir</a> <span class="w">DIR</span><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">$node</span><span class="cm">,</span> <span class="i">$self</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>It's probably worth mentioning that if you're going to filetest the
return values out of a readdir, you'd better prepend the directory
in question.  Otherwise, because we didn't chdir() there, it would
have been testing the wrong file.</p>
</li>
<li><a name="FETCH-this%2c-key"></a><b>FETCH this, key
</b>
<p>This method will be triggered every time an element in the tied hash is
accessed (read).  It takes one argument beyond its self reference: the key
whose value we're trying to fetch.</p>
<p>Here's the fetch for our DotFiles example.</p>
<pre class="verbatim"><ol><li><a name="FETCH"></a>    sub <span class="m">FETCH</span> <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dot</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dir</span> = <span class="i">$self</span>-&gt;{<span class="w">HOME</span>}<span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="q">&quot;$dir/.$dot&quot;</span><span class="sc">;</span></li><li></li><li>	unless <span class="s">(</span><a class="l_k" href="functions/exists.html">exists</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>} || -f <span class="i">$file</span><span class="s">)</span> <span class="s">{</span></li><li>	    <span class="w">carp</span> <span class="q">&quot;@{[&amp;whowasi]}: no $dot file&quot;</span> if <span class="i">$DEBUG</span><span class="sc">;</span></li><li>	    <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>	<span class="s">}</span></li><li></li><li>	if <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>}<span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>}<span class="sc">;</span></li><li>	<span class="s">}</span> else <span class="s">{</span></li><li>	    <a class="l_k" href="functions/return.html">return</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>} = <span class="q">`cat $dir/.$dot`</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>It was easy to write by having it call the Unix cat(1) command, but it
would probably be more portable to open the file manually (and somewhat
more efficient).  Of course, because dot files are a Unixy concept, we're
not that concerned.</p>
</li>
<li><a name="STORE-this%2c-key%2c-value"></a><b>STORE this, key, value
</b>
<p>This method will be triggered every time an element in the tied hash is set
(written).  It takes two arguments beyond its self reference: the index at
which we're trying to store something, and the value we're trying to put
there.</p>
<p>Here in our DotFiles example, we'll be careful not to let
them try to overwrite the file unless they've called the clobber()
method on the original object reference returned by tie().</p>
<pre class="verbatim"><ol><li><a name="STORE"></a>    sub <span class="m">STORE</span> <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dot</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="i">$self</span>-&gt;{<span class="w">HOME</span>} . <span class="q">&quot;/.$dot&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$user</span> = <span class="i">$self</span>-&gt;{<span class="w">USER</span>}<span class="sc">;</span></li><li></li><li>	<span class="w">croak</span> <span class="q">&quot;@{[&amp;whowasi]}: $file not clobberable&quot;</span></li><li>	    unless <span class="i">$self</span>-&gt;{<span class="w">CLOBBER</span>}<span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span> || <span class="w">croak</span> <span class="q">&quot;can&#39;t open $file: $!&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="i">$f</span> <span class="i">$value</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="i">$f</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>If they wanted to clobber something, they might say:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ob</span> = <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%daemon_dots</span><span class="cm">,</span> <span class="q">&#39;daemon&#39;</span><span class="sc">;</span></li><li>    <span class="i">$ob</span><span class="i">-&gt;clobber</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$daemon_dots</span>{<span class="w">signature</span>} = <span class="q">&quot;A true daemon\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Another way to lay hands on a reference to the underlying object is to
use the tied() function, so they might alternately have set clobber
using:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%daemon_dots</span><span class="cm">,</span> <span class="q">&#39;daemon&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tied.html">tied</a><span class="s">(</span><span class="i">%daemon_dots</span><span class="s">)</span><span class="i">-&gt;clobber</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The clobber method is simply:</p>
<pre class="verbatim"><ol><li><a name="clobber"></a>    sub <span class="m">clobber</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<span class="i">$self</span>-&gt;{<span class="w">CLOBBER</span>} = <span class="i">@_</span> ? <a class="l_k" href="functions/shift.html">shift</a> <span class="co">:</span> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="DELETE-this%2c-key"></a><b>DELETE this, key
</b>
<p>This method is triggered when we remove an element from the hash,
typically by using the delete() function.  Again, we'll
be careful to check whether they really want to clobber files.</p>
<pre class="verbatim"><ol><li><a name="DELETE"></a> sub <span class="m">DELETE</span>   <span class="s">{</span></li><li>     <span class="w">carp</span> <span class="i">&amp;whowasi</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$DEBUG</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/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$dot</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span> = <span class="i">$self</span>-&gt;{<span class="w">HOME</span>} . <span class="q">&quot;/.$dot&quot;</span><span class="sc">;</span></li><li>     <span class="w">croak</span> <span class="q">&quot;@{[&amp;whowasi]}: won&#39;t remove file $file&quot;</span></li><li>         <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$self</span>-&gt;{<span class="w">CLOBBER</span>}<span class="sc">;</span></li><li>     <a class="l_k" href="functions/delete.html">delete</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>}<span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$success</span> = <a class="l_k" href="functions/unlink.html">unlink</a><span class="s">(</span><span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="w">carp</span> <span class="q">&quot;@{[&amp;whowasi]}: can&#39;t unlink $file: $!&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$success</span><span class="sc">;</span></li><li>     <span class="i">$success</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>The value returned by DELETE becomes the return value of the call
to delete().  If you want to emulate the normal behavior of delete(),
you should return whatever FETCH would have returned for this key.
In this example, we have chosen instead to return a value which tells
the caller whether the file was successfully deleted.</p>
</li>
<li><a name="CLEAR-this"></a><b>CLEAR this
</b>
<p>This method is triggered when the whole hash is to be cleared, usually by
assigning the empty list to it.</p>
<p>In our example, that would remove all the user's dot files!  It's such a
dangerous thing that they'll have to set CLOBBER to something higher than
1 to make it happen.</p>
<pre class="verbatim"><ol><li><a name="CLEAR"></a> sub <span class="m">CLEAR</span>    <span class="s">{</span></li><li>     <span class="w">carp</span> <span class="i">&amp;whowasi</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$DEBUG</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <span class="w">croak</span> <span class="q">&quot;@{[&amp;whowasi]}: won&#39;t remove all dot files for $self-&gt;{USER}&quot;</span></li><li>         <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$self</span>-&gt;{<span class="w">CLOBBER</span>} &gt; <span class="n">1</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$dot</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/foreach.html">foreach</a> <span class="i">$dot</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%</span>{<span class="i">$self</span>-&gt;{<span class="w">LIST</span>}}<span class="s">)</span> <span class="s">{</span></li><li>         <span class="i">$self</span><span class="i">-&gt;DELETE</span><span class="s">(</span><span class="i">$dot</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="EXISTS-this%2c-key"></a><b>EXISTS this, key
</b>
<p>This method is triggered when the user uses the exists() function
on a particular hash.  In our example, we'll look at the <code class="inline"><span class="s">{</span><span class="w">LIST</span><span class="s">}</span></code>

hash element for this:</p>
<pre class="verbatim"><ol><li><a name="EXISTS"></a>    sub <span class="m">EXISTS</span>   <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$dot</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> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$self</span>-&gt;{<span class="w">LIST</span>}-&gt;{<span class="i">$dot</span>}<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="FIRSTKEY-this"></a><b>FIRSTKEY this
</b>
<p>This method will be triggered when the user is going
to iterate through the hash, such as via a keys(), values(), or each() call.</p>
<pre class="verbatim"><ol><li><a name="FIRSTKEY"></a>    sub <span class="m">FIRSTKEY</span> <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$DEBUG</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%</span>{<span class="i">$self</span>-&gt;{<span class="w">LIST</span>}}<span class="sc">;</span>  <span class="c"># reset each() iterator</span></li><li>	<a class="l_k" href="functions/each.html">each</a> <span class="i">%</span>{<span class="i">$self</span>-&gt;{<span class="w">LIST</span>}}</li><li>    <span class="s">}</span></li></ol></pre><p>FIRSTKEY is always called in scalar context and it should just
return the first key.  values(), and each() in list context,
will call FETCH for the returned keys.</p>
</li>
<li><a name="NEXTKEY-this%2c-lastkey"></a><b>NEXTKEY this, lastkey
</b>
<p>This method gets triggered during a keys(), values(), or each() iteration.  It has a
second argument which is the last key that had been accessed.  This is
useful if you're caring about ordering or calling the iterator from more
than one sequence, or not really storing things in a hash anywhere.</p>
<p>NEXTKEY is always called in scalar context and it should just
return the next key.  values(), and each() in list context,
will call FETCH for the returned keys.</p>
<p>For our example, we're using a real hash so we'll do just the simple
thing, but we'll have to go through the LIST field indirectly.</p>
<pre class="verbatim"><ol><li><a name="NEXTKEY"></a>    sub <span class="m">NEXTKEY</span>  <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</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> <a class="l_k" href="functions/each.html">each</a> <span class="i">%</span>{ <span class="i">$self</span>-&gt;{<span class="w">LIST</span>} }</li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="SCALAR-this"></a><b>SCALAR this
</b>
<p>This is called when the hash is evaluated in scalar context. In order
to mimic the behaviour of untied hashes, this method should return a
false value when the tied hash is considered empty. If this method does
not exist, perl will make some educated guesses and return true when
the hash is inside an iteration. If this isn't the case, FIRSTKEY is
called, and the result will be a false value if FIRSTKEY returns the empty
list, true otherwise.</p>
<p>However, you should <b>not</b> blindly rely on perl always doing the right 
thing. Particularly, perl will mistakenly return true when you clear the 
hash by repeatedly calling DELETE until it is empty. You are therefore 
advised to supply your own SCALAR method when you want to be absolutely 
sure that your hash behaves nicely in scalar context.</p>
<p>In our example we can just call <code class="inline"><a class="l_k" href="functions/scalar.html">scalar</a></code> on the underlying hash
referenced by <code class="inline"><span class="i">$self</span>-&gt;{<span class="w">LIST</span>}</code>
:</p>
<pre class="verbatim"><ol><li><a name="SCALAR"></a>    sub <span class="m">SCALAR</span> <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</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> <a class="l_k" href="functions/scalar.html">scalar</a> <span class="i">%</span>{ <span class="i">$self</span>-&gt;{<span class="w">LIST</span>} }</li><li>    <span class="s">}</span></li></ol></pre><p>NOTE: In perl 5.25 the behavior of scalar %hash on an untied hash changed
to return the count of keys. Prior to this it returned a string containing
information about the bucket setup of the hash. See
<a href="Hash/Util.html#bucket_ratio">bucket_ratio in Hash::Util</a> for a backwards compatibility path.</p>
</li>
<li><a name="UNTIE-this"></a><b>UNTIE this
</b>
<p>This is called when <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> occurs.  See <a href="#The-untie-Gotcha">The untie Gotcha</a> below.</p>
</li>
<li><a name="DESTROY-this"></a><b>DESTROY this
</b>
<p>This method is triggered when a tied hash is about to go out of
scope.  You don't really need it unless you're trying to add debugging
or have auxiliary state to clean up.  Here's a very simple function:</p>
<pre class="verbatim"><ol><li><a name="DESTROY"></a>    sub <span class="m">DESTROY</span>  <span class="s">{</span></li><li>	<span class="w">carp</span> <span class="i">&amp;whowasi</span> if <span class="i">$DEBUG</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
</ul>
<p>Note that functions such as keys() and values() may return huge lists
when used on large objects, like DBM files.  You may prefer to use the
each() function to iterate over such.  Example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># print out history file offsets</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">NDBM_File</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a><span class="s">(</span><span class="i">%HIST</span><span class="cm">,</span> <span class="q">&#39;NDBM_File&#39;</span><span class="cm">,</span> <span class="q">&#39;/usr/lib/news/history&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="s">(</span><span class="i">$key</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span> = <a class="l_k" href="functions/each.html">each</a> <span class="i">%HIST</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$key</span><span class="cm">,</span> <span class="q">&#39; = &#39;</span><span class="cm">,</span> <a class="l_k" href="functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&#39;L&#39;</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/untie.html">untie</a><span class="s">(</span><span class="i">%HIST</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Tying-FileHandles"></a><h2>Tying FileHandles
</h2>
<p>This is partially implemented now.</p>
<p>A class implementing a tied filehandle should define the following
methods: TIEHANDLE, at least one of PRINT, PRINTF, WRITE, READLINE, GETC,
READ, and possibly CLOSE, UNTIE and DESTROY.  The class can also provide: BINMODE,
OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators are
used on the handle.</p>
<p>When STDERR is tied, its PRINT method will be called to issue warnings
and error messages.  This feature is temporarily disabled during the call, 
which means you can use <code class="inline"><a class="l_k" href="functions/warn.html">warn()</a></code> inside PRINT without starting a recursive
loop.  And just like <code class="inline"><span class="w">__WARN__</span></code>
 and <code class="inline"><span class="w">__DIE__</span></code>
 handlers, STDERR's PRINT
method may be called to report parser errors, so the caveats mentioned under 
<a href="perlvar.html#%25SIG">%SIG in perlvar</a> apply.</p>
<p>All of this is especially useful when perl is embedded in some other 
program, where output to STDOUT and STDERR may have to be redirected 
in some special way.  See nvi and the Apache module for examples.</p>
<p>When tying a handle, the first argument to <code class="inline"><a class="l_k" href="functions/tie.html">tie</a></code> should begin with an
asterisk.  So, if you are tying STDOUT, use <code class="inline"><span class="i">*STDOUT</span></code>
.  If you have
assigned it to a scalar variable, say <code class="inline"><span class="i">$handle</span></code>
, use <code class="inline"><span class="i">*$handle</span></code>
.
<code class="inline"><a class="l_k" href="functions/tie.html">tie</a> <span class="i">$handle</span></code>
 ties the scalar variable <code class="inline"><span class="i">$handle</span></code>
, not the handle inside
it.</p>
<p>In our example we're going to create a shouting handle.</p>
<pre class="verbatim"><ol><li><a name="package-Shout"></a>    package <span class="i">Shout</span><span class="sc">;</span></li></ol></pre><ul>
<li><a name="TIEHANDLE-classname%2c-LIST"></a><b>TIEHANDLE classname, LIST
</b>
<p>This is the constructor for the class.  That means it is expected to
return a blessed reference of some sort. The reference can be used to
hold some internal information.</p>
<pre class="verbatim"><ol><li><a name="TIEHANDLE"></a>    sub <span class="m">TIEHANDLE</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&lt;shout&gt;\n&quot;</span><span class="sc">;</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$i</span><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a> <span class="s">}</span></li></ol></pre></li>
<li><a name="WRITE-this%2c-LIST"></a><b>WRITE this, LIST
</b>
<p>This method will be called when the handle is written to via the
<code class="inline"><a class="l_k" href="functions/syswrite.html">syswrite</a></code> function.</p>
<pre class="verbatim"><ol><li><a name="WRITE"></a> sub <span class="m">WRITE</span> <span class="s">{</span></li><li>     <span class="i">$r</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">$buf</span><span class="cm">,</span><span class="i">$len</span><span class="cm">,</span><span class="i">$offset</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;WRITE called, \$buf=$buf, \$len=$len, \$offset=$offset&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="PRINT-this%2c-LIST"></a><b>PRINT this, LIST
</b>
<p>This method will be triggered every time the tied handle is printed to
with the <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code> or <code class="inline"><a class="l_k" href="functions/say.html">say()</a></code> functions.  Beyond its self reference
it also expects the list that was passed to the print function.</p>
<pre class="verbatim"><ol><li><a name="PRINT"></a>  sub <span class="m">PRINT</span> <span class="s">{</span> <span class="i">$r</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <span class="i">$$r</span>++<span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="i">$,</span><span class="cm">,</span><a class="l_k" href="functions/map.html">map</a><span class="s">(</span><a class="l_k" href="functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span><span class="i">@_</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span><span class="i">$\</span> <span class="s">}</span></li></ol></pre><p><code class="inline"><a class="l_k" href="functions/say.html">say()</a></code> acts just like <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code> except $\ will be localized to <code class="inline">\<span class="w">n</span></code>
 so
you need do nothing special to handle <code class="inline"><a class="l_k" href="functions/say.html">say()</a></code> in <code class="inline"><span class="i">PRINT</span><span class="s">(</span><span class="s">)</span></code>
.</p>
</li>
<li><a name="PRINTF-this%2c-LIST"></a><b>PRINTF this, LIST
</b>
<p>This method will be triggered every time the tied handle is printed to
with the <code class="inline"><a class="l_k" href="functions/printf.html">printf()</a></code> function.
Beyond its self reference it also expects the format and list that was
passed to the printf function.</p>
<pre class="verbatim"><ol><li><a name="PRINTF"></a>    sub <span class="m">PRINTF</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$fmt</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="i">$fmt</span><span class="cm">,</span> <span class="i">@_</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="READ-this%2c-LIST"></a><b>READ this, LIST
</b>
<p>This method will be called when the handle is read from via the <code class="inline"><a class="l_k" href="functions/read.html">read</a></code>
or <code class="inline"><a class="l_k" href="functions/sysread.html">sysread</a></code> functions.</p>
<pre class="verbatim"><ol><li><a name="READ"></a> sub <span class="m">READ</span> <span class="s">{</span></li><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">$bufref</span> = \<span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span></li><li>   <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span><span class="i">$len</span><span class="cm">,</span><span class="i">$offset</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>   <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;READ called, \$buf=$bufref, \$len=$len, \$offset=$offset&quot;</span><span class="sc">;</span></li><li>   <span class="c"># add to $$bufref, set $len to number of characters read</span></li><li>   <span class="i">$len</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="READLINE-this"></a><b>READLINE this
</b>
<p>This method is called when the handle is read via <code class="inline"><span class="q">&lt;HANDLE&gt;</span></code>

or <code class="inline"><a class="l_k" href="functions/readline.html">readline</a> <span class="w">HANDLE</span></code>
.</p>
<p>As per <a href="functions/readline.html">readline</a>, in scalar context it should return
the next line, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> for no more data.  In list context it should
return all remaining lines, or an empty list for no more data.  The strings
returned should include the input record separator <code class="inline"><span class="i">$/</span></code>
 (see <a href="perlvar.html">perlvar</a>),
unless it is <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> (which means "slurp" mode).</p>
<pre class="verbatim"><ol><li><a name="READLINE"></a>    sub <span class="m">READLINE</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$r</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      if <span class="s">(</span><a class="l_k" href="functions/wantarray.html">wantarray</a><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="q">&quot;all remaining\n&quot;</span><span class="cm">,</span></li><li>                <span class="q">&quot;lines up\n&quot;</span><span class="cm">,</span></li><li>                <span class="q">&quot;to eof\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>      <span class="s">}</span> else <span class="s">{</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="q">&quot;READLINE called &quot;</span> . ++<span class="i">$$r</span> . <span class="q">&quot; times\n&quot;</span><span class="sc">;</span></li><li>      <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre></li>
<li><a name="GETC-this"></a><b>GETC this
</b>
<p>This method will be called when the <code class="inline"><a class="l_k" href="functions/getc.html">getc</a></code> function is called.</p>
<pre class="verbatim"><ol><li><a name="GETC"></a>    sub <span class="m">GETC</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Don&#39;t GETC, Get Perl&quot;</span><span class="sc">;</span> <a class="l_k" href="functions/return.html">return</a> <span class="q">&quot;a&quot;</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="EOF-this"></a><b>EOF this
</b>
<p>This method will be called when the <code class="inline"><a class="l_k" href="functions/eof.html">eof</a></code> function is called.</p>
<p>Starting with Perl 5.12, an additional integer parameter will be passed.  It
will be zero if <code class="inline"><a class="l_k" href="functions/eof.html">eof</a></code> is called without parameter; <code class="inline"><span class="n">1</span></code>
 if <code class="inline"><a class="l_k" href="functions/eof.html">eof</a></code> is given
a filehandle as a parameter, e.g. <code class="inline"><a class="l_k" href="functions/eof.html">eof(FH)</a></code>; and <code class="inline"><span class="n">2</span></code>
 in the very special
case that the tied filehandle is <code class="inline"><span class="w">ARGV</span></code>
 and <code class="inline"><a class="l_k" href="functions/eof.html">eof</a></code> is called with an empty
parameter list, e.g. <code class="inline"><a class="l_k" href="functions/eof.html">eof()</a></code>.</p>
<pre class="verbatim"><ol><li><a name="EOF"></a>    sub <span class="m">EOF</span> <span class="s">{</span> not <a class="l_k" href="functions/length.html">length</a> <span class="i">$stringbuf</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="CLOSE-this"></a><b>CLOSE this
</b>
<p>This method will be called when the handle is closed via the <code class="inline"><a class="l_k" href="functions/close.html">close</a></code>
function.</p>
<pre class="verbatim"><ol><li><a name="CLOSE"></a>    sub <span class="m">CLOSE</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;CLOSE called.\n&quot;</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="UNTIE-this"></a><b>UNTIE this
</b>
<p>As with the other types of ties, this method will be called when <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> happens.
It may be appropriate to "auto CLOSE" when this occurs.  See
<a href="#The-untie-Gotcha">The untie Gotcha</a> below.</p>
</li>
<li><a name="DESTROY-this"></a><b>DESTROY this
</b>
<p>As with the other types of ties, this method will be called when the
tied handle is about to be destroyed. This is useful for debugging and
possibly cleaning up.</p>
<pre class="verbatim"><ol><li><a name="DESTROY"></a>    sub <span class="m">DESTROY</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&lt;/shout&gt;\n&quot;</span> <span class="s">}</span></li></ol></pre></li>
</ul>
<p>Here's how to use our little example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/tie.html">tie</a><span class="s">(</span><span class="i">*FOO</span><span class="cm">,</span><span class="q">&#39;Shout&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">FOO</span> <span class="q">&quot;hello\n&quot;</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="n">4</span><span class="sc">;</span> <span class="i">$b</span> = <span class="n">6</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">FOO</span> <span class="i">$a</span><span class="cm">,</span> <span class="q">&quot; plus &quot;</span><span class="cm">,</span> <span class="i">$b</span><span class="cm">,</span> <span class="q">&quot; equals &quot;</span><span class="cm">,</span> <span class="i">$a</span> + <span class="i">$b</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&lt;FOO&gt;</span><span class="sc">;</span></li></ol></pre><a name="UNTIE-this"></a><h2>UNTIE this
</h2>
<p>You can define for all tie types an UNTIE method that will be called
at untie().  See <a href="#The-untie-Gotcha">The untie Gotcha</a> below.</p>
<a name="The-untie-Gotcha"></a><h2>The <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> Gotcha
</h2>
<p>If you intend making use of the object returned from either tie() or
tied(), and if the tie's target class defines a destructor, there is a
subtle gotcha you <i>must</i> guard against.</p>
<p>As setup, consider this (admittedly rather contrived) example of a
tie; all it does is use a file to keep a log of the values assigned to
a scalar.</p>
<pre class="verbatim"><ol><li><a name="package-Remember"></a>    package <span class="i">Remember</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>    <a class="l_k" href="functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li></li><li><a name="TIESCALAR"></a>    sub <span class="m">TIESCALAR</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="i">$filename</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="q">&quot;&gt; $filename&quot;</span> <span class="s">)</span></li><li>                         or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Cannot open $filename: $!\n&quot;</span><span class="sc">;</span></li><li></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$handle</span> <span class="q">&quot;The Start\n&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span><span class="w">FH</span> <span class="cm">=&gt;</span> <span class="i">$handle</span><span class="cm">,</span> <span class="w">Value</span> <span class="cm">=&gt;</span> <span class="n">0</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><li></li><li><a name="FETCH"></a>    sub <span class="m">FETCH</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">$self</span>-&gt;{<span class="w">Value</span>}<span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="STORE"></a>    sub <span class="m">STORE</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span> = <span class="i">$self</span>-&gt;{<span class="w">FH</span>}<span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$handle</span> <span class="q">&quot;$value\n&quot;</span><span class="sc">;</span></li><li>        <span class="i">$self</span>-&gt;{<span class="w">Value</span>} = <span class="i">$value</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></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span> = <span class="i">$self</span>-&gt;{<span class="w">FH</span>}<span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$handle</span> <span class="q">&quot;The End\n&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/close.html">close</a> <span class="i">$handle</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>Here is an example that makes use of this tie:</p>
<pre class="verbatim"><ol><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">Remember</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$fred</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$fred</span><span class="cm">,</span> <span class="q">&#39;Remember&#39;</span><span class="cm">,</span> <span class="q">&#39;myfile.txt&#39;</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">4</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">5</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/untie.html">untie</a> <span class="i">$fred</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;cat myfile.txt&quot;</span><span class="sc">;</span></li></ol></pre><p>This is the output when it is executed:</p>
<pre class="verbatim"><ol><li>    The Start</li><li>    1</li><li>    4</li><li>    5</li><li>    The End</li></ol></pre><p>So far so good.  Those of you who have been paying attention will have
spotted that the tied object hasn't been used so far.  So lets add an
extra method to the Remember class to allow comments to be included in
the file; say, something like this:</p>
<pre class="verbatim"><ol><li><a name="comment"></a>    sub <span class="m">comment</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$text</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span> = <span class="i">$self</span>-&gt;{<span class="w">FH</span>}<span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$handle</span> <span class="i">$text</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>And here is the previous example modified to use the <code class="inline"><span class="w">comment</span></code>
 method
(which requires the tied object):</p>
<pre class="verbatim"><ol><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">Remember</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$fred</span><span class="cm">,</span> <span class="i">$x</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$x</span> = <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$fred</span><span class="cm">,</span> <span class="q">&#39;Remember&#39;</span><span class="cm">,</span> <span class="q">&#39;myfile.txt&#39;</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">4</span><span class="sc">;</span></li><li>    <span class="w">comment</span> <span class="i">$x</span> <span class="q">&quot;changing...&quot;</span><span class="sc">;</span></li><li>    <span class="i">$fred</span> = <span class="n">5</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/untie.html">untie</a> <span class="i">$fred</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;cat myfile.txt&quot;</span><span class="sc">;</span></li></ol></pre><p>When this code is executed there is no output.  Here's why:</p>
<p>When a variable is tied, it is associated with the object which is the
return value of the TIESCALAR, TIEARRAY, or TIEHASH function.  This
object normally has only one reference, namely, the implicit reference
from the tied variable.  When untie() is called, that reference is
destroyed.  Then, as in the first example above, the object's
destructor (DESTROY) is called, which is normal for objects that have
no more valid references; and thus the file is closed.</p>
<p>In the second example, however, we have stored another reference to
the tied object in $x.  That means that when untie() gets called
there will still be a valid reference to the object in existence, so
the destructor is not called at that time, and thus the file is not
closed.  The reason there is no output is because the file buffers
have not been flushed to disk.</p>
<p>Now that you know what the problem is, what can you do to avoid it?
Prior to the introduction of the optional UNTIE method the only way
was the good old <code class="inline">-w</code>
 flag. Which will spot any instances where you call
untie() and there are still valid references to the tied object.  If
the second script above this near the top <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;untie&#39;</span></code>

or was run with the <code class="inline">-w</code>
 flag, Perl prints this
warning message:</p>
<pre class="verbatim"><ol><li>    untie attempted while 1 inner references still exist</li></ol></pre><p>To get the script to work properly and silence the warning make sure
there are no valid references to the tied object <i>before</i> untie() is
called:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/undef.html">undef</a> <span class="i">$x</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/untie.html">untie</a> <span class="i">$fred</span><span class="sc">;</span></li></ol></pre><p>Now that UNTIE exists the class designer can decide which parts of the
class functionality are really associated with <code class="inline"><a class="l_k" href="functions/untie.html">untie</a></code> and which with
the object being destroyed. What makes sense for a given class depends
on whether the inner references are being kept so that non-tie-related
methods can be called on the object. But in most cases it probably makes
sense to move the functionality that would have been in DESTROY to the UNTIE
method.</p>
<p>If the UNTIE method exists then the warning above does not occur. Instead the
UNTIE method is passed the count of "extra" references and can issue its own
warning if appropriate. e.g. to replicate the no UNTIE case this method can
be used:</p>
<pre class="verbatim"><ol><li><a name="UNTIE"></a> sub <span class="m">UNTIE</span></li><li> <span class="s">{</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$obj</span><span class="cm">,</span><span class="i">$count</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>  <span class="w">carp</span> <span class="q">&quot;untie attempted while $count inner references still exist&quot;</span></li><li>                                                              <a class="l_k" href="functions/if.html">if</a> <span class="i">$count</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>See <a href="DB_File.html">DB_File</a> or <a href="Config.html">Config</a> for some interesting tie() implementations.
A good starting point for many tie() implementations is with one of the
modules <a href="Tie/Scalar.html">Tie::Scalar</a>, <a href="Tie/Array.html">Tie::Array</a>, <a href="Tie/Hash.html">Tie::Hash</a>, or <a href="Tie/Handle.html">Tie::Handle</a>.</p>
<a name="BUGS"></a><h1>BUGS</h1>
<p>The normal return provided by <code class="inline"><a class="l_k" href="functions/scalar.html">scalar(%hash)</a></code> is not
available.  What this means is that using %tied_hash in boolean
context doesn't work right (currently this always tests false,
regardless of whether the hash is empty or hash elements).
[ This paragraph needs review in light of changes in 5.25 ]</p>
<p>Localizing tied arrays or hashes does not work.  After exiting the
scope the arrays or the hashes are not restored.</p>
<p>Counting the number of entries in a hash via <code class="inline"><a class="l_k" href="functions/scalar.html">scalar(keys(%hash))</a></code>
or <code class="inline"><a class="l_k" href="functions/scalar.html">scalar(values(%hash)</a></code>) is inefficient since it needs to iterate
through all the entries with FIRSTKEY/NEXTKEY.</p>
<p>Tied hash/array slices cause multiple FETCH/STORE pairs, there are no
tie methods for slice operations.</p>
<p>You cannot easily tie a multilevel data structure (such as a hash of
hashes) to a dbm file.  The first problem is that all but GDBM and
Berkeley DB have size limitations, but beyond that, you also have problems
with how references are to be represented on disk.  One
module that does attempt to address this need is DBM::Deep.  Check your
nearest CPAN site as described in <a href="perlmodlib.html">perlmodlib</a> for source code.  Note
that despite its name, DBM::Deep does not use dbm.  Another earlier attempt
at solving the problem is MLDBM, which is also available on the CPAN, but
which has some fairly serious limitations.</p>
<p>Tied filehandles are still incomplete.  sysopen(), truncate(),
flock(), fcntl(), stat() and -X can't currently be trapped.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Tom Christiansen</p>
<p>TIEHANDLE by Sven Verdoolaege &lt;<i>skimo@dns.ufsia.ac.be</i>&gt; and Doug MacEachern &lt;<i>dougm@osf.org</i>&gt;</p>
<p>UNTIE by Nick Ing-Simmons &lt;<i>nick@ing-simmons.net</i>&gt;</p>
<p>SCALAR by Tassilo von Parseval &lt;<i>tassilo.von.parseval@rwth-aachen.de</i>&gt;</p>
<p>Tying Arrays by Casey West &lt;<i>casey@geeknest.com</i>&gt;</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="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Tying-Scalars">Tying Scalars
</a><li><a href="#Tying-Arrays">Tying Arrays
</a><li><a href="#Tying-Hashes">Tying Hashes
</a><li><a href="#Tying-FileHandles">Tying FileHandles
</a><li><a href="#UNTIE-this">UNTIE this
</a><li><a href="#The-untie-Gotcha">The untie Gotcha
</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#BUGS">BUGS</a><li><a href="#AUTHOR">AUTHOR</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    = 'perltie';
  perldoc.pageAddress = 'perltie.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>