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

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


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#History-and-Background">History and Background</a><li><a href="#Basic-Structure">Basic Structure</a><li><a href="#Layers-vs-Disciplines">Layers vs Disciplines</a><li><a href="#Data-Structures">Data Structures</a><li><a href="#Functions-and-Attributes">Functions and Attributes</a><li><a href="#Per-instance-Data">Per-instance Data</a><li><a href="#Layers-in-action.">Layers in action.</a><li><a href="#Per-instance-flag-bits">Per-instance flag bits</a><li><a href="#Methods-in-Detail">Methods in Detail</a><li><a href="#Utilities">Utilities</a><li><a href="#Implementing-PerlIO-Layers">Implementing PerlIO Layers</a><li><a href="#Core-Layers">Core Layers</a><li><a href="#Extension-Layers">Extension Layers</a></ul><li><a href="#TODO">TODO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perliol - C API for Perl's implementation of IO in Layers.</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>    <span class="q">/* Defining a layer ... */</span></li><li>    <span class="c">#include &lt;perliol.h&gt;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document describes the behavior and implementation of the PerlIO
abstraction described in <a href="perlapio.html">perlapio</a> when <code class="inline"><span class="w">USE_PERLIO</span></code>
 is defined.</p>
<a name="History-and-Background"></a><h2>History and Background</h2>
<p>The PerlIO abstraction was introduced in perl5.003_02 but languished as
just an abstraction until perl5.7.0. However during that time a number
of perl extensions switched to using it, so the API is mostly fixed to
maintain (source) compatibility.</p>
<p>The aim of the implementation is to provide the PerlIO API in a flexible
and platform neutral manner. It is also a trial of an "Object Oriented
C, with vtables" approach which may be applied to Perl 6.</p>
<a name="Basic-Structure"></a><h2>Basic Structure</h2>
<p>PerlIO is a stack of layers.</p>
<p>The low levels of the stack work with the low-level operating system
calls (file descriptors in C) getting bytes in and out, the higher
layers of the stack buffer, filter, and otherwise manipulate the I/O,
and return characters (or bytes) to Perl.  Terms <i>above</i> and <i>below</i>
are used to refer to the relative positioning of the stack layers.</p>
<p>A layer contains a "vtable", the table of I/O operations (at C level
a table of function pointers), and status flags.  The functions in the
vtable implement operations like "open", "read", and "write".</p>
<p>When I/O, for example "read", is requested, the request goes from Perl
first down the stack using "read" functions of each layer, then at the
bottom the input is requested from the operating system services, then
the result is returned up the stack, finally being interpreted as Perl
data.</p>
<p>The requests do not necessarily go always all the way down to the
operating system: that's where PerlIO buffering comes into play.</p>
<p>When you do an open() and specify extra PerlIO layers to be deployed,
the layers you specify are "pushed" on top of the already existing
default stack.  One way to see it is that "operating system is
on the left" and "Perl is on the right".</p>
<p>What exact layers are in this default stack depends on a lot of
things: your operating system, Perl version, Perl compile time
configuration, and Perl runtime configuration.  See <a href="PerlIO.html">PerlIO</a>,
<a href="perlrun.html#PERLIO">PERLIO in perlrun</a>, and <a href="open.html">open</a> for more information.</p>
<p>binmode() operates similarly to open(): by default the specified
layers are pushed on top of the existing stack.</p>
<p>However, note that even as the specified layers are "pushed on top"
for open() and binmode(), this doesn't mean that the effects are
limited to the "top": PerlIO layers can be very 'active' and inspect
and affect layers also deeper in the stack.  As an example there
is a layer called "raw" which repeatedly "pops" layers until
it reaches the first layer that has declared itself capable of
handling binary data.  The "pushed" layers are processed in left-to-right
order.</p>
<p>sysopen() operates (unsurprisingly) at a lower level in the stack than
open().  For example in Unix or Unix-like systems sysopen() operates
directly at the level of file descriptors: in the terms of PerlIO
layers, it uses only the "unix" layer, which is a rather thin wrapper
on top of the Unix file descriptors.</p>
<a name="Layers-vs-Disciplines"></a><h2>Layers vs Disciplines</h2>
<p>Initial discussion of the ability to modify IO streams behaviour used
the term "discipline" for the entities which were added. This came (I
believe) from the use of the term in "sfio", which in turn borrowed it
from "line disciplines" on Unix terminals. However, this document (and
the C code) uses the term "layer".</p>
<p>This is, I hope, a natural term given the implementation, and should
avoid connotations that are inherent in earlier uses of "discipline"
for things which are rather different.</p>
<a name="Data-Structures"></a><h2>Data Structures</h2>
<p>The basic data structure is a PerlIOl:</p>
<pre class="verbatim"><ol><li>	<span class="w">typedef</span> <span class="w">struct</span> <span class="w">_PerlIO</span> <span class="w">PerlIOl</span><span class="sc">;</span></li><li>	<span class="w">typedef</span> <span class="w">struct</span> <span class="w">_PerlIO_funcs</span> <span class="w">PerlIO_funcs</span><span class="sc">;</span></li><li>	<span class="w">typedef</span> <span class="w">PerlIOl</span> *<span class="w">PerlIO</span><span class="sc">;</span></li><li></li><li>	<span class="w">struct</span> <span class="w">_PerlIO</span></li><li>	<span class="s">{</span></li><li>	 <span class="w">PerlIOl</span> *	<a class="l_k" href="functions/next.html">next</a><span class="sc">;</span>       <span class="q">/* Lower layer */</span></li><li>	 <span class="w">PerlIO_funcs</span> *	<span class="w">tab</span><span class="sc">;</span>        <span class="q">/* Functions for this layer */</span></li><li>	 <span class="w">U32</span>		<span class="w">flags</span><span class="sc">;</span>      <span class="q">/* Various flags for state */</span></li><li>	<span class="s">}</span><span class="sc">;</span></li></ol></pre><p>A <code class="inline"><span class="w">PerlIOl</span> *</code>
 is a pointer to the struct, and the <i>application</i>
level <code class="inline"><span class="w">PerlIO</span> *</code>
 is a pointer to a <code class="inline"><span class="w">PerlIOl</span> *</code>
 - i.e. a pointer
to a pointer to the struct. This allows the application level <code class="inline"><span class="w">PerlIO</span> *</code>

to remain constant while the actual <code class="inline"><span class="w">PerlIOl</span> *</code>
 underneath
changes. (Compare perl's <code class="inline"><span class="w">SV</span> *</code>
 which remains constant while its
<code class="inline"><span class="w">sv_any</span></code>
 field changes as the scalar's type changes.) An IO stream is
then in general represented as a pointer to this linked-list of
"layers".</p>
<p>It should be noted that because of the double indirection in a <code class="inline"><span class="w">PerlIO</span> *</code>
,
a <code class="inline">&amp;(perlio-&gt;next)</code> "is" a <code class="inline"><span class="w">PerlIO</span> *</code>
, and so to some degree
at least one layer can use the "standard" API on the next layer down.</p>
<p>A "layer" is composed of two parts:</p>
<dl>
<dt>1.</dt><dd>
<p>The functions and attributes of the "layer class".</p>
</dd>
<dt>2.</dt><dd>
<p>The per-instance data for a particular handle.</p>
</dd>
</dl>
<a name="Functions-and-Attributes"></a><h2>Functions and Attributes</h2>
<p>The functions and attributes are accessed via the "tab" (for table)
member of <code class="inline"><span class="w">PerlIOl</span></code>
. The functions (methods of the layer "class") are
fixed, and are defined by the <code class="inline"><span class="w">PerlIO_funcs</span></code>
 type. They are broadly the
same as the public <code class="inline"><span class="w">PerlIO_xxxxx</span></code>
 functions:</p>
<pre class="verbatim"><ol><li> <span class="w">struct</span> <span class="w">_PerlIO_funcs</span></li><li> <span class="s">{</span></li><li>  <span class="w">Size_t</span>     <span class="w">fsize</span><span class="sc">;</span></li><li>  <span class="w">char</span> *     <span class="w">name</span><span class="sc">;</span></li><li>  <span class="w">Size_t</span>     <span class="w">size</span><span class="sc">;</span></li><li>  <span class="w">IV</span>         <span class="w">kind</span><span class="sc">;</span></li><li>  <span class="w">IV</span>         <span class="s">(</span><span class="i">*Pushed</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="cm">,</span></li><li>                             <span class="w">const</span> <span class="w">char</span> *<span class="w">mode</span><span class="cm">,</span></li><li>                             <span class="w">SV</span> *<span class="w">arg</span><span class="cm">,</span></li><li>                             <span class="w">PerlIO_funcs</span> *<span class="w">tab</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">IV</span>         <span class="s">(</span><span class="i">*Popped</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">PerlIO</span> *   <span class="s">(</span><span class="i">*Open</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO_funcs</span> *<span class="w">tab</span><span class="cm">,</span></li><li>                           <span class="w">PerlIO_list_t</span> *<span class="w">layers</span><span class="cm">,</span> <span class="w">IV</span> <span class="w">n</span><span class="cm">,</span></li><li>                           <span class="w">const</span> <span class="w">char</span> *<span class="w">mode</span><span class="cm">,</span></li><li>                           <a class="l_k" href="functions/int.html">int</a> <span class="w">fd</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">imode</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">perm</span><span class="cm">,</span></li><li>                           <span class="w">PerlIO</span> *<span class="w">old</span><span class="cm">,</span></li><li>                           <a class="l_k" href="functions/int.html">int</a> <span class="w">narg</span><span class="cm">,</span> <span class="w">SV</span> **<span class="w">args</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">IV</span>         <span class="s">(</span><span class="i">*Binmode</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">SV</span> *       <span class="s">(</span><span class="i">*Getarg</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="cm">,</span> <span class="w">CLONE_PARAMS</span> *<span class="w">param</span><span class="cm">,</span> <a class="l_k" href="functions/int.html">int</a> <span class="w">flags</span><span class="s">)</span></li><li>  <span class="w">IV</span>         <span class="s">(</span><span class="i">*Fileno</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="w">PerlIO</span> *   <span class="s">(</span><span class="i">*Dup</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="cm">,</span></li><li>                          <span class="w">PerlIO</span> *<span class="w">o</span><span class="cm">,</span></li><li>                          <span class="w">CLONE_PARAMS</span> *<span class="w">param</span><span class="cm">,</span></li><li>                          <a class="l_k" href="functions/int.html">int</a> <span class="w">flags</span><span class="s">)</span></li><li>  /<span class="i">* Unix</span>-<span class="w">like</span> <span class="w">functions</span> - <span class="w">cf</span> <span class="w">sfio</span> <span class="w">line</span> <span class="w">disciplines</span> *<span class="q">/</span></li><li>  <span class="q">  SSize_t    (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);</span></li><li>  <span class="q">  SSize_t    (*Unread)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);</span></li><li>  <span class="q">  SSize_t    (*Write)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);</span></li><li>  <span class="q">  IV         (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);</span></li><li>  <span class="q">  Off_t      (*Tell)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  IV         (*Close)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  /</span>* <span class="w">Stdio</span>-<span class="w">like</span> <span class="w">buffered</span> <span class="w">IO</span> <span class="w">functions</span> *<span class="q">/</span></li><li>  <span class="q">  IV         (*Flush)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  IV         (*Fill)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  IV         (*Eof)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  IV         (*Error)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  void       (*Clearerr)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  void       (*Setlinebuf)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  /</span>* <span class="w">Perl&#39;s</span> <span class="w">snooping</span> <span class="w">functions</span> *<span class="q">/</span></li><li>  <span class="q">  STDCHAR *  (*Get_base)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  Size_t     (*Get_bufsiz)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  STDCHAR *  (*Get_ptr)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  SSize_t    (*Get_cnt)(pTHX_ PerlIO *f);</span></li><li>  <span class="q">  void       (*Set_ptrcnt)(pTHX_ PerlIO *f,STDCHAR *ptr,SSize_t cnt);</span></li><li> <span class="q"> };</span></li></ol></pre><p>The first few members of the struct give a function table size for
compatibility check "name" for the layer, the  size to <code class="inline"><span class="w">malloc</span></code>
 for the per-instance data,
and some flags which are attributes of the class as whole (such as whether it is a buffering
layer), then follow the functions which fall into four basic groups:</p>
<dl>
<dt>1.</dt><dd>
<p>Opening and setup functions</p>
</dd>
<dt>2.</dt><dd>
<p>Basic IO operations</p>
</dd>
<dt>3.</dt><dd>
<p>Stdio class buffering options.</p>
</dd>
<dt>4.</dt><dd>
<p>Functions to support Perl's traditional "fast" access to the buffer.</p>
</dd>
</dl>
<p>A layer does not have to implement all the functions, but the whole
table has to be present. Unimplemented slots can be NULL (which will
result in an error when called) or can be filled in with stubs to
"inherit" behaviour from a "base class". This "inheritance" is fixed
for all instances of the layer, but as the layer chooses which stubs
to populate the table, limited "multiple inheritance" is possible.</p>
<a name="Per-instance-Data"></a><h2>Per-instance Data</h2>
<p>The per-instance data are held in memory beyond the basic PerlIOl
struct, by making a PerlIOl the first member of the layer's struct
thus:</p>
<pre class="verbatim"><ol><li>	typedef struct</li><li>	{</li><li>	 struct _PerlIO base;       /* Base "class" info */</li><li>	 STDCHAR *	buf;        /* Start of buffer */</li><li>	 STDCHAR *	end;        /* End of valid part of buffer */</li><li>	 STDCHAR *	ptr;        /* Current position in buffer */</li><li>	 Off_t		posn;       /* Offset of buf into the file */</li><li>	 Size_t		bufsiz;     /* Real size of buffer */</li><li>	 IV		oneword;    /* Emergency buffer */</li><li>	} PerlIOBuf;</li></ol></pre><p>In this way (as for perl's scalars) a pointer to a PerlIOBuf can be
treated as a pointer to a PerlIOl.</p>
<a name="Layers-in-action."></a><h2>Layers in action.</h2>
<pre class="verbatim"><ol><li>                table           perlio          unix</li><li>            |           |</li><li>            +-----------+    +----------+    +--------+</li><li>   PerlIO -&gt;|           |---&gt;|  next    |---&gt;|  NULL  |</li><li>            +-----------+    +----------+    +--------+</li><li>            |           |    |  buffer  |    |   fd   |</li><li>            +-----------+    |          |    +--------+</li><li>            |           |    +----------+</li></ol></pre><p>The above attempts to show how the layer scheme works in a simple case.
The application's <code class="inline"><span class="w">PerlIO</span> *</code>
 points to an entry in the table(s)
representing open (allocated) handles. For example the first three slots
in the table correspond to <code class="inline"><span class="w">stdin</span></code>
,<code class="inline"><span class="w">stdout</span></code>
 and <code class="inline"><span class="w">stderr</span></code>
. The table
in turn points to the current "top" layer for the handle - in this case
an instance of the generic buffering layer "perlio". That layer in turn
points to the next layer down - in this case the low-level "unix" layer.</p>
<p>The above is roughly equivalent to a "stdio" buffered stream, but with
much more flexibility:</p>
<ul>
<li>
<p>If Unix level <code class="inline"><a class="l_k" href="functions/read.html">read</a></code>/<code class="inline"><a class="l_k" href="functions/write.html">write</a></code>/<code class="inline"><span class="w">lseek</span></code>
 is not appropriate for (say)
sockets then the "unix" layer can be replaced (at open time or even
dynamically) with a "socket" layer.</p>
</li>
<li>
<p>Different handles can have different buffering schemes. The "top"
layer could be the "mmap" layer if reading disk files was quicker
using <code class="inline"><span class="w">mmap</span></code>
 than <code class="inline"><a class="l_k" href="functions/read.html">read</a></code>. An "unbuffered" stream can be implemented
simply by not having a buffer layer.</p>
</li>
<li>
<p>Extra layers can be inserted to process the data as it flows through.
This was the driving need for including the scheme in perl 5.7.0+ - we
needed a mechanism to allow data to be translated between perl's
internal encoding (conceptually at least Unicode as UTF-8), and the
"native" format used by the system. This is provided by the
":encoding(xxxx)" layer which typically sits above the buffering layer.</p>
</li>
<li>
<p>A layer can be added that does "\n" to CRLF translation. This layer
can be used on any platform, not just those that normally do such
things.</p>
</li>
</ul>
<a name="Per-instance-flag-bits"></a><h2>Per-instance flag bits</h2>
<p>The generic flag bits are a hybrid of <code class="inline"><span class="w">O_XXXXX</span></code>
 style flags deduced
from the mode string passed to <code class="inline"><span class="i">PerlIO_open</span><span class="s">(</span><span class="s">)</span></code>
, and state bits for
typical buffer layers.</p>
<ul>
<li><a name="PERLIO_F_EOF"></a><b>PERLIO_F_EOF</b>
<p>End of file.</p>
</li>
<li><a name="PERLIO_F_CANWRITE"></a><b>PERLIO_F_CANWRITE</b>
<p>Writes are permitted, i.e. opened as "w" or "r+" or "a", etc.</p>
</li>
<li><a name="PERLIO_F_CANREAD"></a><b>PERLIO_F_CANREAD</b>
<p>Reads are permitted i.e. opened "r" or "w+" (or even "a+" - ick).</p>
</li>
<li><a name="PERLIO_F_ERROR"></a><b>PERLIO_F_ERROR</b>
<p>An error has occurred (for <code class="inline"><span class="i">PerlIO_error</span><span class="s">(</span><span class="s">)</span></code>
).</p>
</li>
<li><a name="PERLIO_F_TRUNCATE"></a><b>PERLIO_F_TRUNCATE</b>
<p>Truncate file suggested by open mode.</p>
</li>
<li><a name="PERLIO_F_APPEND"></a><b>PERLIO_F_APPEND</b>
<p>All writes should be appends.</p>
</li>
<li><a name="PERLIO_F_CRLF"></a><b>PERLIO_F_CRLF</b>
<p>Layer is performing Win32-like "\n" mapped to CR,LF for output and CR,LF
mapped to "\n" for input. Normally the provided "crlf" layer is the only
layer that need bother about this. <code class="inline"><span class="i">PerlIO_binmode</span><span class="s">(</span><span class="s">)</span></code>
 will mess with this
flag rather than add/remove layers if the <code class="inline"><span class="w">PERLIO_K_CANCRLF</span></code>
 bit is set
for the layers class.</p>
</li>
<li><a name="PERLIO_F_UTF8"></a><b>PERLIO_F_UTF8</b>
<p>Data written to this layer should be UTF-8 encoded; data provided
by this layer should be considered UTF-8 encoded. Can be set on any layer
by ":utf8" dummy layer. Also set on ":encoding" layer.</p>
</li>
<li><a name="PERLIO_F_UNBUF"></a><b>PERLIO_F_UNBUF</b>
<p>Layer is unbuffered - i.e. write to next layer down should occur for
each write to this layer.</p>
</li>
<li><a name="PERLIO_F_WRBUF"></a><b>PERLIO_F_WRBUF</b>
<p>The buffer for this layer currently holds data written to it but not sent
to next layer.</p>
</li>
<li><a name="PERLIO_F_RDBUF"></a><b>PERLIO_F_RDBUF</b>
<p>The buffer for this layer currently holds unconsumed data read from
layer below.</p>
</li>
<li><a name="PERLIO_F_LINEBUF"></a><b>PERLIO_F_LINEBUF</b>
<p>Layer is line buffered. Write data should be passed to next layer down
whenever a "\n" is seen. Any data beyond the "\n" should then be
processed.</p>
</li>
<li><a name="PERLIO_F_TEMP"></a><b>PERLIO_F_TEMP</b>
<p>File has been <code class="inline"><a class="l_k" href="functions/unlink.html">unlink()</a></code>ed, or should be deleted on <code class="inline"><a class="l_k" href="functions/close.html">close()</a></code>.</p>
</li>
<li><a name="PERLIO_F_OPEN"></a><b>PERLIO_F_OPEN</b>
<p>Handle is open.</p>
</li>
<li><a name="PERLIO_F_FASTGETS"></a><b>PERLIO_F_FASTGETS</b>
<p>This instance of this layer supports the "fast <code class="inline"><span class="w">gets</span></code>
" interface.
Normally set based on <code class="inline"><span class="w">PERLIO_K_FASTGETS</span></code>
 for the class and by the
existence of the function(s) in the table. However a class that
normally provides that interface may need to avoid it on a
particular instance. The "pending" layer needs to do this when
it is pushed above a layer which does not support the interface.
(Perl's <code class="inline"><span class="i">sv_gets</span><span class="s">(</span><span class="s">)</span></code>
 does not expect the streams fast <code class="inline"><span class="w">gets</span></code>
 behaviour
to change during one "get".)</p>
</li>
</ul>
<a name="Methods-in-Detail"></a><h2>Methods in Detail</h2>
<ul>
<li><a name="fsize"></a><b>fsize</b>
<pre class="verbatim"><ol><li>	<span class="w">Size_t</span> <span class="w">fsize</span><span class="sc">;</span></li></ol></pre><p>Size of the function table. This is compared against the value PerlIO
code "knows" as a compatibility check. Future versions <i>may</i> be able
to tolerate layers compiled against an old version of the headers.</p>
</li>
<li><a name="name"></a><b>name</b>
<pre class="verbatim"><ol><li>	<span class="w">char</span> * <span class="w">name</span><span class="sc">;</span></li></ol></pre><p>The name of the layer whose open() method Perl should invoke on
open().  For example if the layer is called APR, you will call:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/open.html">open</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&gt;:APR&quot;</span><span class="cm">,</span> ...</li></ol></pre><p>and Perl knows that it has to invoke the PerlIOAPR_open() method
implemented by the APR layer.</p>
</li>
<li><a name="size"></a><b>size</b>
<pre class="verbatim"><ol><li>	<span class="w">Size_t</span> <span class="w">size</span><span class="sc">;</span></li></ol></pre><p>The size of the per-instance data structure, e.g.:</p>
<pre class="verbatim"><ol><li>  <span class="i">sizeof</span><span class="s">(</span><span class="w">PerlIOAPR</span><span class="s">)</span></li></ol></pre><p>If this field is zero then <code class="inline"><span class="w">PerlIO_pushed</span></code>
 does not malloc anything
and assumes layer's Pushed function will do any required layer stack
manipulation - used to avoid malloc/free overhead for dummy layers.
If the field is non-zero it must be at least the size of <code class="inline"><span class="w">PerlIOl</span></code>
,
<code class="inline"><span class="w">PerlIO_pushed</span></code>
 will allocate memory for the layer's data structures
and link new layer onto the stream's stack. (If the layer's Pushed
method returns an error indication the layer is popped again.)</p>
</li>
<li><a name="kind"></a><b>kind</b>
<pre class="verbatim"><ol><li>	<span class="w">IV</span> <span class="w">kind</span><span class="sc">;</span></li></ol></pre><ul>
<li><a name="*-PERLIO_K_BUFFERED"></a><b>PERLIO_K_BUFFERED</b>
<p>The layer is buffered.</p>
</li>
<li><a name="*-PERLIO_K_RAW"></a><b>PERLIO_K_RAW</b>
<p>The layer is acceptable to have in a binmode(FH) stack - i.e. it does not
(or will configure itself not to) transform bytes passing through it.</p>
</li>
<li><a name="*-PERLIO_K_CANCRLF"></a><b>PERLIO_K_CANCRLF</b>
<p>Layer can translate between "\n" and CRLF line ends.</p>
</li>
<li><a name="*-PERLIO_K_FASTGETS"></a><b>PERLIO_K_FASTGETS</b>
<p>Layer allows buffer snooping.</p>
</li>
<li><a name="*-PERLIO_K_MULTIARG"></a><b>PERLIO_K_MULTIARG</b>
<p>Used when the layer's open() accepts more arguments than usual. The
extra arguments should come not before the <code class="inline"><span class="w">MODE</span></code>
 argument. When this
flag is used it's up to the layer to validate the args.</p>
</li>
</ul>
</li>
<li><a name="Pushed"></a><b>Pushed</b>
<pre class="verbatim"><ol><li> <span class="w">IV</span>	<span class="s">(</span><span class="i">*Pushed</span><span class="s">)</span><span class="s">(</span><span class="w">pTHX_</span> <span class="w">PerlIO</span> *<span class="w">f</span><span class="cm">,</span><span class="w">const</span> <span class="w">char</span> *<span class="w">mode</span><span class="cm">,</span> <span class="w">SV</span> *<span class="w">arg</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The only absolutely mandatory method. Called when the layer is pushed
onto the stack.  The <code class="inline"><span class="w">mode</span></code>
 argument may be NULL if this occurs
post-open. The <code class="inline"><span class="w">arg</span></code>
 will be non-<code class="inline"><span class="w">NULL</span></code>
 if an argument string was
passed. In most cases this should call <code class="inline"><span class="i">PerlIOBase_pushed</span><span class="s">(</span><span class="s">)</span></code>
 to
convert <code class="inline"><span class="w">mode</span></code>
 into the appropriate <code class="inline"><span class="w">PERLIO_F_XXXXX</span></code>
 flags in
addition to any actions the layer itself takes.  If a layer is not
expecting an argument it need neither save the one passed to it, nor
provide <code class="inline"><span class="i">Getarg</span><span class="s">(</span><span class="s">)</span></code>
 (it could perhaps <code class="inline"><span class="w">Perl_warn</span></code>
 that the argument
was un-expected).</p>
<p>Returns 0 on success. On failure returns -1 and should set errno.</p>
</li>
<li><a name="Popped"></a><b>Popped</b>
<pre class="verbatim"><ol><li>	IV	(*Popped)(pTHX_ PerlIO *f);</li></ol></pre><p>Called when the layer is popped from the stack. A layer will normally
be popped after <code class="inline"><span class="i">Close</span><span class="s">(</span><span class="s">)</span></code>
 is called. But a layer can be popped
without being closed if the program is dynamically managing layers on
the stream. In such cases <code class="inline"><span class="i">Popped</span><span class="s">(</span><span class="s">)</span></code>
 should free any resources
(buffers, translation tables, ...) not held directly in the layer's
struct.  It should also <code class="inline"><span class="i">Unread</span><span class="s">(</span><span class="s">)</span></code>
 any unconsumed data that has been
read and buffered from the layer below back to that layer, so that it
can be re-provided to what ever is now above.</p>
<p>Returns 0 on success and failure.  If <code class="inline"><span class="i">Popped</span><span class="s">(</span><span class="s">)</span></code>
 returns <i>true</i> then
<i>perlio.c</i> assumes that either the layer has popped itself, or the
layer is super special and needs to be retained for other reasons.
In most cases it should return <i>false</i>.</p>
</li>
<li><a name="Open"></a><b>Open</b>
<pre class="verbatim"><ol><li>	PerlIO *	(*Open)(...);</li></ol></pre><p>The <code class="inline"><span class="i">Open</span><span class="s">(</span><span class="s">)</span></code>
 method has lots of arguments because it combines the
functions of perl's <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>, <code class="inline"><span class="w">PerlIO_open</span></code>
, perl's <code class="inline"><a class="l_k" href="functions/sysopen.html">sysopen</a></code>,
<code class="inline"><span class="w">PerlIO_fdopen</span></code>
 and <code class="inline"><span class="w">PerlIO_reopen</span></code>
.  The full prototype is as
follows:</p>
<pre class="verbatim"><ol><li> PerlIO *	(*Open)(pTHX_ PerlIO_funcs *tab,</li><li>			PerlIO_list_t *layers, IV n,</li><li>			const char *mode,</li><li>			int fd, int imode, int perm,</li><li>			PerlIO *old,</li><li>			int narg, SV **args);</li></ol></pre><p>Open should (perhaps indirectly) call <code class="inline"><span class="i">PerlIO_allocate</span><span class="s">(</span><span class="s">)</span></code>
 to allocate
a slot in the table and associate it with the layers information for
the opened file, by calling <code class="inline"><span class="w">PerlIO_push</span></code>
.  The <i>layers</i> is an
array of all the layers destined for the <code class="inline"><span class="w">PerlIO</span> *</code>
, and any
arguments passed to them, <i>n</i> is the index into that array of the
layer being called. The macro <code class="inline"><span class="w">PerlIOArg</span></code>
 will return a (possibly
<code class="inline"><span class="w">NULL</span></code>
) SV * for the argument passed to the layer.</p>
<p>The <i>mode</i> string is an "<code class="inline"><span class="i">fopen</span><span class="s">(</span><span class="s">)</span></code>
-like" string which would match
the regular expression <code class="inline"><span class="q">/^[I#]?[rwa]\+?[bt]?$/</span></code>
.</p>
<p>The <code class="inline"><span class="q">&#39;I&#39;</span></code>
 prefix is used during creation of <code class="inline"><span class="w">stdin</span></code>
..<code class="inline"><span class="w">stderr</span></code>
 via
special <code class="inline"><span class="w">PerlIO_fdopen</span></code>
 calls; the <code class="inline"><span class="q">&#39;#&#39;</span></code>
 prefix means that this is
<code class="inline"><a class="l_k" href="functions/sysopen.html">sysopen</a></code> and that <i>imode</i> and <i>perm</i> should be passed to
<code class="inline"><span class="w">PerlLIO_open3</span></code>
; <code class="inline"><span class="q">&#39;r&#39;</span></code>
 means <b>r</b>ead, <code class="inline"><span class="q">&#39;w&#39;</span></code>
 means <b>w</b>rite and
<code class="inline"><span class="q">&#39;a&#39;</span></code>
 means <b>a</b>ppend. The <code class="inline"><span class="q">&#39;+&#39;</span></code>
 suffix means that both reading and
writing/appending are permitted.  The <code class="inline"><span class="q">&#39;b&#39;</span></code>
 suffix means file should
be binary, and <code class="inline"><span class="q">&#39;t&#39;</span></code>
 means it is text. (Almost all layers should do
the IO in binary mode, and ignore the b/t bits. The <code class="inline"><span class="j">:</span><span class="w">crlf</span></code>
 layer
should be pushed to handle the distinction.)</p>
<p>If <i>old</i> is not <code class="inline"><span class="w">NULL</span></code>
 then this is a <code class="inline"><span class="w">PerlIO_reopen</span></code>
. Perl itself
does not use this (yet?) and semantics are a little vague.</p>
<p>If <i>fd</i> not negative then it is the numeric file descriptor <i>fd</i>,
which will be open in a manner compatible with the supplied mode
string, the call is thus equivalent to <code class="inline"><span class="w">PerlIO_fdopen</span></code>
. In this case
<i>nargs</i> will be zero.</p>
<p>If <i>nargs</i> is greater than zero then it gives the number of arguments
passed to <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>, otherwise it will be 1 if for example
<code class="inline"><span class="w">PerlIO_open</span></code>
 was called.  In simple cases SvPV_nolen(*args) is the
pathname to open.</p>
<p>If a layer provides <code class="inline"><span class="i">Open</span><span class="s">(</span><span class="s">)</span></code>
 it should normally call the <code class="inline"><span class="i">Open</span><span class="s">(</span><span class="s">)</span></code>

method of next layer down (if any) and then push itself on top if that
succeeds.  <code class="inline"><span class="w">PerlIOBase_open</span></code>
 is provided to do exactly that, so in
most cases you don't have to write your own <code class="inline"><span class="i">Open</span><span class="s">(</span><span class="s">)</span></code>
 method.  If this
method is not defined, other layers may have difficulty pushing
themselves on top of it during open.</p>
<p>If <code class="inline"><span class="w">PerlIO_push</span></code>
 was performed and open has failed, it must
<code class="inline"><span class="w">PerlIO_pop</span></code>
 itself, since if it's not, the layer won't be removed
and may cause bad problems.</p>
<p>Returns <code class="inline"><span class="w">NULL</span></code>
 on failure.</p>
</li>
<li><a name="Binmode"></a><b>Binmode</b>
<pre class="verbatim"><ol><li>	IV        (*Binmode)(pTHX_ PerlIO *f);</li></ol></pre><p>Optional. Used when <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 layer is pushed (explicitly or as a result
of binmode(FH)). If not present layer will be popped. If present
should configure layer as binary (or pop itself) and return 0.
If it returns -1 for error <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> will fail with layer
still on the stack.</p>
</li>
<li><a name="Getarg"></a><b>Getarg</b>
<pre class="verbatim"><ol><li>	SV *      (*Getarg)(pTHX_ PerlIO *f,</li><li>			    CLONE_PARAMS *param, int flags);</li></ol></pre><p>Optional. If present should return an SV * representing the string
argument passed to the layer when it was
pushed. e.g. ":encoding(ascii)" would return an SvPV with value
"ascii". (<i>param</i> and <i>flags</i> arguments can be ignored in most
cases)</p>
<p><code class="inline"><span class="w">Dup</span></code>
 uses <code class="inline"><span class="w">Getarg</span></code>
 to retrieve the argument originally passed to
<code class="inline"><span class="w">Pushed</span></code>
, so you must implement this function if your layer has an
extra argument to <code class="inline"><span class="w">Pushed</span></code>
 and will ever be <code class="inline"><span class="w">Dup</span></code>
ed.</p>
</li>
<li><a name="Fileno"></a><b>Fileno</b>
<pre class="verbatim"><ol><li>	IV        (*Fileno)(pTHX_ PerlIO *f);</li></ol></pre><p>Returns the Unix/Posix numeric file descriptor for the handle. Normally
<code class="inline"><span class="i">PerlIOBase_fileno</span><span class="s">(</span><span class="s">)</span></code>
 (which just asks next layer down) will suffice
for this.</p>
<p>Returns -1 on error, which is considered to include the case where the
layer cannot provide such a file descriptor.</p>
</li>
<li><a name="Dup"></a><b>Dup</b>
<pre class="verbatim"><ol><li>	PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,</li><li>			CLONE_PARAMS *param, int flags);</li></ol></pre><p>XXX: Needs more docs.</p>
<p>Used as part of the "clone" process when a thread is spawned (in which
case param will be non-NULL) and when a stream is being duplicated via
'&amp;' in the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>.</p>
<p>Similar to <code class="inline"><span class="w">Open</span></code>
, returns PerlIO* on success, <code class="inline"><span class="w">NULL</span></code>
 on failure.</p>
</li>
<li><a name="Read"></a><b>Read</b>
<pre class="verbatim"><ol><li>	SSize_t	(*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);</li></ol></pre><p>Basic read operation.</p>
<p>Typically will call <code class="inline"><span class="w">Fill</span></code>
 and manipulate pointers (possibly via the
API).  <code class="inline"><span class="i">PerlIOBuf_read</span><span class="s">(</span><span class="s">)</span></code>
 may be suitable for derived classes which
provide "fast gets" methods.</p>
<p>Returns actual bytes read, or -1 on an error.</p>
</li>
<li><a name="Unread"></a><b>Unread</b>
<pre class="verbatim"><ol><li>	SSize_t	(*Unread)(pTHX_ PerlIO *f,</li><li>			  const void *vbuf, Size_t count);</li></ol></pre><p>A superset of stdio's <code class="inline"><span class="i">ungetc</span><span class="s">(</span><span class="s">)</span></code>
. Should arrange for future reads to
see the bytes in <code class="inline"><span class="w">vbuf</span></code>
. If there is no obviously better implementation
then <code class="inline"><span class="i">PerlIOBase_unread</span><span class="s">(</span><span class="s">)</span></code>
 provides the function by pushing a "fake"
"pending" layer above the calling layer.</p>
<p>Returns the number of unread chars.</p>
</li>
<li><a name="Write"></a><b>Write</b>
<pre class="verbatim"><ol><li>	SSize_t	(*Write)(PerlIO *f, const void *vbuf, Size_t count);</li></ol></pre><p>Basic write operation.</p>
<p>Returns bytes written or -1 on an error.</p>
</li>
<li><a name="Seek"></a><b>Seek</b>
<pre class="verbatim"><ol><li>	IV	(*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);</li></ol></pre><p>Position the file pointer. Should normally call its own <code class="inline"><span class="w">Flush</span></code>

method and then the <code class="inline"><span class="w">Seek</span></code>
 method of next layer down.</p>
<p>Returns 0 on success, -1 on failure.</p>
</li>
<li><a name="Tell"></a><b>Tell</b>
<pre class="verbatim"><ol><li>	Off_t	(*Tell)(pTHX_ PerlIO *f);</li></ol></pre><p>Return the file pointer. May be based on layers cached concept of
position to avoid overhead.</p>
<p>Returns -1 on failure to get the file pointer.</p>
</li>
<li><a name="Close"></a><b>Close</b>
<pre class="verbatim"><ol><li>	IV	(*Close)(pTHX_ PerlIO *f);</li></ol></pre><p>Close the stream. Should normally call <code class="inline"><span class="i">PerlIOBase_close</span><span class="s">(</span><span class="s">)</span></code>
 to flush
itself and close layers below, and then deallocate any data structures
(buffers, translation tables, ...) not  held directly in the data
structure.</p>
<p>Returns 0 on success, -1 on failure.</p>
</li>
<li><a name="Flush"></a><b>Flush</b>
<pre class="verbatim"><ol><li>	IV	(*Flush)(pTHX_ PerlIO *f);</li></ol></pre><p>Should make stream's state consistent with layers below. That is, any
buffered write data should be written, and file position of lower layers
adjusted for data read from below but not actually consumed.
(Should perhaps <code class="inline"><span class="i">Unread</span><span class="s">(</span><span class="s">)</span></code>
 such data to the lower layer.)</p>
<p>Returns 0 on success, -1 on failure.</p>
</li>
<li><a name="Fill"></a><b>Fill</b>
<pre class="verbatim"><ol><li>	IV	(*Fill)(pTHX_ PerlIO *f);</li></ol></pre><p>The buffer for this layer should be filled (for read) from layer
below.  When you "subclass" PerlIOBuf layer, you want to use its
<i>_read</i> method and to supply your own fill method, which fills the
PerlIOBuf's buffer.</p>
<p>Returns 0 on success, -1 on failure.</p>
</li>
<li><a name="Eof"></a><b>Eof</b>
<pre class="verbatim"><ol><li>	IV	(*Eof)(pTHX_ PerlIO *f);</li></ol></pre><p>Return end-of-file indicator. <code class="inline"><span class="i">PerlIOBase_eof</span><span class="s">(</span><span class="s">)</span></code>
 is normally sufficient.</p>
<p>Returns 0 on end-of-file, 1 if not end-of-file, -1 on error.</p>
</li>
<li><a name="Error"></a><b>Error</b>
<pre class="verbatim"><ol><li>	IV	(*Error)(pTHX_ PerlIO *f);</li></ol></pre><p>Return error indicator. <code class="inline"><span class="i">PerlIOBase_error</span><span class="s">(</span><span class="s">)</span></code>
 is normally sufficient.</p>
<p>Returns 1 if there is an error (usually when <code class="inline"><span class="w">PERLIO_F_ERROR</span></code>
 is set),
0 otherwise.</p>
</li>
<li><a name="Clearerr"></a><b>Clearerr</b>
<pre class="verbatim"><ol><li>	void	(*Clearerr)(pTHX_ PerlIO *f);</li></ol></pre><p>Clear end-of-file and error indicators. Should call <code class="inline"><span class="i">PerlIOBase_clearerr</span><span class="s">(</span><span class="s">)</span></code>

to set the <code class="inline"><span class="w">PERLIO_F_XXXXX</span></code>
 flags, which may suffice.</p>
</li>
<li><a name="Setlinebuf"></a><b>Setlinebuf</b>
<pre class="verbatim"><ol><li>	void	(*Setlinebuf)(pTHX_ PerlIO *f);</li></ol></pre><p>Mark the stream as line buffered. <code class="inline"><span class="i">PerlIOBase_setlinebuf</span><span class="s">(</span><span class="s">)</span></code>
 sets the
PERLIO_F_LINEBUF flag and is normally sufficient.</p>
</li>
<li><a name="Get_base"></a><b>Get_base</b>
<pre class="verbatim"><ol><li>	STDCHAR *	(*Get_base)(pTHX_ PerlIO *f);</li></ol></pre><p>Allocate (if not already done so) the read buffer for this layer and
return pointer to it. Return NULL on failure.</p>
</li>
<li><a name="Get_bufsiz"></a><b>Get_bufsiz</b>
<pre class="verbatim"><ol><li>	Size_t	(*Get_bufsiz)(pTHX_ PerlIO *f);</li></ol></pre><p>Return the number of bytes that last <code class="inline"><span class="i">Fill</span><span class="s">(</span><span class="s">)</span></code>
 put in the buffer.</p>
</li>
<li><a name="Get_ptr"></a><b>Get_ptr</b>
<pre class="verbatim"><ol><li>	STDCHAR *	(*Get_ptr)(pTHX_ PerlIO *f);</li></ol></pre><p>Return the current read pointer relative to this layer's buffer.</p>
</li>
<li><a name="Get_cnt"></a><b>Get_cnt</b>
<pre class="verbatim"><ol><li>	SSize_t	(*Get_cnt)(pTHX_ PerlIO *f);</li></ol></pre><p>Return the number of bytes left to be read in the current buffer.</p>
</li>
<li><a name="Set_ptrcnt"></a><b>Set_ptrcnt</b>
<pre class="verbatim"><ol><li>	void	(*Set_ptrcnt)(pTHX_ PerlIO *f,</li><li>			      STDCHAR *ptr, SSize_t cnt);</li></ol></pre><p>Adjust the read pointer and count of bytes to match <code class="inline"><span class="w">ptr</span></code>
 and/or <code class="inline"><span class="w">cnt</span></code>
.
The application (or layer above) must ensure they are consistent.
(Checking is allowed by the paranoid.)</p>
</li>
</ul>
<a name="Utilities"></a><h2>Utilities</h2>
<p>To ask for the next layer down use PerlIONext(PerlIO *f).</p>
<p>To check that a PerlIO* is valid use PerlIOValid(PerlIO *f).  (All
this does is really just to check that the pointer is non-NULL and
that the pointer behind that is non-NULL.)</p>
<p>PerlIOBase(PerlIO *f) returns the "Base" pointer, or in other words,
the <code class="inline"><span class="w">PerlIOl</span>*</code>
 pointer.</p>
<p>PerlIOSelf(PerlIO* f, type) return the PerlIOBase cast to a type.</p>
<p>Perl_PerlIO_or_Base(PerlIO* f, callback, base, failure, args) either
calls the <i>callback</i> from the functions of the layer <i>f</i> (just by
the name of the IO function, like "Read") with the <i>args</i>, or if
there is no such callback, calls the <i>base</i> version of the callback
with the same args, or if the f is invalid, set errno to EBADF and
return <i>failure</i>.</p>
<p>Perl_PerlIO_or_fail(PerlIO* f, callback, failure, args) either calls
the <i>callback</i> of the functions of the layer <i>f</i> with the <i>args</i>,
or if there is no such callback, set errno to EINVAL.  Or if the f is
invalid, set errno to EBADF and return <i>failure</i>.</p>
<p>Perl_PerlIO_or_Base_void(PerlIO* f, callback, base, args) either calls
the <i>callback</i> of the functions of the layer <i>f</i> with the <i>args</i>,
or if there is no such callback, calls the <i>base</i> version of the
callback with the same args, or if the f is invalid, set errno to
EBADF.</p>
<p>Perl_PerlIO_or_fail_void(PerlIO* f, callback, args) either calls the
<i>callback</i> of the functions of the layer <i>f</i> with the <i>args</i>, or if
there is no such callback, set errno to EINVAL.  Or if the f is
invalid, set errno to EBADF.</p>
<a name="Implementing-PerlIO-Layers"></a><h2>Implementing PerlIO Layers</h2>
<p>If you find the implementation document unclear or not sufficient,
look at the existing PerlIO layer implementations, which include:</p>
<ul>
<li><a name="*-C-implementations"></a><b>C implementations</b>
<p>The <i>perlio.c</i> and <i>perliol.h</i> in the Perl core implement the
"unix", "perlio", "stdio", "crlf", "utf8", "byte", "raw", "pending"
layers, and also the "mmap" and "win32" layers if applicable.
(The "win32" is currently unfinished and unused, to see what is used
instead in Win32, see <a href="PerlIO.html#Querying-the-layers-of-filehandles">Querying the layers of filehandles in PerlIO</a> .)</p>
<p>PerlIO::encoding, PerlIO::scalar, PerlIO::via in the Perl core.</p>
<p>PerlIO::gzip and APR::PerlIO (mod_perl 2.0) on CPAN.</p>
</li>
<li><a name="*-Perl-implementations"></a><b>Perl implementations</b>
<p>PerlIO::via::QuotedPrint in the Perl core and PerlIO::via::* on CPAN.</p>
</li>
</ul>
<p>If you are creating a PerlIO layer, you may want to be lazy, in other
words, implement only the methods that interest you.  The other methods
you can either replace with the "blank" methods</p>
<pre class="verbatim"><ol><li>    <span class="w">PerlIOBase_noop_ok</span></li><li>    <span class="w">PerlIOBase_noop_fail</span></li></ol></pre><p>(which do nothing, and return zero and -1, respectively) or for
certain methods you may assume a default behaviour by using a NULL
method.  The Open method looks for help in the 'parent' layer.
The following table summarizes the behaviour:</p>
<pre class="verbatim"><ol><li>    <span class="w">method</span>      <span class="w">behaviour</span> <span class="w">with</span> <span class="w">NULL</span></li><li></li><li>    <span class="w">Clearerr</span>    <span class="w">PerlIOBase_clearerr</span></li><li>    <span class="w">Close</span>       <span class="w">PerlIOBase_close</span></li><li>    <span class="w">Dup</span>         <span class="w">PerlIOBase_dup</span></li><li>    <span class="w">Eof</span>         <span class="w">PerlIOBase_eof</span></li><li>    <span class="w">Error</span>       <span class="w">PerlIOBase_error</span></li><li>    <span class="w">Fileno</span>      <span class="w">PerlIOBase_fileno</span></li><li>    <span class="w">Fill</span>        <span class="w">FAILURE</span></li><li>    <span class="w">Flush</span>       <span class="w">SUCCESS</span></li><li>    <span class="w">Getarg</span>      <span class="w">SUCCESS</span></li><li>    <span class="w">Get_base</span>    <span class="w">FAILURE</span></li><li>    <span class="w">Get_bufsiz</span>  <span class="w">FAILURE</span></li><li>    <span class="w">Get_cnt</span>     <span class="w">FAILURE</span></li><li>    <span class="w">Get_ptr</span>     <span class="w">FAILURE</span></li><li>    <span class="w">Open</span>        <span class="w">INHERITED</span></li><li>    <span class="w">Popped</span>      <span class="w">SUCCESS</span></li><li>    <span class="w">Pushed</span>      <span class="w">SUCCESS</span></li><li>    <span class="w">Read</span>        <span class="w">PerlIOBase_read</span></li><li>    <span class="w">Seek</span>        <span class="w">FAILURE</span></li><li>    <span class="w">Set_cnt</span>     <span class="w">FAILURE</span></li><li>    <span class="w">Set_ptrcnt</span>  <span class="w">FAILURE</span></li><li>    <span class="w">Setlinebuf</span>  <span class="w">PerlIOBase_setlinebuf</span></li><li>    <span class="w">Tell</span>        <span class="w">FAILURE</span></li><li>    <span class="w">Unread</span>      <span class="w">PerlIOBase_unread</span></li><li>    <span class="w">Write</span>       <span class="w">FAILURE</span></li><li></li><li> <span class="w">FAILURE</span>        <span class="w">Set</span> <span class="w">errno</span> <span class="s">(</span><span class="w">to</span> <span class="w">EINVAL</span> <span class="w">in</span> <span class="w">Unixish</span><span class="cm">,</span> <span class="w">to</span> <span class="w">LIB</span><span class="i">$_INVARG</span> <span class="w">in</span> <span class="w">VMS</span><span class="s">)</span></li><li>                <a class="l_k" href="functions/and.html">and</a> <a class="l_k" href="functions/return.html">return</a> <span class="n">-1</span> <span class="s">(</span><a class="l_k" href="functions/for.html">for</a> <span class="w">numeric</span> <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/values.html">values</a><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">NULL</span> <span class="s">(</span><a class="l_k" href="functions/for.html">for</a></li><li>                <span class="w">pointers</span><span class="s">)</span></li><li> <span class="w">INHERITED</span>      <span class="w">Inherited</span> <span class="w">from</span> <span class="w">the</span> <span class="w">layer</span> <span class="w">below</span></li><li> <span class="w">SUCCESS</span>        <span class="w">Return</span> <span class="n">0</span> <span class="s">(</span><a class="l_k" href="functions/for.html">for</a> <span class="w">numeric</span> <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/values.html">values</a><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">a</span> <span class="w">pointer</span></li></ol></pre><a name="Core-Layers"></a><h2>Core Layers</h2>
<p>The file <code class="inline"><span class="w">perlio</span>.<span class="w">c</span></code>
 provides the following layers:</p>
<ul>
<li><a name="%22unix%22"></a><b>"unix"</b>
<p>A basic non-buffered layer which calls Unix/POSIX <code class="inline"><a class="l_k" href="functions/read.html">read()</a></code>, <code class="inline"><a class="l_k" href="functions/write.html">write()</a></code>,
<code class="inline"><span class="i">lseek</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><a class="l_k" href="functions/close.html">close()</a></code>. No buffering. Even on platforms that distinguish
between O_TEXT and O_BINARY this layer is always O_BINARY.</p>
</li>
<li><a name="%22perlio%22"></a><b>"perlio"</b>
<p>A very complete generic buffering layer which provides the whole of
PerlIO API. It is also intended to be used as a "base class" for other
layers. (For example its <code class="inline"><span class="i">Read</span><span class="s">(</span><span class="s">)</span></code>
 method is implemented in terms of
the <code class="inline"><span class="i">Get_cnt</span><span class="s">(</span><span class="s">)</span></code>
/<code class="inline"><span class="i">Get_ptr</span><span class="s">(</span><span class="s">)</span></code>
/<code class="inline"><span class="i">Set_ptrcnt</span><span class="s">(</span><span class="s">)</span></code>
 methods).</p>
<p>"perlio" over "unix" provides a complete replacement for stdio as seen
via PerlIO API. This is the default for USE_PERLIO when system's stdio
does not permit perl's "fast gets" access, and which do not
distinguish between <code class="inline"><span class="w">O_TEXT</span></code>
 and <code class="inline"><span class="w">O_BINARY</span></code>
.</p>
</li>
<li><a name="%22stdio%22"></a><b>"stdio"</b>
<p>A layer which provides the PerlIO API via the layer scheme, but
implements it by calling system's stdio. This is (currently) the default
if system's stdio provides sufficient access to allow perl's "fast gets"
access and which do not distinguish between <code class="inline"><span class="w">O_TEXT</span></code>
 and <code class="inline"><span class="w">O_BINARY</span></code>
.</p>
</li>
<li><a name="%22crlf%22"></a><b>"crlf"</b>
<p>A layer derived using "perlio" as a base class. It provides Win32-like
"\n" to CR,LF translation. Can either be applied above "perlio" or serve
as the buffer layer itself. "crlf" over "unix" is the default if system
distinguishes between <code class="inline"><span class="w">O_TEXT</span></code>
 and <code class="inline"><span class="w">O_BINARY</span></code>
 opens. (At some point
"unix" will be replaced by a "native" Win32 IO layer on that platform,
as Win32's read/write layer has various drawbacks.) The "crlf" layer is
a reasonable model for a layer which transforms data in some way.</p>
</li>
<li><a name="%22mmap%22"></a><b>"mmap"</b>
<p>If Configure detects <code class="inline"><span class="i">mmap</span><span class="s">(</span><span class="s">)</span></code>
 functions this layer is provided (with
"perlio" as a "base") which does "read" operations by mmap()ing the
file. Performance improvement is marginal on modern systems, so it is
mainly there as a proof of concept. It is likely to be unbundled from
the core at some point. The "mmap" layer is a reasonable model for a
minimalist "derived" layer.</p>
</li>
<li><a name="%22pending%22"></a><b>"pending"</b>
<p>An "internal" derivative of "perlio" which can be used to provide
Unread() function for layers which have no buffer or cannot be
bothered.  (Basically this layer's <code class="inline"><span class="i">Fill</span><span class="s">(</span><span class="s">)</span></code>
 pops itself off the stack
and so resumes reading from layer below.)</p>
</li>
<li><a name="%22raw%22"></a><b>"raw"</b>
<p>A dummy layer which never exists on the layer stack. Instead when
"pushed" it actually pops the stack removing itself, it then calls
Binmode function table entry on all the layers in the stack - normally
this (via PerlIOBase_binmode) removes any layers which do not have
<code class="inline"><span class="w">PERLIO_K_RAW</span></code>
 bit set. Layers can modify that behaviour by defining
their own Binmode entry.</p>
</li>
<li><a name="%22utf8%22"></a><b>"utf8"</b>
<p>Another dummy layer. When pushed it pops itself and sets the
<code class="inline"><span class="w">PERLIO_F_UTF8</span></code>
 flag on the layer which was (and now is once more)
the top of the stack.</p>
</li>
</ul>
<p>In addition <i>perlio.c</i> also provides a number of <code class="inline"><span class="i">PerlIOBase_xxxx</span><span class="s">(</span><span class="s">)</span></code>

functions which are intended to be used in the table slots of classes
which do not need to do anything special for a particular method.</p>
<a name="Extension-Layers"></a><h2>Extension Layers</h2>
<p>Layers can be made available by extension modules. When an unknown layer
is encountered the PerlIO code will perform the equivalent of :</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/use.html">use</a> <span class="w">PerlIO</span> <span class="q">&#39;layer&#39;</span><span class="sc">;</span></li></ol></pre><p>Where <i>layer</i> is the unknown layer. <i>PerlIO.pm</i> will then attempt to:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/require.html">require</a> <span class="w">PerlIO::layer</span><span class="sc">;</span></li></ol></pre><p>If after that process the layer is still not defined then the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>
will fail.</p>
<p>The following extension layers are bundled with perl:</p>
<ul>
<li><a name="%22%3aencoding%22"></a><b>":encoding"</b>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/use.html">use</a> <span class="w">Encoding</span><span class="sc">;</span></li></ol></pre><p>makes this layer available, although <i>PerlIO.pm</i> "knows" where to
find it.  It is an example of a layer which takes an argument as it is
called thus:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/open.html">open</a><span class="s">(</span> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(iso-8859-7)&quot;</span><span class="cm">,</span> <span class="i">$pathname</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="%22%3ascalar%22"></a><b>":scalar"</b>
<p>Provides support for reading data from and writing data to a scalar.</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/open.html">open</a><span class="s">(</span> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;+&lt;:scalar&quot;</span><span class="cm">,</span> \<span class="i">$scalar</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When a handle is so opened, then reads get bytes from the string value
of <i>$scalar</i>, and writes change the value. In both cases the position
in <i>$scalar</i> starts as zero but can be altered via <code class="inline"><a class="l_k" href="functions/seek.html">seek</a></code>, and
determined via <code class="inline"><a class="l_k" href="functions/tell.html">tell</a></code>.</p>
<p>Please note that this layer is implied when calling open() thus:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/open.html">open</a><span class="s">(</span> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;+&lt;&quot;</span><span class="cm">,</span> \<span class="i">$scalar</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="%22%3avia%22"></a><b>":via"</b>
<p>Provided to allow layers to be implemented as Perl code.  For instance:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/use.html">use</a> <span class="w">PerlIO::via::StripHTML</span><span class="sc">;</span></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">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:via(StripHTML)&quot;</span><span class="cm">,</span> <span class="q">&quot;index.html&quot;</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See <a href="PerlIO/via.html">PerlIO::via</a> for details.</p>
</li>
</ul>
<a name="TODO"></a><h1>TODO</h1>
<p>Things that need to be done to improve this document.</p>
<ul>
<li>
<p>Explain how to make a valid fh without going through open()(i.e. apply
a layer). For example if the file is not opened through perl, but we
want to get back a fh, like it was opened by Perl.</p>
<p>How PerlIO_apply_layera fits in, where its docs, was it made public?</p>
<p>Currently the example could be something like this:</p>
<pre class="verbatim"><ol><li>  PerlIO *foo_to_PerlIO(pTHX_ char *mode, ...)</li><li>  {</li><li>      char *mode; /* "w", "r", etc */</li><li>      const char *layers = ":APR"; /* the layer name */</li><li>      PerlIO *f = PerlIO_allocate(aTHX);</li><li>      if (!f) {</li><li>          return NULL;</li><li>      }</li><li></li><li>      PerlIO_apply_layers(aTHX_ f, mode, layers);</li><li></li><li>      if (f) {</li><li>          PerlIOAPR *st = PerlIOSelf(f, PerlIOAPR);</li><li>          /* fill in the st struct, as in _open() */</li><li>          st-&gt;file = file;</li><li>          PerlIOBase(f)-&gt;flags |= PERLIO_F_OPEN;</li><li></li><li>          return f;</li><li>      }</li><li>      return NULL;</li><li>  }</li></ol></pre></li>
<li>
<p>fix/add the documentation in places marked as XXX.</p>
</li>
<li>
<p>The handling of errors by the layer is not specified. e.g. when $!
should be set explicitly, when the error handling should be just
delegated to the top layer.</p>
<p>Probably give some hints on using SETERRNO() or pointers to where they
can be found.</p>
</li>
<li>
<p>I think it would help to give some concrete examples to make it easier
to understand the API. Of course I agree that the API has to be
concise, but since there is no second document that is more of a
guide, I think that it'd make it easier to start with the doc which is
an API, but has examples in it in places where things are unclear, to
a person who is not a PerlIO guru (yet).</p>
</li>
</ul>




  <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="#History-and-Background">History and Background</a><li><a href="#Basic-Structure">Basic Structure</a><li><a href="#Layers-vs-Disciplines">Layers vs Disciplines</a><li><a href="#Data-Structures">Data Structures</a><li><a href="#Functions-and-Attributes">Functions and Attributes</a><li><a href="#Per-instance-Data">Per-instance Data</a><li><a href="#Layers-in-action.">Layers in action.</a><li><a href="#Per-instance-flag-bits">Per-instance flag bits</a><li><a href="#Methods-in-Detail">Methods in Detail</a><li><a href="#Utilities">Utilities</a><li><a href="#Implementing-PerlIO-Layers">Implementing PerlIO Layers</a><li><a href="#Core-Layers">Core Layers</a><li><a href="#Extension-Layers">Extension Layers</a></ul><li><a href="#TODO">TODO</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    = 'perliol';
  perldoc.pageAddress = 'perliol.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>