File: perlopentut.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 (624 lines) | stat: -rw-r--r-- 40,300 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlopentut - 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>perlopentut</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-tutorials.html">Tutorials</a> &gt;
      
    
    perlopentut
  

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


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Opening-Text-Files">Opening Text Files</a><ul><li><a href="#Opening-Text-Files-for-Reading">Opening Text Files for Reading</a><li><a href="#Opening-Text-Files-for-Writing">Opening Text Files for Writing</a></ul><li><a href="#Opening-Binary-Files">Opening Binary Files</a><li><a href="#Opening-Pipes">Opening Pipes</a><li><a href="#Low-level-File-Opens-via-sysopen">Low-level File Opens via sysopen</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR-and-COPYRIGHT">AUTHOR and COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlopentut - simple recipes for opening files and pipes in Perl</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>Whenever you do I/O on a file in Perl, you do so through what in Perl is
called a <b>filehandle</b>.  A filehandle is an internal name for an external
file.  It is the job of the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> function to make the association
between the internal name and the external name, and it is the job
of the <code class="inline"><a class="l_k" href="functions/close.html">close</a></code> function to break that association.</p>
<p>For your convenience, Perl sets up a few special filehandles that are
already open when you run.  These include <code class="inline"><span class="w">STDIN</span></code>
, <code class="inline"><span class="w">STDOUT</span></code>
, <code class="inline"><span class="w">STDERR</span></code>
,
and <code class="inline"><span class="w">ARGV</span></code>
.  Since those are pre-opened, you can use them right away
without having to go to the trouble of opening them yourself:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;This is a debugging message.\n&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;Please enter something: &quot;</span><span class="sc">;</span></li><li>    <span class="i">$response</span> = <span class="q">&lt;STDIN&gt;</span> // <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;how come no input?&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;Thank you!\n&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;ARGV&gt;</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>As you see from those examples, <code class="inline"><span class="w">STDOUT</span></code>
 and <code class="inline"><span class="w">STDERR</span></code>
 are output
handles, and <code class="inline"><span class="w">STDIN</span></code>
 and <code class="inline"><span class="w">ARGV</span></code>
 are input handles.  They are
in all capital letters because they are reserved to Perl, much
like the <code class="inline"><span class="i">@ARGV</span></code>
 array and the <code class="inline"><span class="i">%ENV</span></code>
 hash are.  Their external
associations were set up by your shell.</p>
<p>You will need to open every other filehandle on your own. Although there
are many variants, the most common way to call Perl's open() function
is with three arguments and one return value:</p>
<p><code class="inline">    <i>OK</i> = open(<i>HANDLE</i>, <i>MODE</i>, <i>PATHNAME</i>)</code></p>
<p>Where:</p>
<ul>
<li><a name="_OK_"></a><b><i>OK</i></b>
<p>will be some defined value if the open succeeds, but
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> if it fails;</p>
</li>
<li><a name="_HANDLE_"></a><b><i>HANDLE</i></b>
<p>should be an undefined scalar variable to be filled in by the
<code class="inline"><a class="l_k" href="functions/open.html">open</a></code> function if it succeeds;</p>
</li>
<li><a name="_MODE_"></a><b><i>MODE</i></b>
<p>is the access mode and the encoding format to open the file with;</p>
</li>
<li><a name="_PATHNAME_"></a><b><i>PATHNAME</i></b>
<p>is the external name of the file you want opened.</p>
</li>
</ul>
<p>Most of the complexity of the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> function lies in the many
possible values that the <i>MODE</i> parameter can take on.</p>
<p>One last thing before we show you how to open files: opening
files does not (usually) automatically lock them in Perl.  See
<a href="perlfaq5.html">perlfaq5</a> for how to lock.</p>
<a name="Opening-Text-Files"></a><h1>Opening Text Files</h1>
<a name="Opening-Text-Files-for-Reading"></a><h2>Opening Text Files for Reading</h2>
<p>If you want to read from a text file, first open it in
read-only mode like this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$filename</span> = <span class="q">&quot;/some/path/to/a/textfile/goes/here&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$encoding</span> = <span class="q">&quot;:encoding(UTF-8)&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span>   = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span>     <span class="c"># this will be filled in on success</span></li><li></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&lt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename for reading: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>As with the shell, in Perl the <code class="inline"><span class="q">&quot;&lt;&quot;</span></code>
 is used to open the file in
read-only mode.  If it succeeds, Perl allocates a brand new filehandle for
you and fills in your previously undefined <code class="inline"><span class="i">$handle</span></code>
 argument with a
reference to that handle.</p>
<p>Now you may use functions like <code class="inline"><a class="l_k" href="functions/readline.html">readline</a></code>, <code class="inline"><a class="l_k" href="functions/read.html">read</a></code>, <code class="inline"><a class="l_k" href="functions/getc.html">getc</a></code>, and
<code class="inline"><a class="l_k" href="functions/sysread.html">sysread</a></code> on that handle.  Probably the most common input function
is the one that looks like an operator:</p>
<pre class="verbatim"><ol><li>    <span class="i">$line</span> = <a class="l_k" href="functions/readline.html">readline</a><span class="s">(</span><span class="i">$handle</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$line</span> = <span class="q">&lt;$handle&gt;</span><span class="sc">;</span>          <span class="c"># same thing</span></li></ol></pre><p>Because the <code class="inline"><a class="l_k" href="functions/readline.html">readline</a></code> function returns <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> at end of file or
upon error, you will sometimes see it used this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$line</span> = <span class="q">&lt;$handle&gt;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="i">$line</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># do something with $line</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>        <span class="c"># $line is not valid, so skip it</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can also just quickly <code class="inline"><a class="l_k" href="functions/die.html">die</a></code> on an undefined value this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$line</span> = <span class="q">&lt;$handle&gt;</span> // <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;no input found&quot;</span><span class="sc">;</span></li></ol></pre><p>However, if hitting EOF is an expected and normal event, you do not want to
exit simply because you have run out of input.  Instead, you probably just want
to exit an input loop.  You can then test to see if an actual error has caused
the loop to terminate, and act accordingly:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;$handle&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># do something with data in $_</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$!</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;unexpected error while reading from $filename: $!&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p><b>A Note on Encodings</b>: Having to specify the text encoding every time
might seem a bit of a bother.  To set up a default encoding for <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> so
that you don't have to supply it each time, you can use the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> pragma:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a> <span class="q">qw&lt; :encoding(UTF-8) &gt;</span><span class="sc">;</span></li></ol></pre><p>Once you've done that, you can safely omit the encoding part of the
open mode:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename for reading: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>But never use the bare <code class="inline"><span class="q">&quot;&lt;&quot;</span></code>
 without having set up a default encoding
first.  Otherwise, Perl cannot know which of the many, many, many possible
flavors of text file you have, and Perl will have no idea how to correctly
map the data in your file into actual characters it can work with.  Other
common encoding formats including <code class="inline"><span class="q">&quot;ASCII&quot;</span></code>
, <code class="inline"><span class="q">&quot;ISO-8859-1&quot;</span></code>
,
<code class="inline"><span class="q">&quot;ISO-8859-15&quot;</span></code>
, <code class="inline"><span class="q">&quot;Windows-1252&quot;</span></code>
, <code class="inline"><span class="q">&quot;MacRoman&quot;</span></code>
, and even <code class="inline"><span class="q">&quot;UTF-16LE&quot;</span></code>
.
See <a href="perlunitut.html">perlunitut</a> for more about encodings.</p>
<a name="Opening-Text-Files-for-Writing"></a><h2>Opening Text Files for Writing</h2>
<p>When you want to write to a file, you first have to decide what to do about
any existing contents of that file.  You have two basic choices here: to
preserve or to clobber.</p>
<p>If you want to preserve any existing contents, then you want to open the file
in append mode.  As in the shell, in Perl you use <code class="inline"><span class="q">&quot;&gt;&gt;&quot;</span></code>
 to open an
existing file in append mode.  <code class="inline"><span class="q">&quot;&gt;&gt;&quot;</span></code>
 creates the file if it does not
already exist.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span>   = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$filename</span> = <span class="q">&quot;/some/path/to/a/textfile/goes/here&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$encoding</span> = <span class="q">&quot;:encoding(UTF-8)&quot;</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="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename for appending: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Now you can write to that filehandle using any of <code class="inline"><a class="l_k" href="functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code>,
<code class="inline"><a class="l_k" href="functions/say.html">say</a></code>, <code class="inline"><a class="l_k" href="functions/write.html">write</a></code>, or <code class="inline"><a class="l_k" href="functions/syswrite.html">syswrite</a></code>.</p>
<p>As noted above, if the file does not already exist, then the append-mode open
will create it for you.  But if the file does already exist, its contents are
safe from harm because you will be adding your new text past the end of the
old text.</p>
<p>On the other hand, sometimes you want to clobber whatever might already be
there.  To empty out a file before you start writing to it, you can open it
in write-only mode:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span>   = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$filename</span> = <span class="q">&quot;/some/path/to/a/textfile/goes/here&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$encoding</span> = <span class="q">&quot;:encoding(UTF-8)&quot;</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="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&gt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename in write-open mode: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Here again Perl works just like the shell in that the <code class="inline"><span class="q">&quot;&gt;&quot;</span></code>
 clobbers
an existing file.</p>
<p>As with the append mode, when you open a file in write-only mode,
you can now write to that filehandle using any of <code class="inline"><a class="l_k" href="functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code>,
<code class="inline"><a class="l_k" href="functions/say.html">say</a></code>, <code class="inline"><a class="l_k" href="functions/write.html">write</a></code>, or <code class="inline"><a class="l_k" href="functions/syswrite.html">syswrite</a></code>.</p>
<p>What about read-write mode?  You should probably pretend it doesn't exist,
because opening text files in read-write mode is unlikely to do what you
would like.  See <a href="perlfaq5.html">perlfaq5</a> for details.</p>
<a name="Opening-Binary-Files"></a><h1>Opening Binary Files</h1>
<p>If the file to be opened contains binary data instead of text characters,
then the <code class="inline"><span class="w">MODE</span></code>
 argument to <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> is a little different.  Instead of
specifying the encoding, you tell Perl that your data are in raw bytes.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$filename</span> = <span class="q">&quot;/some/path/to/a/binary/file/goes/here&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$encoding</span> = <span class="q">&quot;:raw :bytes&quot;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$handle</span>   = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span>     <span class="c"># this will be filled in on success</span></li></ol></pre><p>And then open as before, choosing <code class="inline"><span class="q">&quot;&lt;&quot;</span></code>
, <code class="inline"><span class="q">&quot;&gt;&gt;&quot;</span></code>
, or
<code class="inline"><span class="q">&quot;&gt;&quot;</span></code>
 as needed:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&lt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename for reading: $!&quot;</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="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename for appending: $!&quot;</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="i">$handle</span><span class="cm">,</span> <span class="q">&quot;&gt; $encoding&quot;</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: can&#39;t open $filename in write-open mode: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Alternately, you can change to binary mode on an existing handle this way:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$handle</span><span class="s">)</span>    || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cannot binmode handle&quot;</span><span class="sc">;</span></li></ol></pre><p>This is especially handy for the handles that Perl has already opened for you.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDIN</span><span class="s">)</span>      || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cannot binmode STDIN&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDOUT</span><span class="s">)</span>     || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cannot binmode STDOUT&quot;</span><span class="sc">;</span></li></ol></pre><p>You can also pass <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> an explicit encoding to change it on the fly.
This isn't exactly "binary" mode, but we still use <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> to do it:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDIN</span><span class="cm">,</span>  <span class="q">&quot;:encoding(MacRoman)&quot;</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cannot binmode STDIN&quot;</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;:encoding(UTF-8)&quot;</span><span class="s">)</span>    || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;cannot binmode STDOUT&quot;</span><span class="sc">;</span></li></ol></pre><p>Once you have your binary file properly opened in the right mode, you can
use all the same Perl I/O functions as you used on text files.  However,
you may wish to use the fixed-size <code class="inline"><a class="l_k" href="functions/read.html">read</a></code> instead of the variable-sized
<code class="inline"><a class="l_k" href="functions/readline.html">readline</a></code> for your input.</p>
<p>Here's an example of how to copy a binary file:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$BUFSIZ</span>   = <span class="n">64</span> * <span class="s">(</span><span class="n">2</span> ** <span class="n">10</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$name_in</span>  = <span class="q">&quot;/some/input/file&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$name_out</span> = <span class="q">&quot;/some/output/flie&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$in_fh</span><span class="cm">,</span> <span class="i">$out_fh</span><span class="cm">,</span> <span class="i">$buffer</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="i">$in_fh</span><span class="cm">,</span>  <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span> <span class="i">$name_in</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: cannot open $name_in for reading: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="i">$out_fh</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="i">$name_out</span><span class="s">)</span></li><li>        || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$0: cannot open $name_out for writing: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span> <span class="s">(</span><span class="i">$in_fh</span><span class="cm">,</span> <span class="i">$out_fh</span><span class="s">)</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>               || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;binmode failed&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><a class="l_k" href="functions/read.html">read</a><span class="s">(</span><span class="i">$in_fh</span><span class="cm">,</span> <span class="i">$buffer</span><span class="cm">,</span> <span class="i">$BUFSIZ</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="i">$out_fh</span> <span class="i">$buffer</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;couldn&#39;t write to $name_out: $!&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="i">$in_fh</span><span class="s">)</span>       || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;couldn&#39;t close $name_in: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/close.html">close</a><span class="s">(</span><span class="i">$out_fh</span><span class="s">)</span>      || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;couldn&#39;t close $name_out: $!&quot;</span><span class="sc">;</span></li></ol></pre><a name="Opening-Pipes"></a><h1>Opening Pipes</h1>
<p>To be announced.</p>
<a name="Low-level-File-Opens-via-sysopen"></a><h1>Low-level File Opens via sysopen</h1>
<p>To be announced.  Or deleted.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>To be announced.</p>
<a name="AUTHOR-and-COPYRIGHT"></a><h1>AUTHOR and COPYRIGHT</h1>
<p>Copyright 2013 Tom Christiansen.</p>
<p>This documentation is free; you can redistribute it and/or modify it under
the same terms as Perl itself.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Opening-Text-Files">Opening Text Files</a><ul><li><a href="#Opening-Text-Files-for-Reading">Opening Text Files for Reading</a><li><a href="#Opening-Text-Files-for-Writing">Opening Text Files for Writing</a></ul><li><a href="#Opening-Binary-Files">Opening Binary Files</a><li><a href="#Opening-Pipes">Opening Pipes</a><li><a href="#Low-level-File-Opens-via-sysopen">Low-level File Opens via sysopen</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR-and-COPYRIGHT">AUTHOR and COPYRIGHT</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    = 'perlopentut';
  perldoc.pageAddress = 'perlopentut.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>