File: perlintro.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 (832 lines) | stat: -rw-r--r-- 70,822 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlintro - 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>perlintro</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-overview.html">Overview</a> &gt;
      
    
    perlintro
  

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


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#What-is-Perl%3f">What is Perl?</a><li><a href="#Running-Perl-programs">Running Perl programs</a><li><a href="#Safety-net">Safety net</a><li><a href="#Basic-syntax-overview">Basic syntax overview</a><li><a href="#Perl-variable-types">Perl variable types</a><li><a href="#Variable-scoping">Variable scoping</a><li><a href="#Conditional-and-looping-constructs">Conditional and looping constructs</a><li><a href="#Builtin-operators-and-functions">Builtin operators and functions</a><li><a href="#Files-and-I%2fO">Files and I/O</a><li><a href="#Regular-expressions">Regular expressions</a><li><a href="#Writing-subroutines">Writing subroutines</a><li><a href="#OO-Perl">OO Perl</a><li><a href="#Using-Perl-modules">Using Perl modules</a></ul><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlintro -- a brief introduction and overview of Perl</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This document is intended to give you a quick overview of the Perl
programming language, along with pointers to further documentation.  It
is intended as a "bootstrap" guide for those who are new to the
language, and provides just enough information for you to be able to
read other peoples' Perl and understand roughly what it's doing, or
write your own simple scripts.</p>
<p>This introductory document does not aim to be complete.  It does not
even aim to be entirely accurate.  In some cases perfection has been
sacrificed in the goal of getting the general idea across.  You are
<i>strongly</i> advised to follow this introduction with more information
from the full Perl manual, the table of contents to which can be found
in <a href="http://search.cpan.org/perldoc/perltoc">perltoc</a>.</p>
<p>Throughout this document you'll see references to other parts of the
Perl documentation.  You can read that documentation using the <code class="inline"><span class="w">perldoc</span></code>

command or whatever method you're using to read this document.</p>
<p>Throughout Perl's documentation, you'll find numerous examples intended
to help explain the discussed features.  Please keep in mind that many
of them are code fragments rather than complete programs.</p>
<p>These examples often reflect the style and preference of the author of
that piece of the documentation, and may be briefer than a corresponding
line of code in a real program.  Except where otherwise noted, you
should assume that <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
 and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
 statements
appear earlier in the "program", and that any variables used have
already been declared, even if those declarations have been omitted
to make the example easier to read.</p>
<p>Do note that the examples have been written by many different authors over
a period of several decades.  Styles and techniques will therefore differ,
although some effort has been made to not vary styles too widely in the
same sections.  Do not consider one style to be better than others - "There's
More Than One Way To Do It" is one of Perl's mottos.  After all, in your
journey as a programmer, you are likely to encounter different styles.</p>
<a name="What-is-Perl%3f"></a><h2>What is Perl?</h2>
<p>Perl is a general-purpose programming language originally developed for
text manipulation and now used for a wide range of tasks including
system administration, web development, network programming, GUI
development, and more.</p>
<p>The language is intended to be practical (easy to use, efficient,
complete) rather than beautiful (tiny, elegant, minimal).  Its major
features are that it's easy to use, supports both procedural and
object-oriented (OO) programming, has powerful built-in support for text
processing, and has one of the world's most impressive collections of
third-party modules.</p>
<p>Different definitions of Perl are given in <a href="perl.html">perl</a>, <a href="perlfaq1.html">perlfaq1</a> and
no doubt other places.  From this we can determine that Perl is different
things to different people, but that lots of people think it's at least
worth writing about.</p>
<a name="Running-Perl-programs"></a><h2>Running Perl programs</h2>
<p>To run a Perl program from the Unix command line:</p>
<pre class="verbatim"><ol><li> <span class="w">perl</span> <span class="w">progname</span>.<span class="w">pl</span></li></ol></pre><p>Alternatively, put this as the first line of your script:</p>
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/env perl</span></li></ol></pre><p>... and run the script as <i>/path/to/script.pl</i>.  Of course, it'll need
to be executable first, so <code class="inline">chmod 755 script.pl</code> (under Unix).</p>
<p>(This start line assumes you have the <b>env</b> program.  You can also put
directly the path to your perl executable, like in <code class="inline">#!/usr/bin/perl</code>
).</p>
<p>For more information, including instructions for other platforms such as
Windows and Mac OS, read <a href="perlrun.html">perlrun</a>.</p>
<a name="Safety-net"></a><h2>Safety net</h2>
<p>Perl by default is very forgiving.  In order to make it more robust
it is recommended to start every program with the following lines:</p>
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>The two additional lines request from perl to catch various common
problems in your code.  They check different things so you need both.  A
potential problem caught by <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
 will cause your code to stop
immediately when it is encountered, while <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></code>
 will merely
give a warning (like the command-line switch <b>-w</b>) and let your code run.
To read more about them check their respective manual pages at <a href="strict.html">strict</a>
and <a href="warnings.html">warnings</a>.</p>
<a name="Basic-syntax-overview"></a><h2>Basic syntax overview</h2>
<p>A Perl script or program consists of one or more statements.  These
statements are simply written in the script in a straightforward
fashion.  There is no need to have a <code class="inline"><span class="i">main</span><span class="s">(</span><span class="s">)</span></code>
 function or anything of
that kind.</p>
<p>Perl statements end in a semi-colon:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello, world&quot;</span><span class="sc">;</span></li></ol></pre><p>Comments start with a hash symbol and run to the end of the line</p>
<pre class="verbatim"><ol><li> <span class="c"># This is a comment</span></li></ol></pre><p>Whitespace is irrelevant:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a></li><li>     <span class="q">&quot;Hello, world&quot;</span></li><li>     <span class="sc">;</span></li></ol></pre><p>... except inside quoted strings:</p>
<pre class="verbatim"><ol><li> <span class="c"># this would print with a linebreak in the middle</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello</span></li><li> <span class="q"> world&quot;</span><span class="sc">;</span></li></ol></pre><p>Double quotes or single quotes may be used around literal strings:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello, world&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&#39;Hello, world&#39;</span><span class="sc">;</span></li></ol></pre><p>However, only double quotes "interpolate" variables and special
characters such as newlines (<code class="inline">\<span class="w">n</span></code>
):</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello, $name\n&quot;</span><span class="sc">;</span>     <span class="c"># works fine</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&#39;Hello, $name\n&#39;</span><span class="sc">;</span>     <span class="c"># prints $name\n literally</span></li></ol></pre><p>Numbers don't need quotes around them:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="n">42</span><span class="sc">;</span></li></ol></pre><p>You can use parentheses for functions' arguments or omit them
according to your personal taste.  They are only required
occasionally to clarify issues of precedence.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;Hello, world\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello, world\n&quot;</span><span class="sc">;</span></li></ol></pre><p>More detailed information about Perl syntax can be found in <a href="perlsyn.html">perlsyn</a>.</p>
<a name="Perl-variable-types"></a><h2>Perl variable types</h2>
<p>Perl has three main variable types: scalars, arrays, and hashes.</p>
<ul>
<li><a name="Scalars"></a><b>Scalars</b>
<p>A scalar represents a single value:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$animal</span> = <span class="q">&quot;camel&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$answer</span> = <span class="n">42</span><span class="sc">;</span></li></ol></pre><p>Scalar values can be strings, integers or floating point numbers, and Perl
will automatically convert between them as required.  There is no need
to pre-declare your variable types, but you have to declare them using
the <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> keyword the first time you use them.  (This is one of the
requirements of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
.)</p>
<p>Scalar values can be used in various ways:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animal</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The animal is $animal\n&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The square of $answer is &quot;</span><span class="cm">,</span> <span class="i">$answer</span> * <span class="i">$answer</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>There are a number of "magic" scalars with names that look like
punctuation or line noise.  These special variables are used for all
kinds of purposes, and are documented in <a href="perlvar.html">perlvar</a>.  The only one you
need to know about for now is <code class="inline"><span class="i">$_</span></code>
 which is the "default variable".
It's used as the default argument to a number of functions in Perl, and
it's set implicitly by certain looping constructs.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span>          <span class="c"># prints contents of $_ by default</span></li></ol></pre></li>
<li><a name="Arrays"></a><b>Arrays</b>
<p>An array represents a list of values:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@animals</span> = <span class="s">(</span><span class="q">&quot;camel&quot;</span><span class="cm">,</span> <span class="q">&quot;llama&quot;</span><span class="cm">,</span> <span class="q">&quot;owl&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@numbers</span> = <span class="s">(</span><span class="n">23</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">69</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@mixed</span>   = <span class="s">(</span><span class="q">&quot;camel&quot;</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">1.23</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Arrays are zero-indexed.  Here's how you get at elements in an array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animals</span>[<span class="n">0</span>]<span class="sc">;</span>              <span class="c"># prints &quot;camel&quot;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animals</span>[<span class="n">1</span>]<span class="sc">;</span>              <span class="c"># prints &quot;llama&quot;</span></li></ol></pre><p>The special variable <code class="inline"><span class="i">$#array</span></code>
 tells you the index of the last element
of an array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$mixed</span>[<span class="i">$#mixed</span>]<span class="sc">;</span>       <span class="c"># last element, prints 1.23</span></li></ol></pre><p>You might be tempted to use <code class="inline"><span class="i">$#array</span> + <span class="n">1</span></code>
 to tell you how many items there
are in an array.  Don't bother.  As it happens, using <code class="inline"><span class="i">@array</span></code>
 where Perl
expects to find a scalar value ("in scalar context") will give you the number
of elements in the array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">@animals</span> &lt; <span class="n">5</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>The elements we're getting from the array start with a <code class="inline"><span class="i">$</span></code>
 because
we're getting just a single value out of the array; you ask for a scalar,
you get a scalar.</p>
<p>To get multiple values from an array:</p>
<pre class="verbatim"><ol><li> <span class="i">@animals</span>[<span class="n">0</span><span class="cm">,</span><span class="n">1</span>]<span class="sc">;</span>                 <span class="c"># gives (&quot;camel&quot;, &quot;llama&quot;);</span></li><li> <span class="i">@animals</span>[<span class="n">0</span>..<span class="n">2</span>]<span class="sc">;</span>                <span class="c"># gives (&quot;camel&quot;, &quot;llama&quot;, &quot;owl&quot;);</span></li><li> <span class="i">@animals</span>[<span class="n">1</span>..<span class="i">$#animals</span>]<span class="sc">;</span>        <span class="c"># gives all except the first element</span></li></ol></pre><p>This is called an "array slice".</p>
<p>You can do various useful things to lists:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span>    = <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@animals</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@backwards</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <span class="i">@numbers</span><span class="sc">;</span></li></ol></pre><p>There are a couple of special arrays too, such as <code class="inline"><span class="i">@ARGV</span></code>
 (the command
line arguments to your script) and <code class="inline"><span class="i">@_</span></code>
 (the arguments passed to a
subroutine).  These are documented in <a href="perlvar.html">perlvar</a>.</p>
</li>
<li><a name="Hashes"></a><b>Hashes</b>
<p>A hash represents a set of key/value pairs:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%fruit_color</span> = <span class="s">(</span><span class="q">&quot;apple&quot;</span><span class="cm">,</span> <span class="q">&quot;red&quot;</span><span class="cm">,</span> <span class="q">&quot;banana&quot;</span><span class="cm">,</span> <span class="q">&quot;yellow&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can use whitespace and the <code class="inline"><span class="cm">=&gt;</span></code>
 operator to lay them out more
nicely:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%fruit_color</span> = <span class="s">(</span></li><li>     <span class="w">apple</span>  <span class="cm">=&gt;</span> <span class="q">&quot;red&quot;</span><span class="cm">,</span></li><li>     <span class="w">banana</span> <span class="cm">=&gt;</span> <span class="q">&quot;yellow&quot;</span><span class="cm">,</span></li><li> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To get at hash elements:</p>
<pre class="verbatim"><ol><li> <span class="i">$fruit_color</span>{<span class="q">&quot;apple&quot;</span>}<span class="sc">;</span>           <span class="c"># gives &quot;red&quot;</span></li></ol></pre><p>You can get at lists of keys and values with <code class="inline"><a class="l_k" href="functions/keys.html">keys()</a></code> and
<code class="inline"><a class="l_k" href="functions/values.html">values()</a></code>.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@fruits</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%fruit_colors</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@colors</span> = <a class="l_k" href="functions/values.html">values</a> <span class="i">%fruit_colors</span><span class="sc">;</span></li></ol></pre><p>Hashes have no particular internal order, though you can sort the keys
and loop through them.</p>
<p>Just like special scalars and arrays, there are also special hashes.
The most well known of these is <code class="inline"><span class="i">%ENV</span></code>
 which contains environment
variables.  Read all about it (and other special variables) in
<a href="perlvar.html">perlvar</a>.</p>
</li>
</ul>
<p>Scalars, arrays and hashes are documented more fully in <a href="perldata.html">perldata</a>.</p>
<p>More complex data types can be constructed using references, which allow
you to build lists and hashes within lists and hashes.</p>
<p>A reference is a scalar value and can refer to any other Perl data
type.  So by storing a reference as the value of an array or hash
element, you can easily create lists and hashes within lists and
hashes.  The following example shows a 2 level hash of hash
structure using anonymous hash references.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$variables</span> = <span class="s">{</span></li><li>     <span class="w">scalar</span>  <span class="cm">=&gt;</span>  <span class="s">{</span></li><li>                  <span class="w">description</span> <span class="cm">=&gt;</span> <span class="q">&quot;single item&quot;</span><span class="cm">,</span></li><li>                  <span class="w">sigil</span> <span class="cm">=&gt;</span> <span class="q">&#39;$&#39;</span><span class="cm">,</span></li><li>                 <span class="s">}</span><span class="cm">,</span></li><li>     <span class="w">array</span>   <span class="cm">=&gt;</span>  <span class="s">{</span></li><li>                  <span class="w">description</span> <span class="cm">=&gt;</span> <span class="q">&quot;ordered list of items&quot;</span><span class="cm">,</span></li><li>                  <span class="w">sigil</span> <span class="cm">=&gt;</span> <span class="q">&#39;@&#39;</span><span class="cm">,</span></li><li>                 <span class="s">}</span><span class="cm">,</span></li><li>     <span class="w">hash</span>    <span class="cm">=&gt;</span>  <span class="s">{</span></li><li>                  <span class="w">description</span> <span class="cm">=&gt;</span> <span class="q">&quot;key/value pairs&quot;</span><span class="cm">,</span></li><li>                  <span class="w">sigil</span> <span class="cm">=&gt;</span> <span class="q">&#39;%&#39;</span><span class="cm">,</span></li><li>                 <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Scalars begin with a $variables-&gt;{&#39;scalar&#39;}-&gt;{&#39;sigil&#39;}\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Exhaustive information on the topic of references can be found in
<a href="perlreftut.html">perlreftut</a>, <a href="perllol.html">perllol</a>, <a href="perlref.html">perlref</a> and <a href="perldsc.html">perldsc</a>.</p>
<a name="Variable-scoping"></a><h2>Variable scoping</h2>
<p>Throughout the previous section all the examples have used the syntax:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$var</span> = <span class="q">&quot;value&quot;</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> is actually not required; you could just use:</p>
<pre class="verbatim"><ol><li> <span class="i">$var</span> = <span class="q">&quot;value&quot;</span><span class="sc">;</span></li></ol></pre><p>However, the above usage will create global variables throughout your
program, which is bad programming practice.  <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> creates lexically
scoped variables instead.  The variables are scoped to the block
(i.e. a bunch of statements surrounded by curly-braces) in which they
are defined.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <span class="q">&quot;foo&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$some_condition</span> = <span class="n">1</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$some_condition</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$y</span> = <span class="q">&quot;bar&quot;</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="sc">;</span>           <span class="c"># prints &quot;foo&quot;</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span><span class="sc">;</span>           <span class="c"># prints &quot;bar&quot;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="sc">;</span>               <span class="c"># prints &quot;foo&quot;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span><span class="sc">;</span>               <span class="c"># prints nothing; $y has fallen out of scope</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> in combination with a <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
 at the top of
your Perl scripts means that the interpreter will pick up certain common
programming errors.  For instance, in the example above, the final
<code class="inline"><a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span></code>
 would cause a compile-time error and prevent you from
running the program.  Using <code class="inline"><span class="w">strict</span></code>
 is highly recommended.</p>
<a name="Conditional-and-looping-constructs"></a><h2>Conditional and looping constructs</h2>
<p>Perl has most of the usual conditional and looping constructs.  As of Perl
5.10, it even has a case/switch statement (spelled <code class="inline">given</code>
/<code class="inline">when</code>
).  See
<a href="perlsyn.html#Switch-Statements">Switch Statements in perlsyn</a> for more details.</p>
<p>The conditions can be any Perl expression.  See the list of operators in
the next section for information on comparison and boolean logic operators,
which are commonly used in conditional statements.</p>
<ul>
<li><a name="if"></a><b>if</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span> <span class="w">other</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span></li></ol></pre><p>There's also a negated version of it:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span></li></ol></pre><p>This is provided as a more readable version of <code class="inline">if (!<i>condition</i>)</code>.</p>
<p>Note that the braces are required in Perl, even if you've only got one
line in the block.  However, there is a clever way of making your one-line
conditional blocks more English like:</p>
<pre class="verbatim"><ol><li> <span class="c"># the traditional way</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$zippy</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Yow!&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># the Perlish post-condition way</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Yow!&quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$zippy</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;We have no bananas&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$bananas</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="while"></a><b>while</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span></li></ol></pre><p>There's also a negated version, for the same reason we have <code class="inline">unless</code>
:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/until.html">until</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span></li></ol></pre><p>You can also use <code class="inline">while</code>
 in a post-condition:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;LA LA LA\n&quot;</span> <a class="l_k" href="functions/while.html">while</a> <span class="n">1</span><span class="sc">;</span>          <span class="c"># loops forever</span></li></ol></pre></li>
<li><a name="for"></a><b>for</b>
<p>Exactly like C:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> &lt;= <span class="i">$max</span><span class="sc">;</span> <span class="i">$i</span>++<span class="s">)</span> <span class="s">{</span></li><li>     ...</li><li> <span class="s">}</span></li></ol></pre><p>The C style for loop is rarely needed in Perl since Perl provides
the more friendly list scanning <code class="inline">foreach</code>
 loop.</p>
</li>
<li><a name="foreach"></a><b>foreach</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;This element is $_\n&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$list</span>[<span class="i">$_</span>] <a class="l_k" href="functions/foreach.html">foreach</a> <span class="n">0</span> .. <span class="i">$max</span><span class="sc">;</span></li><li></li><li> <span class="c"># you don&#39;t have to use the default $_ either...</span></li><li> <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;The value of $key is $hash{$key}\n&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>The <code class="inline">foreach</code>
 keyword is actually a synonym for the <code class="inline">for</code>

keyword.  See <code class="inline"><a href="perlsyn.html#Foreach-Loops">Foreach Loops in perlsyn</a></code>.</p>
</li>
</ul>
<p>For more detail on looping constructs (and some that weren't mentioned in
this overview) see <a href="perlsyn.html">perlsyn</a>.</p>
<a name="Builtin-operators-and-functions"></a><h2>Builtin operators and functions</h2>
<p>Perl comes with a wide selection of builtin functions.  Some of the ones
we've already seen include <code class="inline"><a class="l_k" href="functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="functions/sort.html">sort</a></code> and <code class="inline"><a class="l_k" href="functions/reverse.html">reverse</a></code>.  A list of
them is given at the start of <a href="perlfunc.html">perlfunc</a> and you can easily read
about any given function by using <code class="inline">perldoc -f <i>functionname</i></code>.</p>
<p>Perl operators are documented in full in <a href="perlop.html">perlop</a>, but here are a few
of the most common ones:</p>
<ul>
<li><a name="Arithmetic"></a><b>Arithmetic</b>
<pre class="verbatim"><ol><li> +   <span class="w">addition</span></li><li> -   <span class="w">subtraction</span></li><li> *   <span class="w">multiplication</span></li><li> /   <span class="w">division</span></li></ol></pre></li>
<li><a name="Numeric-comparison"></a><b>Numeric comparison</b>
<pre class="verbatim"><ol><li> ==  <span class="w">equality</span></li><li> !=  <span class="w">inequality</span></li><li> &lt;   <span class="w">less</span> <span class="w">than</span></li><li> &gt;   <span class="w">greater</span> <span class="w">than</span></li><li> &lt;=  <span class="w">less</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li><li> &gt;=  <span class="w">greater</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li></ol></pre></li>
<li><a name="String-comparison"></a><b>String comparison</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/eq.html">eq</a>  <span class="w">equality</span></li><li> <a class="l_k" href="functions/ne.html">ne</a>  <span class="w">inequality</span></li><li> <a class="l_k" href="functions/lt.html">lt</a>  <span class="w">less</span> <span class="w">than</span></li><li> <a class="l_k" href="functions/gt.html">gt</a>  <span class="w">greater</span> <span class="w">than</span></li><li> <a class="l_k" href="functions/le.html">le</a>  <span class="w">less</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li><li> <a class="l_k" href="functions/ge.html">ge</a>  <span class="w">greater</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li></ol></pre><p>(Why do we have separate numeric and string comparisons?  Because we don't
have special variable types, and Perl needs to know whether to sort
numerically (where 99 is less than 100) or alphabetically (where 100 comes
before 99).</p>
</li>
<li><a name="Boolean-logic"></a><b>Boolean logic</b>
<pre class="verbatim"><ol><li> &amp;&amp;  <a class="l_k" href="functions/and.html">and</a></li><li> ||  <a class="l_k" href="functions/or.html">or</a></li><li> !   <a class="l_k" href="functions/not.html">not</a></li></ol></pre><p>(<code class="inline">and</code>
, <code class="inline">or</code>
 and <code class="inline">not</code>
 aren't just in the above table as descriptions
of the operators.  They're also supported as operators in their own
right.  They're more readable than the C-style operators, but have
different precedence to <code class="inline">&amp;&amp;</code> and friends.  Check <a href="perlop.html">perlop</a> for more
detail.)</p>
</li>
<li><a name="Miscellaneous"></a><b>Miscellaneous</b>
<pre class="verbatim"><ol><li> =   <span class="w">assignment</span></li><li> .   <span class="w">string</span> <span class="w">concatenation</span></li><li> <span class="w">x</span>   <span class="w">string</span> <span class="w">multiplication</span></li><li> ..  <span class="w">range</span> <span class="w">operator</span> <span class="s">(</span><span class="w">creates</span> <span class="w">a</span> <span class="w">list</span> <span class="w">of</span> <span class="w">numbers</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">strings</span><span class="s">)</span></li></ol></pre></li>
</ul>
<p>Many operators can be combined with a <code class="inline">=</code>
 as follows:</p>
<pre class="verbatim"><ol><li> <span class="i">$a</span> += <span class="n">1</span><span class="sc">;</span>        <span class="c"># same as $a = $a + 1</span></li><li> <span class="i">$a</span> -= <span class="n">1</span><span class="sc">;</span>        <span class="c"># same as $a = $a - 1</span></li><li> <span class="i">$a</span> .= <span class="q">&quot;\n&quot;</span><span class="sc">;</span>     <span class="c"># same as $a = $a . &quot;\n&quot;;</span></li></ol></pre><a name="Files-and-I%2fO"></a><h2>Files and I/O</h2>
<p>You can open a file for input or output using the <code class="inline"><a class="l_k" href="functions/open.html">open()</a></code> function.
It's documented in extravagant detail in <a href="perlfunc.html">perlfunc</a> and <a href="perlopentut.html">perlopentut</a>,
but in short:</p>
<pre class="verbatim"><ol><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">$in</span><span class="cm">,</span>  <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span>  <span class="q">&quot;input.txt&quot;</span><span class="s">)</span>  <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open input.txt: $!&quot;</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">$out</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span>  <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open output.txt: $!&quot;</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">$log</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;my.log&quot;</span><span class="s">)</span>     <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open my.log: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>You can read from an open filehandle using the <code class="inline">&lt;&gt;</code>
 operator.  In
scalar context it reads a single line from the filehandle, and in list
context it reads the whole file in, assigning each line to an element of
the list:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span>  = <span class="q">&lt;$in&gt;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@lines</span> = <span class="q">&lt;$in&gt;</span><span class="sc">;</span></li></ol></pre><p>Reading in the whole file at one time is called slurping.  It can
be useful but it may be a memory hog.  Most text file processing
can be done a line at a time with Perl's looping constructs.</p>
<p>The <code class="inline">&lt;&gt;</code>
 operator is most often seen in a <code class="inline">while</code>
 loop:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;$in&gt;</span><span class="s">)</span> <span class="s">{</span>     <span class="c"># assigns each line in turn to $_</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Just read in this line: $_&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>We've already seen how to print to standard output using <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code>.
However, <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code> can also take an optional first argument specifying
which filehandle to print to:</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 your final warning.\n&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$out</span> <span class="i">$record</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$log</span> <span class="i">$logmessage</span><span class="sc">;</span></li></ol></pre><p>When you're done with your filehandles, you should <code class="inline"><a class="l_k" href="functions/close.html">close()</a></code> them
(though to be honest, Perl will clean up after you if you forget):</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$in</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;$in: $!&quot;</span><span class="sc">;</span></li></ol></pre><a name="Regular-expressions"></a><h2>Regular expressions</h2>
<p>Perl's regular expression support is both broad and deep, and is the
subject of lengthy documentation in <a href="perlrequick.html">perlrequick</a>, <a href="perlretut.html">perlretut</a>, and
elsewhere.  However, in short:</p>
<ul>
<li><a name="Simple-matching"></a><b>Simple matching</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/foo/</span><span class="s">)</span>       <span class="s">{</span> ... <span class="s">}</span>  <span class="c"># true if $_ contains &quot;foo&quot;</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$a</span> =~ <span class="q">/foo/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span>  <span class="c"># true if $a contains &quot;foo&quot;</span></li></ol></pre><p>The <code class="inline"><span class="q">//</span></code>
 matching operator is documented in <a href="perlop.html">perlop</a>.  It operates on
<code class="inline"><span class="i">$_</span></code>
 by default, or can be bound to another variable using the <code class="inline">=~</code>

binding operator (also documented in <a href="perlop.html">perlop</a>).</p>
</li>
<li><a name="Simple-substitution"></a><b>Simple substitution</b>
<pre class="verbatim"><ol><li> <span class="q">s/foo/bar/</span><span class="sc">;</span>               <span class="c"># replaces foo with bar in $_</span></li><li> <span class="i">$a</span> =~ <span class="q">s/foo/bar/</span><span class="sc">;</span>         <span class="c"># replaces foo with bar in $a</span></li><li> <span class="i">$a</span> =~ <span class="q">s/foo/bar/g</span><span class="sc">;</span>        <span class="c"># replaces ALL INSTANCES of foo with bar</span></li><li>                           <span class="c"># in $a</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> substitution operator is documented in <a href="perlop.html">perlop</a>.</p>
</li>
<li><a name="More-complex-regular-expressions"></a><b>More complex regular expressions</b>
<p>You don't just have to match on fixed strings.  In fact, you can match
on just about anything you could dream of by using more complex regular
expressions.  These are documented at great length in <a href="perlre.html">perlre</a>, but for
the meantime, here's a quick cheat sheet:</p>
<pre class="verbatim"><ol><li> .                   <span class="w">a</span> <span class="w">single</span> <span class="w">character</span></li><li> \<span class="q">s                  a whitespace character</span> <span class="s">(</span><span class="w">space</span><span class="cm">,</span> <span class="w">tab</span><span class="cm">,</span> <span class="w">newline</span><span class="cm">,</span></li><li>                     ...<span class="s">)</span></li><li> \<span class="w">S</span>                  <span class="w">non</span>-<span class="w">whitespace</span> <span class="w">character</span></li><li> \<span class="w">d</span>                  <span class="w">a</span> <span class="w">digit</span> <span class="s">(</span><span class="n">0</span>-<span class="n">9</span><span class="s">)</span></li><li> \<span class="w">D</span>                  <span class="w">a</span> <span class="w">non</span>-<span class="w">digit</span></li><li> \<span class="w">w</span>                  <span class="w">a</span> <span class="w">word</span> <span class="w">character</span> <span class="s">(</span><span class="w">a</span>-z<span class="cm">,</span> <span class="w">A</span>-<span class="w">Z</span><span class="cm">,</span> <span class="n">0</span>-<span class="n">9</span><span class="cm">,</span> <span class="w">_</span><span class="s">)</span></li><li> \<span class="w">W</span>                  <span class="w">a</span> <span class="w">non</span>-<span class="w">word</span> <span class="w">character</span></li><li> <span class="s">[</span><span class="w">aeiou</span><span class="s">]</span>             <span class="w">matches</span> <span class="w">a</span> <span class="w">single</span> <span class="w">character</span> <span class="w">in</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a> <span class="w">set</span></li><li> <span class="s">[</span>^<span class="w">aeiou</span><span class="s">]</span>            <span class="w">matches</span> <span class="w">a</span> <span class="w">single</span> <span class="w">character</span> <span class="w">outside</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a></li><li>                     <span class="i">set</span></li><li> <span class="s">(</span><span class="w">foo</span>|<span class="w">bar</span>|<span class="w">baz</span><span class="s">)</span>       <span class="w">matches</span> <span class="w">any</span> <span class="w">of</span> <span class="w">the</span> <span class="w">alternatives</span> <span class="w">specified</span></li><li></li><li> ^                   <span class="w">start</span> <span class="w">of</span> <span class="w">string</span></li><li> <span class="i">$                   end</span> <span class="w">of</span> <span class="w">string</span></li></ol></pre><p>Quantifiers can be used to specify how many of the previous thing you
want to match on, where "thing" means either a literal character, one
of the metacharacters listed above, or a group of characters or
metacharacters in parentheses.</p>
<pre class="verbatim"><ol><li> <span class="i">*                   zero</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> +                   <span class="w">one</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> ?                   <span class="w">zero</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">one</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="s">}</span>                 <span class="w">matches</span> <span class="w">exactly</span> <span class="n">3</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="cm">,</span><span class="n">6</span><span class="s">}</span>               <span class="w">matches</span> <span class="w">between</span> <span class="n">3</span> <a class="l_k" href="functions/and.html">and</a> <span class="n">6</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="cm">,</span><span class="s">}</span>                <span class="w">matches</span> <span class="n">3</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li></ol></pre><p>Some brief examples:</p>
<pre class="verbatim"><ol><li> <span class="q">/^\d+/</span>              <span class="w">string</span> <span class="w">starts</span> <span class="w">with</span> <span class="w">one</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">digits</span></li><li> <span class="q">/^$/</span>                <span class="w">nothing</span> <span class="w">in</span> <span class="w">the</span> <span class="w">string</span> <span class="s">(</span><span class="w">start</span> <a class="l_k" href="functions/and.html">and</a> <span class="w">end</span> <span class="w">are</span></li><li>                     <span class="w">adjacent</span><span class="s">)</span></li><li> /<span class="s">(</span>\<span class="w">d</span>\<span class="q">s){3}/         three digits, each followed by a whitespace</span></li><li>                     <span class="q">                     character (eg &quot;3 4 5 &quot;)</span></li><li> <span class="q"> /(a.)</span>+<span class="q">/             matches a string in which every odd-numbered</span></li><li>                     <span class="q">                     letter is a (eg &quot;abacadaf&quot;)</span></li><li></li><li> <span class="q"> # This loop reads from STDIN, and prints non-blank lines:</span></li><li> <span class="q"> while (&lt;&gt;) {</span></li><li>     <span class="q">     next if /</span>^<span class="i">$/</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="Parentheses-for-capturing"></a><b>Parentheses for capturing</b>
<p>As well as grouping, parentheses serve a second purpose.  They can be
used to capture the results of parts of the regexp match for later use.
The results end up in <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
 and so on.</p>
<pre class="verbatim"><ol><li> <span class="c"># a cheap and nasty way to break an email address up into parts</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$email</span> =~ <span class="q">/([^@]+)@(.+)/</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Username is $1\n&quot;</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hostname is $2\n&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a name="Other-regexp-features"></a><b>Other regexp features</b>
<p>Perl regexps also support backreferences, lookaheads, and all kinds of
other complex details.  Read all about them in <a href="perlrequick.html">perlrequick</a>,
<a href="perlretut.html">perlretut</a>, and <a href="perlre.html">perlre</a>.</p>
</li>
</ul>
<a name="Writing-subroutines"></a><h2>Writing subroutines</h2>
<p>Writing subroutines is easy:</p>
<pre class="verbatim"><ol><li><a name="logger"></a> sub <span class="m">logger</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$logfile</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;my.log&quot;</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Could not open my.log: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$logfile</span> <span class="i">$logmessage</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Now we can use the subroutine just as any other built-in function:</p>
<pre class="verbatim"><ol><li> <span class="i">logger</span><span class="s">(</span><span class="q">&quot;We have a logger subroutine!&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>What's that <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code>?  Well, the arguments to a subroutine are available
to us as a special array called <code class="inline"><span class="i">@_</span></code>
 (see <a href="perlvar.html">perlvar</a> for more on that).
The default argument to the <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code> function just happens to be <code class="inline"><span class="i">@_</span></code>
.
So <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></code>
 shifts the first item off the list of
arguments and assigns it to <code class="inline"><span class="i">$logmessage</span></code>
.</p>
<p>We can manipulate <code class="inline"><span class="i">@_</span></code>
 in other ways too:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$logmessage</span><span class="cm">,</span> <span class="i">$priority</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>       <span class="c"># common</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span>                 <span class="c"># uncommon, and ugly</span></li></ol></pre><p>Subroutines can also return values:</p>
<pre class="verbatim"><ol><li><a name="square"></a> sub <span class="m">square</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$num</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$result</span> = <span class="i">$num</span> * <span class="i">$num</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Then use it like:</p>
<pre class="verbatim"><ol><li> <span class="i">$sq</span> = <span class="i">square</span><span class="s">(</span><span class="n">8</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>For more information on writing subroutines, see <a href="perlsub.html">perlsub</a>.</p>
<a name="OO-Perl"></a><h2>OO Perl</h2>
<p>OO Perl is relatively simple and is implemented using references which
know what sort of object they are based on Perl's concept of packages.
However, OO Perl is largely beyond the scope of this document.
Read <a href="perlootut.html">perlootut</a> and <a href="perlobj.html">perlobj</a>.</p>
<p>As a beginning Perl programmer, your most common use of OO Perl will be
in using third-party modules, which are documented below.</p>
<a name="Using-Perl-modules"></a><h2>Using Perl modules</h2>
<p>Perl modules provide a range of features to help you avoid reinventing
the wheel, and can be downloaded from CPAN ( <a href="http://www.cpan.org/">http://www.cpan.org/</a> ).  A
number of popular modules are included with the Perl distribution
itself.</p>
<p>Categories of modules range from text manipulation to network protocols
to database integration to graphics.  A categorized list of modules is
also available from CPAN.</p>
<p>To learn how to install modules you download from CPAN, read
<a href="perlmodinstall.html">perlmodinstall</a>.</p>
<p>To learn how to use a particular module, use <code class="inline">perldoc <i>Module::Name</i></code>.
Typically you will want to <code class="inline">use <i>Module::Name</i></code>, which will then give
you access to exported functions or an OO interface to the module.</p>
<p><a href="perlfaq.html">perlfaq</a> contains questions and answers related to many common
tasks, and often provides suggestions for good CPAN modules to use.</p>
<p><a href="perlmod.html">perlmod</a> describes Perl modules in general.  <a href="perlmodlib.html">perlmodlib</a> lists the
modules which came with your Perl installation.</p>
<p>If you feel the urge to write Perl modules, <a href="perlnewmod.html">perlnewmod</a> will give you
good advice.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Kirrily "Skud" Robert &lt;skud@cpan.org&gt;</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#What-is-Perl%3f">What is Perl?</a><li><a href="#Running-Perl-programs">Running Perl programs</a><li><a href="#Safety-net">Safety net</a><li><a href="#Basic-syntax-overview">Basic syntax overview</a><li><a href="#Perl-variable-types">Perl variable types</a><li><a href="#Variable-scoping">Variable scoping</a><li><a href="#Conditional-and-looping-constructs">Conditional and looping constructs</a><li><a href="#Builtin-operators-and-functions">Builtin operators and functions</a><li><a href="#Files-and-I%2fO">Files and I/O</a><li><a href="#Regular-expressions">Regular expressions</a><li><a href="#Writing-subroutines">Writing subroutines</a><li><a href="#OO-Perl">OO Perl</a><li><a href="#Using-Perl-modules">Using Perl modules</a></ul><li><a href="#AUTHOR">AUTHOR</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


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