File: PerlIO.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 (688 lines) | stat: -rw-r--r-- 35,237 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>PerlIO - 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>PerlIO</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-modules-P.html">Core modules (P)</a> &gt;
      
    
    PerlIO
  

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


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Custom-Layers">Custom Layers</a><li><a href="#Alternatives-to-raw">Alternatives to raw</a><li><a href="#Defaults-and-how-to-override-them">Defaults and how to override them</a><li><a href="#Querying-the-layers-of-filehandles">Querying the layers of filehandles</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<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;:crlf&quot;</span><span class="cm">,</span> <span class="q">&quot;my.txt&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># support platform-native and </span></li><li>                                 <span class="c"># CRLF text files</span></li><li></li><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="q">&quot;his.jpg&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># portably open a binary file for reading</span></li><li>  <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$fh</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>  <span class="j">Shell:</span></li><li>    <span class="w">PERLIO</span>=<span class="w">perlio</span> <span class="w">perl</span> ....</li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>When an undefined layer 'foo' is encountered in an <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> or
<code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> layer specification then C code performs 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;foo&#39;</span><span class="sc">;</span></li></ol></pre><p>The perl code in PerlIO.pm then attempts to locate a layer by doing</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/require.html">require</a> <span class="w">PerlIO::foo</span><span class="sc">;</span></li></ol></pre><p>Otherwise the <code class="inline"><span class="w">PerlIO</span></code>
 package is a place holder for additional
PerlIO related functions.</p>
<p>The following layers are currently defined:</p>
<ul>
<li><a name="%3aunix"></a><b>:unix</b>
<p>Lowest level layer which provides basic PerlIO operations in terms of
UNIX/POSIX numeric file descriptor calls
(open(), read(), write(), lseek(), close()).</p>
</li>
<li><a name="%3astdio"></a><b>:stdio</b>
<p>Layer which calls <code class="inline"><span class="w">fread</span></code>
, <code class="inline"><span class="w">fwrite</span></code>
 and <code class="inline"><span class="w">fseek</span></code>
/<code class="inline"><span class="w">ftell</span></code>
 etc.  Note
that as this is "real" stdio it will ignore any layers beneath it and
go straight to the operating system via the C library as usual.</p>
</li>
<li><a name="%3aperlio"></a><b>:perlio</b>
<p>A from scratch implementation of buffering for PerlIO. Provides fast
access to the buffer for <code class="inline"><span class="w">sv_gets</span></code>
 which implements perl's readline/&lt;&gt;
and in general attempts to minimize data copying.</p>
<p><code class="inline"><span class="j">:</span><span class="w">perlio</span></code>
 will insert a <code class="inline"><span class="j">:</span><span class="w">unix</span></code>
 layer below itself to do low level IO.</p>
</li>
<li><a name="%3acrlf"></a><b>:crlf</b>
<p>A layer that implements DOS/Windows like CRLF line endings.  On read
converts pairs of CR,LF to a single "\n" newline character.  On write
converts each "\n" to a CR,LF pair.  Note that this layer will silently
refuse to be pushed on top of itself.</p>
<p>It currently does <i>not</i> mimic MS-DOS as far as treating of Control-Z
as being an end-of-file marker.</p>
<p>Based on the <code class="inline"><span class="j">:</span><span class="w">perlio</span></code>
 layer.</p>
</li>
<li><a name="%3autf8"></a><b>:utf8</b>
<p>Declares that the stream accepts perl's <i>internal</i> encoding of
characters.  (Which really is UTF-8 on ASCII machines, but is
UTF-EBCDIC on EBCDIC machines.)  This allows any character perl can
represent to be read from or written to the stream. The UTF-X encoding
is chosen to render simple text parts (i.e.  non-accented letters,
digits and common punctuation) human readable in the encoded file.</p>
<p>(<b>CAUTION</b>: This layer does not validate byte sequences.  For reading input,
you should instead use <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">UTF</span>-<span class="n">8</span><span class="s">)</span></code>
 instead of bare <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
.)</p>
<p>Here is how to write your native data out using UTF-8 (or UTF-EBCDIC)
and then read it back in.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">F</span><span class="cm">,</span> <span class="q">&quot;&gt;:utf8&quot;</span><span class="cm">,</span> <span class="q">&quot;data.utf&quot;</span><span class="s">)</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">$out</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="w">F</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">F</span><span class="cm">,</span> <span class="q">&quot;&lt;:utf8&quot;</span><span class="cm">,</span> <span class="q">&quot;data.utf&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">$in</span> = <span class="q">&lt;F&gt;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="w">F</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="%3abytes"></a><b>:bytes</b>
<p>This is the inverse of the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer. It turns off the flag
on the layer below so that data read from it is considered to
be "octets" i.e. characters in the range 0..255 only. Likewise
on output perl will warn if a "wide" character is written
to a such a stream.</p>
</li>
<li><a name="%3araw"></a><b>:raw</b>
<p>The <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 layer is <i>defined</i> as being identical to calling
<code class="inline"><a class="l_k" href="functions/binmode.html">binmode($fh)</a></code> - the stream is made suitable for passing binary data,
i.e. each byte is passed as-is. The stream will still be
buffered.</p>
<p>In Perl 5.6 and some books the <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 layer (previously sometimes also
referred to as a "discipline") is documented as the inverse of the
<code class="inline"><span class="j">:</span><span class="w">crlf</span></code>
 layer. That is no longer the case - other layers which would
alter the binary nature of the stream are also disabled.  If you want UNIX
line endings on a platform that normally does CRLF translation, but still
want UTF-8 or encoding defaults, the appropriate thing to do is to add
<code class="inline"><span class="j">:</span><span class="w">perlio</span></code>
 to the PERLIO environment variable.</p>
<p>The implementation of <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 is as a pseudo-layer which when "pushed"
pops itself and then any layers which do not declare themselves as suitable
for binary data. (Undoing :utf8 and :crlf are implemented by clearing
flags rather than popping layers but that is an implementation detail.)</p>
<p>As a consequence of the fact that <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 normally pops layers,
it usually only makes sense to have it as the only or first element in
a layer specification.  When used as the first element it provides
a known base on which to build e.g.</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;:raw:utf8&quot;</span><span class="cm">,</span>...<span class="s">)</span></li></ol></pre><p>will construct a "binary" stream, but then enable UTF-8 translation.</p>
</li>
<li><a name="%3apop"></a><b>:pop</b>
<p>A pseudo layer that removes the top-most layer. Gives perl code a
way to manipulate the layer stack.  Note that <code class="inline"><span class="j">:</span><a class="l_k" href="functions/pop.html">pop</a></code>
 only works on
real layers and will not undo the effects of pseudo layers like
<code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
.  An example of a possible use might be:</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="s">)</span></li><li>    ...</li><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span><span class="q">&quot;:encoding(...)&quot;</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># next chunk is encoded</span></li><li>    ...</li><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span><span class="q">&quot;:pop&quot;</span><span class="s">)</span><span class="sc">;</span>            <span class="c"># back to un-encoded</span></li></ol></pre><p>A more elegant (and safer) interface is needed.</p>
</li>
<li><a name="%3awin32"></a><b>:win32</b>
<p>On Win32 platforms this <i>experimental</i> layer uses the native "handle" IO
rather than the unix-like numeric file descriptor layer. Known to be
buggy as of perl 5.8.2.</p>
</li>
</ul>
<a name="Custom-Layers"></a><h2>Custom Layers</h2>
<p>It is possible to write custom layers in addition to the above builtin
ones, both in C/XS and Perl.  Two such layers (and one example written
in Perl using the latter) come with the Perl distribution.</p>
<ul>
<li><a name="%3aencoding"></a><b>:encoding</b>
<p>Use <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">ENCODING</span><span class="s">)</span></code>
 either in open() or binmode() to install
a layer that transparently does character set and encoding transformations,
for example from Shift-JIS to Unicode.  Note that under <code class="inline"><span class="w">stdio</span></code>

an <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 also enables <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
.  See <a href="PerlIO/encoding.html">PerlIO::encoding</a>
for more information.</p>
</li>
<li><a name="%3ammap"></a><b>:mmap</b>
<p>A layer which implements "reading" of files by using <code class="inline"><span class="i">mmap</span><span class="s">(</span><span class="s">)</span></code>
 to
make a (whole) file appear in the process's address space, and then
using that as PerlIO's "buffer". This <i>may</i> be faster in certain
circumstances for large files, and may result in less physical memory
use when multiple processes are reading the same file.</p>
<p>Files which are not <code class="inline"><span class="i">mmap</span><span class="s">(</span><span class="s">)</span></code>
-able revert to behaving like the <code class="inline"><span class="j">:</span><span class="w">perlio</span></code>

layer. Writes also behave like the <code class="inline"><span class="j">:</span><span class="w">perlio</span></code>
 layer, as <code class="inline"><span class="i">mmap</span><span class="s">(</span><span class="s">)</span></code>
 for write
needs extra house-keeping (to extend the file) which negates any advantage.</p>
<p>The <code class="inline"><span class="j">:</span><span class="w">mmap</span></code>
 layer will not exist if the platform does not support <code class="inline"><span class="i">mmap</span><span class="s">(</span><span class="s">)</span></code>
.</p>
</li>
<li><a name="%3avia"></a><b>:via</b>
<p>Use <code class="inline"><span class="j">:</span><span class="i">via</span><span class="s">(</span><span class="w">MODULE</span><span class="s">)</span></code>
 either in open() or binmode() to install a layer
that does whatever transformation (for example compression /
decompression, encryption / decryption) to the filehandle.
See <a href="PerlIO/via.html">PerlIO::via</a> for more information.</p>
</li>
</ul>
<a name="Alternatives-to-raw"></a><h2>Alternatives to raw</h2>
<p>To get a binary stream an alternate method is to use:</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;whatever&quot;</span><span class="s">)</span></li><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$fh</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>this has the advantage of being backward compatible with how such things have
had to be coded on some platforms for years.</p>
<p>To get an unbuffered stream specify an unbuffered layer (e.g. <code class="inline"><span class="j">:</span><span class="w">unix</span></code>
)
in the open call:</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;:unix&quot;</span><span class="cm">,</span><span class="i">$path</span><span class="s">)</span></li></ol></pre><a name="Defaults-and-how-to-override-them"></a><h2>Defaults and how to override them</h2>
<p>If the platform is MS-DOS like and normally does CRLF to "\n"
translation for text files then the default layers are :</p>
<pre class="verbatim"><ol><li>  <span class="w">unix</span> <span class="w">crlf</span></li></ol></pre><p>(The low level "unix" layer may be replaced by a platform specific low
level layer.)</p>
<p>Otherwise if <code class="inline"><span class="w">Configure</span></code>
 found out how to do "fast" IO using the system's
stdio, then the default layers are:</p>
<pre class="verbatim"><ol><li>  <span class="w">unix</span> <span class="w">stdio</span></li></ol></pre><p>Otherwise the default layers are</p>
<pre class="verbatim"><ol><li>  <span class="w">unix</span> <span class="w">perlio</span></li></ol></pre><p>These defaults may change once perlio has been better tested and tuned.</p>
<p>The default can be overridden by setting the environment variable
PERLIO to a space separated list of layers (<code class="inline"><span class="w">unix</span></code>
 or platform low
level layer is always pushed first).</p>
<p>This can be used to see the effect of/bugs in the various layers e.g.</p>
<pre class="verbatim"><ol><li>  cd .../perl/t</li><li>  PERLIO=stdio  ./perl harness</li><li>  PERLIO=perlio ./perl harness</li></ol></pre><p>For the various values of PERLIO see <a href="perlrun.html#PERLIO">PERLIO in perlrun</a>.</p>
<a name="Querying-the-layers-of-filehandles"></a><h2>Querying the layers of filehandles</h2>
<p>The following returns the <b>names</b> of the PerlIO layers on a filehandle.</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">@layers</span> = <span class="i">PerlIO::get_layers</span><span class="s">(</span><span class="i">$fh</span><span class="s">)</span><span class="sc">;</span> <span class="c"># Or FH, *FH, &quot;FH&quot;.</span></li></ol></pre><p>The layers are returned in the order an open() or binmode() call would
use them.  Note that the "default stack" depends on the operating
system and on the Perl version, and both the compile-time and
runtime configurations of Perl.</p>
<p>The following table summarizes the default layers on UNIX-like and
DOS-like platforms and depending on the setting of <code class="inline"><span class="i">$ENV</span>{<span class="w">PERLIO</span>}</code>
:</p>
<pre class="verbatim"><ol><li> <span class="w">PERLIO</span>     <span class="w">UNIX</span>-<span class="w">like</span>                   <span class="w">DOS</span>-<span class="w">like</span></li><li> ------     ---------                   --------</li><li> <span class="w">unset</span> <span class="q">/ &quot;&quot; unix perlio /</span> <span class="w">stdio</span> <span class="s">[</span><span class="n">1</span><span class="s">]</span>     <span class="w">unix</span> <span class="w">crlf</span></li><li> <span class="w">stdio</span>      <span class="w">unix</span> <span class="w">perlio</span> / <span class="w">stdio</span> <span class="s">[</span><span class="n">1</span><span class="s">]</span>     <span class="w">stdio</span></li><li> <span class="w">perlio</span>     <span class="w">unix</span> <span class="w">perlio</span>                 <span class="w">unix</span> <span class="w">perlio</span></li><li></li><li> <span class="c"># [1] &quot;stdio&quot; if Configure found out how to do &quot;fast stdio&quot; (depends</span></li><li> <span class="c"># on the stdio implementation) and in Perl 5.8, otherwise &quot;unix perlio&quot;</span></li></ol></pre><p>By default the layers from the input side of the filehandle are
returned; to get the output side, use the optional <code class="inline"><span class="w">output</span></code>
 argument:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">@layers</span> = <span class="i">PerlIO::get_layers</span><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="w">output</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(Usually the layers are identical on either side of a filehandle but
for example with sockets there may be differences, or if you have
been using the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> pragma.)</p>
<p>There is no set_layers(), nor does get_layers() return a tied array
mirroring the stack, or anything fancy like that.  This is not
accidental or unintentional.  The PerlIO layer stack is a bit more
complicated than just a stack (see for example the behaviour of <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
).
You are supposed to use open() and binmode() to manipulate the stack.</p>
<p><b>Implementation details follow, please close your eyes.</b></p>
<p>The arguments to layers are by default returned in parentheses after
the name of the layer, and certain layers (like <code class="inline"><span class="w">utf8</span></code>
) are not real
layers but instead flags on real layers; to get all of these returned
separately, use the optional <code class="inline"><span class="w">details</span></code>
 argument:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/my.html">my</a> <span class="i">@layer_and_args_and_flags</span> = <span class="i">PerlIO::get_layers</span><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="w">details</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The result will be up to be three times the number of layers:
the first element will be a name, the second element the arguments
(unspecified arguments will be <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>), the third element the flags,
the fourth element a name again, and so forth.</p>
<p><b>You may open your eyes now.</b></p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Nick Ing-Simmons &lt;nick@ing-simmons.net&gt;</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="functions/binmode.html">binmode</a>, <a href="functions/open.html">open</a>, <a href="perlunicode.html">perlunicode</a>, <a href="perliol.html">perliol</a>,
<a href="Encode.html">Encode</a></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="#Custom-Layers">Custom Layers</a><li><a href="#Alternatives-to-raw">Alternatives to raw</a><li><a href="#Defaults-and-how-to-override-them">Defaults and how to override them</a><li><a href="#Querying-the-layers-of-filehandles">Querying the layers of filehandles</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


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