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

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


  <!--    -->
<ul><li><a href="#NAME">NAME
</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Declarations">Declarations
   </a><li><a href="#Comments">Comments
 </a><li><a href="#Simple-Statements">Simple Statements
   </a><li><a href="#Truth-and-Falsehood">Truth and Falsehood
       </a><li><a href="#Statement-Modifiers">Statement Modifiers
    
   </a><li><a href="#Compound-Statements">Compound Statements
    
         </a><li><a href="#Loop-Control">Loop Control
     </a><li><a href="#For-Loops">For Loops
 </a><li><a href="#Foreach-Loops">Foreach Loops
 </a><li><a href="#Basic-BLOCKs">Basic BLOCKs
</a><li><a href="#Switch-Statements">Switch Statements</a><li><a href="#Goto">Goto
</a><li><a href="#The-Ellipsis-Statement">The Ellipsis Statement










</a><li><a href="#PODs%3a-Embedded-Documentation">PODs: Embedded Documentation
 </a><li><a href="#Plain-Old-Comments-(Not!)">Plain Old Comments (Not!)
    </a><li><a href="#Experimental-Details-on-given-and-when">Experimental Details on given and when</a></ul></ul><a name="NAME"></a><h1>NAME
</h1>
<p>perlsyn - Perl syntax</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>A Perl program consists of a sequence of declarations and statements
which run from the top to the bottom.  Loops, subroutines, and other
control structures allow you to jump around within the code.</p>
<p>Perl is a <b>free-form</b> language: you can format and indent it however
you like.  Whitespace serves mostly to separate tokens, unlike
languages like Python where it is an important part of the syntax,
or Fortran where it is immaterial.</p>
<p>Many of Perl's syntactic elements are <b>optional</b>.  Rather than
requiring you to put parentheses around every function call and
declare every variable, you can often leave such explicit elements off
and Perl will figure out what you meant.  This is known as <b>Do What I
Mean</b>, abbreviated <b>DWIM</b>.  It allows programmers to be <b>lazy</b> and to
code in a style with which they are comfortable.</p>
<p>Perl <b>borrows syntax</b> and concepts from many languages: awk, sed, C,
Bourne Shell, Smalltalk, Lisp and even English.  Other
languages have borrowed syntax from Perl, particularly its regular
expression extensions.  So if you have programmed in another language
you will see familiar pieces in Perl.  They often work the same, but
see <a href="perltrap.html">perltrap</a> for information about how they differ.</p>
<a name="Declarations"></a><h2>Declarations
   </h2>
<p>The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines).  A scalar variable holds
the undefined value (<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>) until it has been assigned a defined
value, which is anything other than <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.  When used as a number,
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is treated as <code class="inline"><span class="n">0</span></code>
; when used as a string, it is treated as
the empty string, <code class="inline"><span class="q">&quot;&quot;</span></code>
; and when used as a reference that isn't being
assigned to, it is treated as an error.  If you enable warnings,
you'll be notified of an uninitialized value whenever you treat
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> as a string or a number.  Well, usually.  Boolean contexts,
such as:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$a</span><span class="s">)</span> <span class="s">{</span><span class="s">}</span></li></ol></pre><p>are exempt from warnings (because they care about truth rather than
definedness).  Operators such as <code class="inline">++</code>
, <code class="inline">--</code>
, <code class="inline">+=</code>
,
<code class="inline">-=</code>
, and <code class="inline">.=</code>
, that operate on undefined variables such as:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/undef.html">undef</a> <span class="i">$a</span><span class="sc">;</span></li><li>    <span class="i">$a</span>++<span class="sc">;</span></li></ol></pre><p>are also always exempt from such warnings.</p>
<p>A declaration can be put anywhere a statement can, but has no effect on
the execution of the primary sequence of statements: declarations all
take effect at compile time.  All declarations are typically put at
the beginning or the end of the script.  However, if you're using
lexically-scoped private variables created with <code class="inline"><a class="l_k" href="functions/my.html">my()</a></code>,
<code class="inline"><a class="l_k" href="functions/state.html">state()</a></code>, or <code class="inline"><a class="l_k" href="functions/our.html">our()</a></code>, you'll have to make sure
your format or subroutine definition is within the same block scope
as the my if you expect to be able to access those private variables.</p>
<p>Declaring a subroutine allows a subroutine name to be used as if it were a
list operator from that point forward in the program.  You can declare a
subroutine without defining it by saying <code class="inline"><a name="name"></a>sub <span class="m">name</span></code>
, thus:
</p>
<pre class="verbatim"><ol><li>    sub <span class="m">myname</span><span class="sc">;</span></li><li>    <span class="i">$me</span> = <span class="i">myname</span> <span class="i">$0</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 get myname&quot;</span><span class="sc">;</span></li></ol></pre><p>A bare declaration like that declares the function to be a list operator,
not a unary operator, so you have to be careful to use parentheses (or
<code class="inline">or</code>
 instead of <code class="inline">||</code>.)  The <code class="inline">||</code> operator binds too tightly to use after
list operators; it becomes part of the last element.  You can always use
parentheses around the list operators arguments to turn the list operator
back into something that behaves more like a function call.  Alternatively,
you can use the prototype <code class="inline"><span class="s">(</span><span class="i">$)</span></code>
 to turn the subroutine into a unary
operator:</p>
<pre class="verbatim"><ol><li>  sub <span class="m">myname ($)</span><span class="sc">;</span></li><li>  <span class="i">$me</span> = <span class="i">myname</span> <span class="i">$0</span>             || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can&#39;t get myname&quot;</span><span class="sc">;</span></li></ol></pre><p>That now parses as you'd expect, but you still ought to get in the habit of
using parentheses in that situation.  For more on prototypes, see
<a href="perlsub.html">perlsub</a>.</p>
<p>Subroutines declarations can also be loaded up with the <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> statement
or both loaded and imported into your namespace with a <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> statement.
See <a href="perlmod.html">perlmod</a> for details on this.</p>
<p>A statement sequence may contain declarations of lexically-scoped
variables, but apart from declaring a variable name, the declaration acts
like an ordinary statement, and is elaborated within the sequence of
statements as if it were an ordinary statement.  That means it actually
has both compile-time and run-time effects.</p>
<a name="Comments"></a><h2>Comments
 </h2>
<p>Text from a <code class="inline"><span class="q">&quot;#&quot;</span></code>
 character until the end of the line is a comment,
and is ignored.  Exceptions include <code class="inline"><span class="q">&quot;#&quot;</span></code>
 inside a string or regular
expression.</p>
<a name="Simple-Statements"></a><h2>Simple Statements
   </h2>
<p>The only kind of simple statement is an expression evaluated for its
side-effects.  Every simple statement must be terminated with a
semicolon, unless it is the final statement in a block, in which case
the semicolon is optional.  But put the semicolon in anyway if the
block takes up more than one line, because you may eventually add
another line.  Note that there are operators like <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
, and
<code class="inline"><a class="l_k" href="functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
 that <i>look</i> like compound statements, but aren't--they're just
TERMs in an expression--and thus need an explicit termination when used
as the last item in a statement.</p>
<a name="Truth-and-Falsehood"></a><h2>Truth and Falsehood
       </h2>
<p>The number 0, the strings <code class="inline"><span class="q">&#39;0&#39;</span></code>
 and <code class="inline"><span class="q">&quot;&quot;</span></code>
, the empty list <code class="inline"><span class="s">(</span><span class="s">)</span></code>
, and
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> are all false in a boolean context.  All other values are true.
Negation of a true value by <code class="inline">!</code>
 or <code class="inline">not</code>
 returns a special false value.
When evaluated as a string it is treated as <code class="inline"><span class="q">&quot;&quot;</span></code>
, but as a number, it
is treated as 0.  Most Perl operators
that return true or false behave this way.</p>
<a name="Statement-Modifiers"></a><h2>Statement Modifiers
    
   </h2>
<p>Any simple statement may optionally be followed by a <i>SINGLE</i> modifier,
just before the terminating semicolon (or block ending).  The possible
modifiers are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="w">EXPR</span></li><li>    <a class="l_k" href="functions/unless.html">unless</a> <span class="w">EXPR</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="w">EXPR</span></li><li>    <a class="l_k" href="functions/until.html">until</a> <span class="w">EXPR</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="w">LIST</span></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="w">LIST</span></li><li>    <a class="l_k" href="functions/when.html">when</a> <span class="w">EXPR</span></li></ol></pre><p>The <code class="inline"><span class="w">EXPR</span></code>
 following the modifier is referred to as the "condition".
Its truth or falsehood determines how the modifier will behave.</p>
<p><code class="inline">if</code>
 executes the statement once <i>if</i> and only if the condition is
true.  <code class="inline">unless</code>
 is the opposite, it executes the statement <i>unless</i>
the condition is true (that is, if the condition is false).</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Basset hounds got long ears&quot;</span> if <a class="l_k" href="functions/length.html">length</a> <span class="i">$ear</span> &gt;= <span class="n">10</span><span class="sc">;</span></li><li>    <span class="i">go_outside</span><span class="s">(</span><span class="s">)</span> and <span class="i">play</span><span class="s">(</span><span class="s">)</span> unless <span class="i">$is_raining</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/for.html">for(each)</a></code> modifier is an iterator: it executes the statement once
for each item in the LIST (with <code class="inline"><span class="i">$_</span></code>
 aliased to each item in turn).</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Hello $_!\n&quot;</span> <a class="l_k" href="functions/for.html">for</a> <span class="q">qw(world Dolly nurse)</span><span class="sc">;</span></li></ol></pre><p><code class="inline">while</code>
 repeats the statement <i>while</i> the condition is true.
<code class="inline">until</code>
 does the opposite, it repeats the statement <i>until</i> the
condition is true (or while the condition is false):</p>
<pre class="verbatim"><ol><li>    <span class="c"># Both of these count from 0 to 10.</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$i</span>++ while <span class="i">$i</span> &lt;= <span class="n">10</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$j</span>++ until <span class="i">$j</span> &gt;  <span class="n">10</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline">while</code>
 and <code class="inline">until</code>
 modifiers have the usual "<code class="inline">while</code>
 loop"
semantics (conditional evaluated first), except when applied to a
<code class="inline"><a class="l_k" href="functions/do.html">do</a></code>-BLOCK (or to the Perl4 <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>-SUBROUTINE statement), in
which case the block executes once before the conditional is
evaluated.</p>
<p>This is so that you can write loops like:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>        <span class="i">$line</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li><li>        ...</li><li>    <span class="s">}</span> <a class="l_k" href="functions/until.html">until</a> !<a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$line</span><span class="s">)</span> || <span class="i">$line</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&quot;.\n&quot;</span></li></ol></pre><p>See <a href="functions/do.html">do</a>.  Note also that the loop control statements described
later will <i>NOT</i> work in this construct, because modifiers don't take
loop labels.  Sorry.  You can always put another block inside of it
(for <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>/<code class="inline"><a class="l_k" href="functions/redo.html">redo</a></code>) or around it (for <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>) to do that sort of thing.
  </p>
<p>For <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> or <code class="inline"><a class="l_k" href="functions/redo.html">redo</a></code>, just double the braces:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span><span class="s">{</span></li><li>        <a class="l_k" href="functions/next.html">next</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$x</span> == <span class="i">$y</span><span class="sc">;</span></li><li>        <span class="c"># do something here</span></li><li>    <span class="s">}</span><span class="s">}</span> <a class="l_k" href="functions/until.html">until</a> <span class="i">$x</span>++ &gt; <span class="i">$z</span><span class="sc">;</span></li></ol></pre><p>For <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>, you have to be more elaborate and put braces around it:
</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>            <a class="l_k" href="functions/last.html">last</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$x</span> == <span class="i">$y</span>**<span class="n">2</span><span class="sc">;</span></li><li>            <span class="c"># do something here</span></li><li>        <span class="s">}</span> <a class="l_k" href="functions/while.html">while</a> <span class="i">$x</span>++ &lt;= <span class="i">$z</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>If you need both <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> and <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>, you have to do both and also use a
loop label:</p>
<pre class="verbatim"><ol><li>    <span class="j">LOOP:</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span><span class="s">{</span></li><li>            <a class="l_k" href="functions/next.html">next</a> <a class="l_k" href="functions/if.html">if</a> <span class="i">$x</span> == <span class="i">$y</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/last.html">last</a> <span class="j">LOOP</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$x</span> == <span class="i">$y</span>**<span class="n">2</span><span class="sc">;</span></li><li>            <span class="c"># do something here</span></li><li>        <span class="s">}</span><span class="s">}</span> <a class="l_k" href="functions/until.html">until</a> <span class="i">$x</span>++ &gt; <span class="i">$z</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p><b>NOTE:</b> The behaviour of a <code class="inline"><a class="l_k" href="functions/my.html">my</a></code>, <code class="inline"><a class="l_k" href="functions/state.html">state</a></code>, or
<code class="inline"><a class="l_k" href="functions/our.html">our</a></code> modified with a statement modifier conditional
or loop construct (for example, <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> if ...</code>
) is
<b>undefined</b>.  The value of the <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> variable may be <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, any
previously assigned value, or possibly anything else.  Don't rely on
it.  Future versions of perl might do something different from the
version of perl you try it out on.  Here be dragons.
</p>
<p>The <code class="inline">when</code>
 modifier is an experimental feature that first appeared in Perl
5.14.  To use it, you should include a <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span></code>
 declaration.
(Technically, it requires only the <code class="inline">switch</code>
 feature, but that aspect of it
was not available before 5.14.)  Operative only from within a <code class="inline">foreach</code>

loop or a <code class="inline">given</code>
 block, it executes the statement only if the smartmatch
<code class="inline">$_ ~~ <i>EXPR</i></code> is true.  If the statement executes, it is followed by
a <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> from inside a <code class="inline">foreach</code>
 and <code class="inline"><span class="w">break</span></code>
 from inside a <code class="inline">given</code>
.</p>
<p>Under the current implementation, the <code class="inline">foreach</code>
 loop can be
anywhere within the <code class="inline">when</code>
 modifier's dynamic scope, but must be
within the <code class="inline">given</code>
 block's lexical scope.  This restriction may
be relaxed in a future release.  See <a href="#Switch-Statements">Switch Statements</a> below.</p>
<a name="Compound-Statements"></a><h2>Compound Statements
    
         </h2>
<p>In Perl, a sequence of statements that defines a scope is called a block.
Sometimes a block is delimited by the file containing it (in the case
of a required file, or the program as a whole), and sometimes a block
is delimited by the extent of a string (in the case of an eval).</p>
<p>But generally, a block is delimited by curly brackets, also known as braces.
We will call this syntactic construct a BLOCK.</p>
<p>The following compound statements may be used to control flow:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/else.html">else</a> <span class="w">BLOCK</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> ...</li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> ... <a class="l_k" href="functions/else.html">else</a> <span class="w">BLOCK</span></li><li></li><li>    <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/else.html">else</a> <span class="w">BLOCK</span></li><li>    <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> ...</li><li>    <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> ... <a class="l_k" href="functions/else.html">else</a> <span class="w">BLOCK</span></li><li></li><li>    <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/until.html">until</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/until.html">until</a> <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="w">EXPR</span><span class="sc">;</span> <span class="w">EXPR</span><span class="sc">;</span> <span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">VAR</span> <span class="s">(</span><span class="w">LIST</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">VAR</span> <span class="s">(</span><span class="w">LIST</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span><span class="w">EXPR</span><span class="sc">;</span> <span class="w">EXPR</span><span class="sc">;</span> <span class="w">EXPR</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/foreach.html">foreach</a> <span class="w">VAR</span> <span class="s">(</span><span class="w">LIST</span><span class="s">)</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <a class="l_k" href="functions/foreach.html">foreach</a> <span class="w">VAR</span> <span class="s">(</span><span class="w">LIST</span><span class="s">)</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">LABEL</span> <span class="w">BLOCK</span></li><li>    <span class="w">LABEL</span> <span class="w">BLOCK</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="w">BLOCK</span></li><li></li><li>    <span class="w">PHASE</span> <span class="w">BLOCK</span></li></ol></pre><p>The experimental <code class="inline">given</code>
 statement is <i>not automatically enabled</i>; see
<a href="#Switch-Statements">Switch Statements</a> below for how to do so, and the attendant caveats.</p>
<p>Unlike in C and Pascal, in Perl these are all defined in terms of BLOCKs,
not statements.  This means that the curly brackets are <i>required</i>--no
dangling statements allowed.  If you want to write conditionals without
curly brackets, there are several other ways to do it.  The following
all do the same thing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span>!<a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $FOO: $!&quot;</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $FOO: $!&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="s">)</span>  || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $FOO: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="s">)</span> ? <span class="s">(</span><span class="s">)</span> <span class="co">:</span> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $FOO: $!&quot;</span><span class="sc">;</span></li><li>        <span class="c"># a bit exotic, that last one</span></li></ol></pre><p>The <code class="inline">if</code>
 statement is straightforward.  Because BLOCKs are always
bounded by curly brackets, there is never any ambiguity about which
<code class="inline">if</code>
 an <code class="inline">else</code>
 goes with.  If you use <code class="inline">unless</code>
 in place of <code class="inline">if</code>
,
the sense of the test is reversed.  Like <code class="inline">if</code>
, <code class="inline">unless</code>
 can be followed
by <code class="inline">else</code>
.  <code class="inline">unless</code>
 can even be followed by one or more <code class="inline">elsif</code>

statements, though you may want to think twice before using that particular
language construct, as everyone reading your code will have to think at least
twice before they can understand what's going on.</p>
<p>The <code class="inline">while</code>
 statement executes the block as long as the expression is
<a href="#Truth-and-Falsehood">true</a>.
The <code class="inline">until</code>
 statement executes the block as long as the expression is
false.
The LABEL is optional, and if present, consists of an identifier followed
by a colon.  The LABEL identifies the loop for the loop control
statements <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>, <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>, and <code class="inline"><a class="l_k" href="functions/redo.html">redo</a></code>.
If the LABEL is omitted, the loop control statement
refers to the innermost enclosing loop.  This may include dynamically
looking back your call-stack at run time to find the LABEL.  Such
desperate behavior triggers a warning if you use the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>

pragma or the <b>-w</b> flag.</p>
<p>If there is a <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> BLOCK, it is always executed just before the
conditional is about to be evaluated again.  Thus it can be used to
increment a loop variable, even when the loop has been continued via
the <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> statement.</p>
<p>When a block is preceding by a compilation phase keyword such as <code class="inline">BEGIN</code>
,
<code class="inline">END</code>
, <code class="inline">INIT</code>
, <code class="inline">CHECK</code>
, or <code class="inline">UNITCHECK</code>
, then the block will run only
during the corresponding phase of execution.  See <a href="perlmod.html">perlmod</a> for more details.</p>
<p>Extension modules can also hook into the Perl parser to define new
kinds of compound statements.  These are introduced by a keyword which
the extension recognizes, and the syntax following the keyword is
defined entirely by the extension.  If you are an implementor, see
<a href="perlapi.html#PL_keyword_plugin">PL_keyword_plugin in perlapi</a> for the mechanism.  If you are using such
a module, see the module's documentation for details of the syntax that
it defines.</p>
<a name="Loop-Control"></a><h2>Loop Control
     </h2>
<p>The <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> command starts the next iteration of the loop:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/next.html">next</a> <span class="j">LINE</span> <a class="l_k" href="functions/if.html">if</a> <span class="q">/^#/</span><span class="sc">;</span>      <span class="c"># discard comments</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/last.html">last</a></code> command immediately exits the loop in question.  The
<code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> block, if any, is not executed:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/last.html">last</a> <span class="j">LINE</span> <a class="l_k" href="functions/if.html">if</a> <span class="q">/^$/</span><span class="sc">;</span>      <span class="c"># exit when done with header</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/redo.html">redo</a></code> command restarts the loop block without evaluating the
conditional again.  The <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> block, if any, is <i>not</i> executed.
This command is normally used by programs that want to lie to themselves
about what was just input.</p>
<p>For example, when processing a file like <i>/etc/termcap</i>.
If your input lines might end in backslashes to indicate continuation, you
want to skip ahead and get the next record.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">s/\\$//</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="i">$_</span> .= &lt;&gt;<span class="sc">;</span></li><li>            <a class="l_k" href="functions/redo.html">redo</a> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="c"># now process $_</span></li><li>    <span class="s">}</span></li></ol></pre><p>which is Perl shorthand for the more explicitly written version:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$line</span> = <span class="q">&lt;ARGV&gt;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$line</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$line</span> =~ <span class="q">s/\\$//</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="i">$line</span> .= <span class="q">&lt;ARGV&gt;</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/redo.html">redo</a> <span class="j">LINE</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="c"># not eof(ARGV)!</span></li><li>        <span class="s">}</span></li><li>        <span class="c"># now process $line</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that if there were a <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> block on the above code, it would
get executed only on lines discarded by the regex (since redo skips the
continue block).  A continue block is often used to reset line counters
or <code class="inline"><a class="l_k" href="functions/m.html">m?pat?</a></code> one-time matches:</p>
<pre class="verbatim"><ol><li>    <span class="c"># inspired by :1,$g/fred/s//WILMA/</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>        <span class="q">m?(fred)?</span>    &amp;&amp; <span class="q">s//WILMA $1 WILMA/</span><span class="sc">;</span></li><li>        <span class="q">m?(barney)?</span>  &amp;&amp; <span class="q">s//BETTY $1 BETTY/</span><span class="sc">;</span></li><li>        <span class="q">m?(homer)?</span>   &amp;&amp; <span class="q">s//MARGE $1 MARGE/</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$ARGV $.: $_&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/close.html">close</a> <span class="w">ARGV</span>  <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/eof.html">eof</a><span class="sc">;</span>             <span class="c"># reset $.</span></li><li>        <a class="l_k" href="functions/reset.html">reset</a>       <a class="l_k" href="functions/if.html">if</a> <a class="l_k" href="functions/eof.html">eof</a><span class="sc">;</span>             <span class="c"># reset ?pat?</span></li><li>    <span class="s">}</span></li></ol></pre><p>If the word <code class="inline">while</code>
 is replaced by the word <code class="inline">until</code>
, the sense of the
test is reversed, but the conditional is still tested before the first
iteration.</p>
<p>Loop control statements don't work in an <code class="inline">if</code>
 or <code class="inline">unless</code>
, since
they aren't loops.  You can double the braces to make them such, though.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/pattern/</span><span class="s">)</span> <span class="s">{</span><span class="s">{</span></li><li>        <a class="l_k" href="functions/last.html">last</a> <a class="l_k" href="functions/if.html">if</a> <span class="q">/fred/</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/next.html">next</a> <a class="l_k" href="functions/if.html">if</a> <span class="q">/barney/</span><span class="sc">;</span> <span class="c"># same effect as &quot;last&quot;,</span></li><li>                          <span class="c"># but doesn&#39;t document as well</span></li><li>        <span class="c"># do something here</span></li><li>    <span class="s">}</span><span class="s">}</span></li></ol></pre><p>This is caused by the fact that a block by itself acts as a loop that
executes once, see <a href="#Basic-BLOCKs">Basic BLOCKs</a>.</p>
<p>The form <code class="inline">while/if BLOCK BLOCK</code>, available in Perl 4, is no longer
available.   Replace any occurrence of <code class="inline">if <span class="w">BLOCK</span></code>
 by <code class="inline">if <span class="s">(</span><a class="l_k" href="functions/do.html">do</a> <span class="w">BLOCK</span><span class="s">)</span></code>
.</p>
<a name="For-Loops"></a><h2>For Loops
 </h2>
<p>Perl's C-style <code class="inline">for</code>
 loop works like the corresponding <code class="inline">while</code>
 loop;
that means that this:</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">1</span><span class="sc">;</span> <span class="i">$i</span> &lt; <span class="n">10</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>is the same as this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$i</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">$i</span> &lt; <span class="n">10</span><span class="s">)</span> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">{</span></li><li>        <span class="i">$i</span>++<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>There is one minor difference: if variables are declared with <code class="inline"><a class="l_k" href="functions/my.html">my</a></code>
in the initialization section of the <code class="inline">for</code>
, the lexical scope of
those variables is exactly the <code class="inline">for</code>
 loop (the body of the loop
and the control sections).
</p>
<p>As a special case, if the test in the <code class="inline">for</code>
 loop (or the corresponding
<code class="inline">while</code>
 loop) is empty, it is treated as true.  That is, both</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>and</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="s">)</span> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><p>are treated as infinite loops.</p>
<p>Besides the normal array index looping, <code class="inline">for</code>
 can lend itself
to many other interesting applications.  Here's one that avoids the
problem you get into if you explicitly test for end-of-file on
an interactive file descriptor causing your program to appear to
hang.
  </p>
<pre class="verbatim"><ol><li>    <span class="i">$on_a_tty</span> = -t <span class="w">STDIN</span> &amp;&amp; -t <span class="w">STDOUT</span><span class="sc">;</span></li><li><a name="prompt"></a>    sub <span class="m">prompt</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;yes? &quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$on_a_tty</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span> <span class="i">prompt</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span> <span class="i">prompt</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># do something</span></li><li>    <span class="s">}</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/readline.html">readline</a></code> (or the operator form, <code class="inline"><span class="q">&lt;EXPR&gt;</span></code>
) as the
conditional of a <code class="inline">for</code>
 loop is shorthand for the following.  This
behaviour is the same as a <code class="inline">while</code>
 loop conditional.
 </p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span> <span class="i">prompt</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span> <span class="i">$_</span> = <span class="q">&lt;STDIN&gt;</span> <span class="s">)</span><span class="sc">;</span> <span class="i">prompt</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># do something</span></li><li>    <span class="s">}</span></li></ol></pre><a name="Foreach-Loops"></a><h2>Foreach Loops
 </h2>
<p>The <code class="inline">foreach</code>
 loop iterates over a normal list value and sets the scalar
variable VAR to be each element of the list in turn.  If the variable
is preceded with the keyword <code class="inline"><a class="l_k" href="functions/my.html">my</a></code>, then it is lexically scoped, and
is therefore visible only within the loop.  Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop.  If the variable was previously declared with <code class="inline"><a class="l_k" href="functions/my.html">my</a></code>, it uses
that variable instead of the global one, but it's still localized to
the loop.  This implicit localization occurs <i>only</i> in a <code class="inline">foreach</code>

loop.
 </p>
<p>The <code class="inline">foreach</code>
 keyword is actually a synonym for the <code class="inline">for</code>
 keyword, so
you can use either.  If VAR is omitted, <code class="inline"><span class="i">$_</span></code>
 is set to each value.
</p>
<p>If any element of LIST is an lvalue, you can modify it by modifying
VAR inside the loop.  Conversely, if any element of LIST is NOT an
lvalue, any attempt to modify that element will fail.  In other words,
the <code class="inline">foreach</code>
 loop index variable is an implicit alias for each item
in the list that you're looping over.
</p>
<p>If any part of LIST is an array, <code class="inline">foreach</code>
 will get very confused if
you add or remove elements within the loop body, for example with
<code class="inline"><a class="l_k" href="functions/splice.html">splice</a></code>.   So don't do that.
</p>
<p><code class="inline">foreach</code>
 probably won't do what you expect if VAR is a tied or other
special variable.   Don't do that either.</p>
<p>As of Perl 5.22, there is an experimental variant of this loop that accepts
a variable preceded by a backslash for VAR, in which case the items in the
LIST must be references.  The backslashed variable will become an alias
to each referenced item in the LIST, which must be of the correct type.
The variable needn't be a scalar in this case, and the backslash may be
followed by <code class="inline"><a class="l_k" href="functions/my.html">my</a></code>.  To use this form, you must enable the <code class="inline"><span class="w">refaliasing</span></code>

feature via <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span></code>
.  (See <a href="feature.html">feature</a>.  See also <a href="perlref.html#Assigning-to-References">Assigning to References in perlref</a>.)</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@ary</span><span class="s">)</span> <span class="s">{</span> <span class="q">s/foo/bar/</span> <span class="s">}</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">$elem</span> <span class="s">(</span><span class="i">@elements</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$elem</span> *= <span class="n">2</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="i">$count</span> <span class="s">(</span><a class="l_k" href="functions/reverse.html">reverse</a><span class="s">(</span><span class="n">1</span>..<span class="n">10</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;BOOM&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$count</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/sleep.html">sleep</a><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="n">1</span>..<span class="n">15</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Merry Christmas\n&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/foreach.html">foreach</a> <span class="i">$item</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/:[\\\n:]*/</span><span class="cm">,</span> <span class="i">$ENV</span>{<span class="w">TERMCAP</span>}<span class="s">)</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;Item: $item\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">&quot;refaliasing&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&quot;experimental::refaliasing&quot;</span><span class="sc">;</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">%hash</span> <span class="s">(</span><span class="i">@array_of_hash_references</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># do something which each %hash</span></li><li>    <span class="s">}</span></li></ol></pre><p>Here's how a C programmer might code up a particular algorithm in Perl:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> &lt; <span class="i">@ary1</span><span class="sc">;</span> <span class="i">$i</span>++<span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$j</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$j</span> &lt; <span class="i">@ary2</span><span class="sc">;</span> <span class="i">$j</span>++<span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$ary1</span>[<span class="i">$i</span>] &gt; <span class="i">$ary2</span>[<span class="i">$j</span>]<span class="s">)</span> <span class="s">{</span></li><li>                <a class="l_k" href="functions/last.html">last</a><span class="sc">;</span> <span class="c"># can&#39;t go to outer :-(</span></li><li>            <span class="s">}</span></li><li>            <span class="i">$ary1</span>[<span class="i">$i</span>] += <span class="i">$ary2</span>[<span class="i">$j</span>]<span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="c"># this is where that last takes me</span></li><li>    <span class="s">}</span></li></ol></pre><p>Whereas here's how a Perl programmer more comfortable with the idiom might
do it:</p>
<pre class="verbatim"><ol><li>    <span class="j">OUTER:</span> <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$wid</span> <span class="s">(</span><span class="i">@ary1</span><span class="s">)</span> <span class="s">{</span></li><li>    <span class="j">INNER:</span>   <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$jet</span> <span class="s">(</span><span class="i">@ary2</span><span class="s">)</span> <span class="s">{</span></li><li>                <a class="l_k" href="functions/next.html">next</a> <span class="j">OUTER</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$wid</span> &gt; <span class="i">$jet</span><span class="sc">;</span></li><li>                <span class="i">$wid</span> += <span class="i">$jet</span><span class="sc">;</span></li><li>             <span class="s">}</span></li><li>          <span class="s">}</span></li></ol></pre><p>See how much easier this is?  It's cleaner, safer, and faster.  It's
cleaner because it's less noisy.  It's safer because if code gets added
between the inner and outer loops later on, the new code won't be
accidentally executed.  The <code class="inline"><a class="l_k" href="functions/next.html">next</a></code> explicitly iterates the other loop
rather than merely terminating the inner one.  And it's faster because
Perl executes a <code class="inline">foreach</code>
 statement more rapidly than it would the
equivalent <code class="inline">for</code>
 loop.</p>
<p>Perceptive Perl hackers may have noticed that a <code class="inline">for</code>
 loop has a return
value, and that this value can be captured by wrapping the loop in a <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>
block.  The reward for this discovery is this cautionary advice:  The
return value of a <code class="inline">for</code>
 loop is unspecified and may change without notice.
Do not rely on it.</p>
<a name="Basic-BLOCKs"></a><h2>Basic BLOCKs
</h2>
<p>A BLOCK by itself (labeled or not) is semantically equivalent to a
loop that executes once.  Thus you can use any of the loop control
statements in it to leave or restart the block.  (Note that this is
<i>NOT</i> true in <code class="inline"><a class="l_k" href="functions/eval.html">eval{}</a></code>, <code class="inline"><a class="l_k" href="functions/sub.html">sub{}</a></code>, or contrary to popular belief
<code class="inline"><a class="l_k" href="functions/do.html">do{}</a></code> blocks, which do <i>NOT</i> count as loops.)  The <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code>
block is optional.</p>
<p>The BLOCK construct can be used to emulate case structures.</p>
<pre class="verbatim"><ol><li>    <span class="j">SWITCH:</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^abc/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$abc</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^def/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$def</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^xyz/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$xyz</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <span class="i">$nothing</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You'll also find that <code class="inline">foreach</code>
 loop used to create a topicalizer
and a switch:</p>
<pre class="verbatim"><ol><li>    <span class="j">SWITCH:</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$var</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^abc/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$abc</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^def/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$def</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/^xyz/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$xyz</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span> <span class="s">}</span></li><li>        <span class="i">$nothing</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Such constructs are quite frequently used, both because older versions of
Perl had no official <code class="inline">switch</code>
 statement, and also because the new version
described immediately below remains experimental and can sometimes be confusing.</p>
<a name="Switch-Statements"></a><h2>Switch Statements</h2>
<p>    </p>
<p>Starting from Perl 5.10.1 (well, 5.10.0, but it didn't work
right), you can say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">&quot;switch&quot;</span><span class="sc">;</span></li></ol></pre><p>to enable an experimental switch feature.  This is loosely based on an
old version of a Perl 6 proposal, but it no longer resembles the Perl 6
construct.   You also get the switch feature whenever you declare that your
code prefers to run under a version of Perl that is 5.10 or later.  For
example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span></li></ol></pre><p>Under the "switch" feature, Perl gains the experimental keywords
<code class="inline">given</code>
, <code class="inline">when</code>
, <code class="inline"><span class="w">default</span></code>
, <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code>, and <code class="inline"><span class="w">break</span></code>
.
Starting from Perl 5.16, one can prefix the switch
keywords with <code class="inline"><span class="w">CORE::</span></code>
 to access the feature without a <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span></code>

statement.  The keywords <code class="inline">given</code>
 and
<code class="inline">when</code>
 are analogous to <code class="inline">switch</code>
 and
<code class="inline">case</code>
 in other languages -- though <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> is not -- so the code
in the previous section could be rewritten as</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.10.1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$var</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^abc/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$abc</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^def/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$def</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^xyz/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$xyz</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <span class="i">default</span>       <span class="s">{</span> <span class="i">$nothing</span> = <span class="n">1</span> <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>The <code class="inline">foreach</code>
 is the non-experimental way to set a topicalizer.
If you wish to use the highly experimental <code class="inline">given</code>
, that could be
written like this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.10.1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span><span class="i">$var</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^abc/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$abc</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^def/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$def</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/^xyz/</span><span class="s">)</span> <span class="s">{</span> <span class="i">$xyz</span> = <span class="n">1</span> <span class="s">}</span></li><li>        <span class="i">default</span>       <span class="s">{</span> <span class="i">$nothing</span> = <span class="n">1</span> <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>As of 5.14, that can also be written this way:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$var</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$abc</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^abc/</span><span class="sc">;</span></li><li>        <span class="i">$def</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^def/</span><span class="sc">;</span></li><li>        <span class="i">$xyz</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^xyz/</span><span class="sc">;</span></li><li>        <span class="i">default</span> <span class="s">{</span> <span class="i">$nothing</span> = <span class="n">1</span> <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Or if you don't care to play it safe, like this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span><span class="i">$var</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$abc</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^abc/</span><span class="sc">;</span></li><li>        <span class="i">$def</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^def/</span><span class="sc">;</span></li><li>        <span class="i">$xyz</span> = <span class="n">1</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">/^xyz/</span><span class="sc">;</span></li><li>        <span class="i">default</span> <span class="s">{</span> <span class="i">$nothing</span> = <span class="n">1</span> <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>The arguments to <code class="inline">given</code>
 and <code class="inline">when</code>
 are in scalar context,
and <code class="inline">given</code>
 assigns the <code class="inline"><span class="i">$_</span></code>
 variable its topic value.</p>
<p>Exactly what the <i>EXPR</i> argument to <code class="inline">when</code>
 does is hard to describe
precisely, but in general, it tries to guess what you want done.  Sometimes
it is interpreted as <code class="inline">$_ ~~ <i>EXPR</i></code>, and sometimes it is not.  It
also behaves differently when lexically enclosed by a <code class="inline">given</code>
 block than
it does when dynamically enclosed by a <code class="inline">foreach</code>
 loop.  The rules are far
too difficult to understand to be described here.  See <a href="#Experimental-Details-on-given-and-when">Experimental Details on given and when</a> later on.</p>
<p>Due to an unfortunate bug in how <code class="inline">given</code>
 was implemented between Perl 5.10
and 5.16, under those implementations the version of <code class="inline"><span class="i">$_</span></code>
 governed by
<code class="inline">given</code>
 is merely a lexically scoped copy of the original, not a
dynamically scoped alias to the original, as it would be if it were a
<code class="inline">foreach</code>
 or under both the original and the current Perl 6 language
specification.  This bug was fixed in Perl 5.18 (and lexicalized <code class="inline"><span class="i">$_</span></code>
 itself
was removed in Perl 5.24).</p>
<p>If your code still needs to run on older versions,
stick to <code class="inline">foreach</code>
 for your topicalizer and
you will be less unhappy.</p>
<a name="Goto"></a><h2>Goto
</h2>
<p>Although not for the faint of heart, Perl does support a <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>
statement.  There are three forms: <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-LABEL, <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-EXPR, and
<code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-&amp;NAME.  A loop's LABEL is not actually a valid target for
a <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>; it's just the name of the loop.</p>
<p>The <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-LABEL form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a <code class="inline">foreach</code>
 loop.  It
also can't be used to go into a construct that is optimized away.  It
can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it's usually better to use some other
construct such as <code class="inline"><a class="l_k" href="functions/last.html">last</a></code> or <code class="inline"><a class="l_k" href="functions/die.html">die</a></code>.  The author of Perl has never felt the
need to use this form of <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code> (in Perl, that is--C is another matter).</p>
<p>The <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-EXPR form expects a label name, whose scope will be resolved
dynamically.  This allows for computed <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>s per FORTRAN, but isn't
necessarily recommended if you're optimizing for maintainability:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/goto.html">goto</a><span class="s">(</span><span class="s">(</span><span class="q">&quot;FOO&quot;</span><span class="cm">,</span> <span class="q">&quot;BAR&quot;</span><span class="cm">,</span> <span class="q">&quot;GLARCH&quot;</span><span class="s">)</span>[<span class="i">$i</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>-&amp;NAME form is highly magical, and substitutes a call to the
named subroutine for the currently running subroutine.  This is used by
<code class="inline">AUTOLOAD<span class="s">(</span><span class="s">)</span></code>
 subroutines that wish to load another subroutine and then
pretend that the other subroutine had been called in the first place
(except that any modifications to <code class="inline"><span class="i">@_</span></code>
 in the current subroutine are
propagated to the other subroutine.)  After the <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>, not even <code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code>
will be able to tell that this routine was called first.</p>
<p>In almost all cases like this, it's usually a far, far better idea to use the
structured control flow mechanisms of <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>, <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>, or <code class="inline"><a class="l_k" href="functions/redo.html">redo</a></code> instead of
resorting to a <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code>.  For certain applications, the catch and throw pair of
<code class="inline"><a class="l_k" href="functions/eval.html">eval{}</a></code> and die() for exception processing can also be a prudent approach.</p>
<a name="The-Ellipsis-Statement"></a><h2>The Ellipsis Statement










</h2>
<p>Beginning in Perl 5.12, Perl accepts an ellipsis, "<code class="inline">...</code>
", as a
placeholder for code that you haven't implemented yet.  This form of
ellipsis, the unimplemented statement, should not be confused with the
binary flip-flop <code class="inline">...</code>
 operator.  One is a statement and the other an
operator.  (Perl doesn't usually confuse them because usually Perl can tell
whether it wants an operator or a statement, but see below for exceptions.)</p>
<p>When Perl 5.12 or later encounters an ellipsis statement, it parses this
without error, but if and when you should actually try to execute it, Perl
throws an exception with the text <code class="inline"><span class="w">Unimplemented</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span><span class="sc">;</span></li><li><a name="unimplemented"></a>    sub <span class="m">unimplemented</span> <span class="s">{</span> ... <span class="s">}</span></li><li>    <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <span class="i">unimplemented</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$@</span> =~ <span class="q">/^Unimplemented at /</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;I found an ellipsis!&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>You can only use the elliptical statement to stand in for a
complete statement.  These examples of how the ellipsis works:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span><span class="sc">;</span></li><li>    <span class="s">{</span> ... <span class="s">}</span></li><li><a name="foo"></a>    sub <span class="m">foo</span> <span class="s">{</span> ... <span class="s">}</span></li><li>    ...<span class="sc">;</span></li><li>    <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> ... <span class="s">}</span><span class="sc">;</span></li><li><a name="somemeth"></a>    sub <span class="m">somemeth</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        ...<span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="i">$x</span> = <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$n</span><span class="sc">;</span></li><li>        ...<span class="sc">;</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;Hurrah!&quot;</span><span class="sc">;</span></li><li>        <span class="i">$n</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>The elliptical statement cannot stand in for an expression that
is part of a larger statement, since the <code class="inline">...</code>
 is also the three-dot
version of the flip-flop operator (see <a href="perlop.html#Range-Operators">Range Operators in perlop</a>).</p>
<p>These examples of attempts to use an ellipsis are syntax errors:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> ...<span class="sc">;</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;/dev/passwd&quot;</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> ...<span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$condition</span> &amp;&amp; ... <span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;Howdy&quot;</span> <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>There are some cases where Perl can't immediately tell the difference
between an expression and a statement.  For instance, the syntax for a
block and an anonymous hash reference constructor look the same unless
there's something in the braces to give Perl a hint.  The ellipsis is a
syntax error if Perl doesn't guess that the <code class="inline"><span class="s">{</span> ... <span class="s">}</span></code>
 is a block.  In that
case, it doesn't think the <code class="inline">...</code>
 is an ellipsis because it's expecting an
expression instead of a statement:</p>
<pre class="verbatim"><ol><li>    <span class="i">@transformed</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> ... <span class="s">}</span> <span class="i">@input</span><span class="sc">;</span>    <span class="c"># syntax error</span></li></ol></pre><p>Inside your block, you can use a <code class="inline"><span class="sc">;</span></code>
 before the ellipsis to denote that the
<code class="inline"><span class="s">{</span> ... <span class="s">}</span></code>
 is a block and not a hash reference constructor.  Now the ellipsis
works:</p>
<pre class="verbatim"><ol><li>    <span class="i">@transformed</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span><span class="sc">;</span> ... <span class="s">}</span> <span class="i">@input</span><span class="sc">;</span>   <span class="c"># &#39;;&#39; disambiguates</span></li></ol></pre><p>Note: Some folks colloquially refer to this bit of punctuation as a
"yada-yada" or "triple-dot", but its true name
is actually an ellipsis.</p>
<a name="PODs%3a-Embedded-Documentation"></a><h2>PODs: Embedded Documentation
 </h2>
<p>Perl has a mechanism for intermixing documentation with source code.
While it's expecting the beginning of a new statement, if the compiler
encounters a line that begins with an equal sign and a word, like this</p>
<pre class="verbatim"><ol><li>    =<span class="w">head1</span> <span class="w">Here</span> <span class="w">There</span> <span class="w">Be</span> <span class="w">Pods</span>!</li></ol></pre><p>Then that text and all remaining text up through and including a line
beginning with <code class="inline"><span class="pd">=cut</span></code>
 will be ignored.  The format of the intervening
text is described in <a href="perlpod.html">perlpod</a>.</p>
<p>This allows you to intermix your source code
and your documentation text freely, as in</p>
<pre class="verbatim"><ol><li>    =<span class="w">item</span> <span class="i">snazzle</span><span class="s">(</span><span class="i">$)</span></li><li></li><li>    <span class="w">The</span> <span class="i">snazzle</span><span class="s">(</span><span class="s">)</span> <span class="w">function</span> <span class="w">will</span> <span class="w">behave</span> <span class="w">in</span> <span class="w">the</span> <span class="w">most</span> <span class="w">spectacular</span></li><li>    <span class="w">form</span> <span class="w">that</span> <span class="w">you</span> <span class="w">can</span> <span class="w">possibly</span> <span class="w">imagine</span><span class="cm">,</span> <a class="l_k" href="functions/not.html">not</a> <span class="w">even</span> <span class="w">excepting</span></li><li>    <span class="w">cybernetic</span> <span class="w">pyrotechnics</span>.</li><li></li><li>    =<span class="w">cut</span> <span class="w">back</span> <span class="w">to</span> <span class="w">the</span> <span class="w">compiler</span><span class="cm">,</span> <span class="w">nuff</span> <span class="w">of</span> <span class="w">this</span> <span class="w">pod</span> <span class="w">stuff</span>!</li><li></li><li><a name="snazzle"></a>    sub <span class="m">snazzle($)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$thingie</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        .........</li><li>    <span class="s">}</span></li></ol></pre><p>Note that pod translators should look at only paragraphs beginning
with a pod directive (it makes parsing easier), whereas the compiler
actually knows to look for pod escapes even in the middle of a
paragraph.  This means that the following secret stuff will be
ignored by both the compiler and the translators.</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span>=<span class="n">3</span><span class="sc">;</span></li><li>    =<span class="w">secret</span> <span class="w">stuff</span></li><li>     <a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;Neither POD nor CODE!?&quot;</span></li><li>    =<span class="w">cut</span> <span class="w">back</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;got $a\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You probably shouldn't rely upon the <code class="inline"><a class="l_k" href="functions/warn.html">warn()</a></code> being podded out forever.
Not all pod translators are well-behaved in this regard, and perhaps
the compiler will become pickier.</p>
<p>One may also use pod directives to quickly comment out a section
of code.</p>
<a name="Plain-Old-Comments-(Not!)"></a><h2>Plain Old Comments (Not!)
    </h2>
<p>Perl can process line directives, much like the C preprocessor.  Using
this, one can control Perl's idea of filenames and line numbers in
error or warning messages (especially for strings that are processed
with <code class="inline"><a class="l_k" href="functions/eval.html">eval()</a></code>).  The syntax for this mechanism is almost the same as for
most C preprocessors: it matches the regular expression</p>
<pre class="verbatim"><ol><li>    <span class="c"># example: &#39;# line 42 &quot;new_filename.plx&quot;&#39;</span></li><li>    <span class="q">/^\#   \s*</span></li><li>      <span class="q">      line \s+ (\d+)   \s*</span></li><li>      <span class="q">      (?:\s(&quot;?)([^&quot;]+)\g2)? \s*</span></li><li>     <span class="q">     $/x</span></li></ol></pre><p>with <code class="inline"><span class="i">$1</span></code>
 being the line number for the next line, and <code class="inline"><span class="i">$3</span></code>
 being
the optional filename (specified with or without quotes).  Note that
no whitespace may precede the <code class="inline"><span class="c">#</span></code>
, unlike modern C preprocessors.</p>
<p>There is a fairly obvious gotcha included with the line directive:
Debuggers and profilers will only show the last source line to appear
at a particular line number in a given file.  Care should be taken not
to cause line number collisions in code you'd like to debug later.</p>
<p>Here are some examples that you should be able to type into your command
shell:</p>
<pre class="verbatim"><ol><li>    <span class="i">% perl</span></li><li>    <span class="c"># line 200 &quot;bzzzt&quot;</span></li><li>    <span class="c"># the &#39;#&#39; on the previous line must be the first char on line</span></li><li>    <a class="l_k" href="functions/die.html">die</a> <span class="q">&#39;foo&#39;</span><span class="sc">;</span></li><li><a name="__END__"></a>    __END__</li><li><span class="q">    foo at bzzzt line 201.</span></li><li></li><li><span class="q">    % perl</span></li><li><span class="q">    # line 200 &quot;bzzzt&quot;</span></li><li><span class="q">    eval qq[\n#line 2001 &quot;&quot;\ndie &#39;foo&#39;]; print $@;</span></li><li><span class="q">    __END__</span></li><li><span class="q">    foo at - line 2001.</span></li><li></li><li><span class="q">    % perl</span></li><li><span class="q">    eval qq[\n#line 200 &quot;foo bar&quot;\ndie &#39;foo&#39;]; print $@;</span></li><li><span class="q">    __END__</span></li><li><span class="q">    foo at foo bar line 200.</span></li><li></li><li><span class="q">    % perl</span></li><li><span class="q">    # line 345 &quot;goop&quot;</span></li><li><span class="q">    eval &quot;\n#line &quot; . __LINE__ . &#39; &quot;&#39; . __FILE__ .&quot;\&quot;\ndie &#39;foo&#39;&quot;;</span></li><li><span class="q">    print $@;</span></li><li><span class="q">    __END__</span></li><li><span class="q">    foo at goop line 345.</span></li></ol></pre><a name="Experimental-Details-on-given-and-when"></a><h2>Experimental Details on given and when</h2>
<p>As previously mentioned, the "switch" feature is considered highly
experimental; it is subject to change with little notice.  In particular,
<code class="inline">when</code>
 has tricky behaviours that are expected to change to become less
tricky in the future.  Do not rely upon its current (mis)implementation.
Before Perl 5.18, <code class="inline">given</code>
 also had tricky behaviours that you should still
beware of if your code must run on older versions of Perl.</p>
<p>Here is a longer example of <code class="inline">given</code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">&quot;:5.10&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span><span class="i">$foo</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo is undefined&#39;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">&quot;foo&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo is the string &quot;foo&quot;&#39;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="s">[</span><span class="n">1</span><span class="cm">,</span><span class="n">3</span><span class="cm">,</span><span class="n">5</span><span class="cm">,</span><span class="n">7</span><span class="cm">,</span><span class="n">9</span><span class="s">]</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo is an odd digit&#39;</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/continue.html">continue</a><span class="sc">;</span> <span class="c"># Fall through</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="i">$_</span> &lt; <span class="n">100</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo is numerically less than 100&#39;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span>\<span class="i">&amp;complicated_check</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;a complicated check for $foo is true&#39;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="i">default</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/die.html">die</a> <span class="q">q(I don&#39;t know what to do with $foo)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Before Perl 5.18, <code class="inline">given<span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
 assigned the value of <i>EXPR</i> to
merely a lexically scoped <i><b>copy</b></i> (!) of <code class="inline"><span class="i">$_</span></code>
, not a dynamically
scoped alias the way <code class="inline">foreach</code>
 does.  That made it similar to</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$_</span> = <span class="w">EXPR</span><span class="sc">;</span> ... <span class="s">}</span></li></ol></pre><p>except that the block was automatically broken out of by a successful
<code class="inline">when</code>
 or an explicit <code class="inline"><span class="w">break</span></code>
.  Because it was only a copy, and because
it was only lexically scoped, not dynamically scoped, you could not do the
things with it that you are used to in a <code class="inline">foreach</code>
 loop.  In particular,
it did not work for arbitrary function calls if those functions might try
to access $_.  Best stick to <code class="inline">foreach</code>
 for that.</p>
<p>Most of the power comes from the implicit smartmatching that can
sometimes apply.  Most of the time, <code class="inline">when<span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
 is treated as an
implicit smartmatch of <code class="inline"><span class="i">$_</span></code>
, that is, <code class="inline"><span class="i">$_</span> ~~ <span class="w">EXPR</span></code>
.  (See
<a href="perlop.html#Smartmatch-Operator">Smartmatch Operator in perlop</a> for more information on smartmatching.)
But when <i>EXPR</i> is one of the 10 exceptional cases (or things like them)
listed below, it is used directly as a boolean.</p>
<ul>
<li><a name="1."></a><b>1.</b>
<p>A user-defined subroutine call or a method invocation.</p>
</li>
<li><a name="2."></a><b>2.</b>
<p>A regular expression match in the form of <code class="inline"><span class="q">/REGEX/</span></code>
, <code class="inline"><span class="i">$foo</span> =~ <span class="q">/REGEX/</span></code>
,
or <code class="inline"><span class="i">$foo</span> =~ <span class="w">EXPR</span></code>
.  Also, a negated regular expression match in
the form <code class="inline">!<span class="q">/REGEX/</span></code>
, <code class="inline"><span class="i">$foo</span> !~ <span class="q">/REGEX/</span></code>
, or <code class="inline"><span class="i">$foo</span> !~ <span class="w">EXPR</span></code>
.</p>
</li>
<li><a name="3."></a><b>3.</b>
<p>A smart match that uses an explicit <code class="inline">~~</code>
 operator, such as <code class="inline"><span class="w">EXPR</span> ~~ <span class="w">EXPR</span></code>
.</p>
<p><b>NOTE:</b> You will often have to use <code class="inline"><span class="i">$c</span> ~~ <span class="i">$_</span></code>
 because the default case
uses <code class="inline"><span class="i">$_</span> ~~ <span class="i">$c</span></code>
 , which is frequentlythe opposite of what you want.</p>
</li>
<li><a name="4."></a><b>4.</b>
<p>A boolean comparison operator such as <code class="inline"><span class="i">$_</span> &lt; <span class="n">10</span></code>
 or <code class="inline"><span class="i">$x</span> eq <span class="q">&quot;abc&quot;</span></code>
.  The
relational operators that this applies to are the six numeric comparisons
(<code class="inline">&lt;</code>
, <code class="inline">&gt;</code>, <code class="inline">&lt;=</code>
, <code class="inline">&gt;=</code>
, <code class="inline">==</code>
, and <code class="inline">!=</code>
), and
the six string comparisons (<code class="inline">lt</code>
, <code class="inline">gt</code>
, <code class="inline">le</code>
, <code class="inline">ge</code>
, <code class="inline">eq</code>
, and <code class="inline">ne</code>
).</p>
</li>
<li><a name="5."></a><b>5.</b>
<p>At least the three builtin functions <code class="inline"><a class="l_k" href="functions/defined.html">defined(...)</a></code>, <code class="inline"><a class="l_k" href="functions/exists.html">exists(...)</a></code>, and
<code class="inline"><a class="l_k" href="functions/eof.html">eof(...)</a></code>.  We might someday add more of these later if we think of them.</p>
</li>
<li><a name="6."></a><b>6.</b>
<p>A negated expression, whether <code class="inline">!<span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
 or <code class="inline"><a class="l_k" href="functions/not.html">not(EXPR)</a></code>, or a logical
exclusive-or, <code class="inline"><span class="s">(</span><span class="w">EXPR1</span><span class="s">)</span> <a class="l_k" href="functions/xor.html">xor</a> <span class="s">(</span><span class="w">EXPR2</span><span class="s">)</span></code>
.  The bitwise versions (<code class="inline">~</code>
 and <code class="inline">^</code>)
are not included.</p>
</li>
<li><a name="7."></a><b>7.</b>
<p>A filetest operator, with exactly 4 exceptions: <code class="inline">-s</code>
, <code class="inline">-M</code>
, <code class="inline">-A</code>
, and
<code class="inline">-C</code>
, as these return numerical values, not boolean ones.  The <code class="inline">-z</code>

filetest operator is not included in the exception list.</p>
</li>
<li><a name="8."></a><b>8.</b>
<p>The <code class="inline">..</code>
 and <code class="inline">...</code>
 flip-flop operators.  Note that the <code class="inline">...</code>
 flip-flop
operator is completely different from the <code class="inline">...</code>
 elliptical statement
just described.</p>
</li>
</ul>
<p>In those 8 cases above, the value of EXPR is used directly as a boolean, so
no smartmatching is done.  You may think of <code class="inline">when</code>
 as a smartsmartmatch.</p>
<p>Furthermore, Perl inspects the operands of logical operators to
decide whether to use smartmatching for each one by applying the
above test to the operands:</p>
<ul>
<li><a name="9."></a><b>9.</b>
<p>If EXPR is <code class="inline"><span class="w">EXPR1</span> &amp;&amp; <span class="w">EXPR2</span></code>
 or <code class="inline"><span class="w">EXPR1</span> <a class="l_k" href="functions/and.html">and</a> <span class="w">EXPR2</span></code>
, the test is applied
<i>recursively</i> to both EXPR1 and EXPR2.
Only if <i>both</i> operands also pass the
test, <i>recursively</i>, will the expression be treated as boolean.  Otherwise,
smartmatching is used.</p>
</li>
<li><a name="10."></a><b>10.</b>
<p>If EXPR is <code class="inline"><span class="w">EXPR1</span> || <span class="w">EXPR2</span></code>
, <code class="inline"><span class="w">EXPR1</span> <span class="q">//</span> <span class="w">EXPR2</span></code>
, or <code class="inline"><span class="w">EXPR1</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">EXPR2</span></code>
, the
test is applied <i>recursively</i> to EXPR1 only (which might itself be a
higher-precedence AND operator, for example, and thus subject to the
previous rule), not to EXPR2.  If EXPR1 is to use smartmatching, then EXPR2
also does so, no matter what EXPR2 contains.  But if EXPR2 does not get to
use smartmatching, then the second argument will not be either.  This is
quite different from the <code class="inline">&amp;&amp;</code> case just described, so be careful.</p>
</li>
</ul>
<p>These rules are complicated, but the goal is for them to do what you want
(even if you don't quite understand why they are doing it).  For example:</p>
<pre class="verbatim"><ol><li>    when <span class="s">(</span><span class="q">/^\d+$/</span> &amp;&amp; <span class="i">$_</span> &lt; <span class="n">75</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>will be treated as a boolean match because the rules say both
a regex match and an explicit test on <code class="inline"><span class="i">$_</span></code>
 will be treated
as boolean.</p>
<p>Also:</p>
<pre class="verbatim"><ol><li>    when <span class="s">(</span><span class="s">[</span><span class="q">qw(foo bar)</span><span class="s">]</span> &amp;&amp; <span class="q">/baz/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>will use smartmatching because only <i>one</i> of the operands is a boolean:
the other uses smartmatching, and that wins.</p>
<p>Further:</p>
<pre class="verbatim"><ol><li>    when <span class="s">(</span><span class="s">[</span><span class="q">qw(foo bar)</span><span class="s">]</span> || <span class="q">/^baz/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>will use smart matching (only the first operand is considered), whereas</p>
<pre class="verbatim"><ol><li>    when <span class="s">(</span><span class="q">/^baz/</span> || <span class="s">[</span><span class="q">qw(foo bar)</span><span class="s">]</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>will test only the regex, which causes both operands to be
treated as boolean.  Watch out for this one, then, because an
arrayref is always a true value, which makes it effectively
redundant.  Not a good idea.</p>
<p>Tautologous boolean operators are still going to be optimized
away.  Don't be tempted to write</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">&quot;foo&quot;</span> <a class="l_k" href="functions/or.html">or</a> <span class="q">&quot;bar&quot;</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>This will optimize down to <code class="inline"><span class="q">&quot;foo&quot;</span></code>
, so <code class="inline"><span class="q">&quot;bar&quot;</span></code>
 will never be considered (even
though the rules say to use a smartmatch
on <code class="inline"><span class="q">&quot;foo&quot;</span></code>
).  For an alternation like
this, an array ref will work, because this will instigate smartmatching:</p>
<pre class="verbatim"><ol><li>    when <span class="s">(</span><span class="s">[</span><span class="q">qw(foo bar)</span><span class="s">]</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>This is somewhat equivalent to the C-style switch statement's fallthrough
functionality (not to be confused with <i>Perl's</i> fallthrough
functionality--see below), wherein the same block is used for several
<code class="inline">case</code>
 statements.</p>
<p>Another useful shortcut is that, if you use a literal array or hash as the
argument to <code class="inline">given</code>
, it is turned into a reference.  So <code class="inline">given<span class="s">(</span><span class="i">@foo</span><span class="s">)</span></code>
 is
the same as <code class="inline">given<span class="s">(</span>\<span class="i">@foo</span><span class="s">)</span></code>
, for example.</p>
<p><code class="inline"><span class="w">default</span></code>
 behaves exactly like <code class="inline">when<span class="s">(</span><span class="n">1</span> == <span class="n">1</span><span class="s">)</span></code>
, which is
to say that it always matches.</p>
<a name="Breaking-out"></a><h3>Breaking out</h3>
<p>You can use the <code class="inline"><span class="w">break</span></code>
 keyword to break out of the enclosing
<code class="inline">given</code>
 block.  Every <code class="inline">when</code>
 block is implicitly ended with
a <code class="inline"><span class="w">break</span></code>
.</p>
<a name="Fall-through"></a><h3>Fall-through</h3>
<p>You can use the <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> keyword to fall through from one
case to the next immediate <code class="inline">when</code>
 or <code class="inline"><span class="w">default</span></code>
:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/given.html">given</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/x/</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo contains an x&#39;</span><span class="sc">;</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">}</span></li><li>        <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="q">/y/</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo contains a y&#39;</span>            <span class="s">}</span></li><li>        <span class="i">default</span>    <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&#39;$foo does not contain a y&#39;</span>    <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><a name="Return-value"></a><h3>Return value</h3>
<p>When a <code class="inline">given</code>
 statement is also a valid expression (for example,
when it's the last statement of a block), it evaluates to:</p>
<ul>
<li>
<p>An empty list as soon as an explicit <code class="inline"><span class="w">break</span></code>
 is encountered.</p>
</li>
<li>
<p>The value of the last evaluated expression of the successful
<code class="inline">when</code>
/<code class="inline"><span class="w">default</span></code>
 clause, if there happens to be one.</p>
</li>
<li>
<p>The value of the last evaluated expression of the <code class="inline">given</code>
 block if no
condition is true.</p>
</li>
</ul>
<p>In both last cases, the last expression is evaluated in the context that
was applied to the <code class="inline">given</code>
 block.</p>
<p>Note that, unlike <code class="inline">if</code>
 and <code class="inline">unless</code>
, failed <code class="inline">when</code>
 statements always
evaluate to an empty list.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$price</span> = <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>        <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span><span class="i">$item</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/when.html">when</a> <span class="s">(</span><span class="s">[</span><span class="q">&quot;pear&quot;</span><span class="cm">,</span> <span class="q">&quot;apple&quot;</span><span class="s">]</span><span class="s">)</span> <span class="s">{</span> <span class="n">1</span> <span class="s">}</span></li><li>            <a class="l_k" href="functions/break.html">break</a> <a class="l_k" href="functions/when.html">when</a> <span class="q">&quot;vote&quot;</span><span class="sc">;</span>      <span class="c"># My vote cannot be bought</span></li><li>            <span class="n">1e10</span>  <a class="l_k" href="functions/when.html">when</a> <span class="q">/Mona Lisa/</span><span class="sc">;</span></li><li>            <span class="q">&quot;unknown&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>Currently, <code class="inline">given</code>
 blocks can't always
be used as proper expressions.  This
may be addressed in a future version of Perl.</p>
<a name="Switching-in-a-loop"></a><h3>Switching in a loop</h3>
<p>Instead of using <code class="inline">given<span class="s">(</span><span class="s">)</span></code>
, you can use a <code class="inline">foreach<span class="s">(</span><span class="s">)</span></code>
 loop.
For example, here's one way to count how many times a particular
string occurs in an array:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.10.1</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</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/when.html">when</a> <span class="s">(</span><span class="q">&quot;foo&quot;</span><span class="s">)</span> <span class="s">{</span> ++<span class="i">$count</span> <span class="s">}</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\@array contains $count copies of &#39;foo&#39;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Or in a more recent version:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>        ++<span class="i">$count</span> <a class="l_k" href="functions/when.html">when</a> <span class="q">&quot;foo&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\@array contains $count copies of &#39;foo&#39;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>At the end of all <code class="inline">when</code>
 blocks, there is an implicit <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>.
You can override that with an explicit <code class="inline"><a class="l_k" href="functions/last.html">last</a></code> if you're
interested in only the first match alone.</p>
<p>This doesn't work if you explicitly specify a loop variable, as
in <code class="inline">for <span class="i">$item</span> <span class="s">(</span><span class="i">@array</span><span class="s">)</span></code>
.  You have to use the default variable <code class="inline"><span class="i">$_</span></code>
.</p>
<a name="Differences-from-Perl-6"></a><h3>Differences from Perl 6</h3>
<p>The Perl 5 smartmatch and <code class="inline">given</code>
/<code class="inline">when</code>
 constructs are not compatible
with their Perl 6 analogues.  The most visible difference and least
important difference is that, in Perl 5, parentheses are required around
the argument to <code class="inline">given<span class="s">(</span><span class="s">)</span></code>
 and <code class="inline">when<span class="s">(</span><span class="s">)</span></code>
 (except when this last one is used
as a statement modifier).  Parentheses in Perl 6 are always optional in a
control construct such as <code class="inline">if<span class="s">(</span><span class="s">)</span></code>
, <code class="inline">while<span class="s">(</span><span class="s">)</span></code>
, or <code class="inline">when<span class="s">(</span><span class="s">)</span></code>
; they can't be
made optional in Perl 5 without a great deal of potential confusion,
because Perl 5 would parse the expression</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/given.html">given</a> <span class="i">$foo</span> {</li><li>        ...</li><li>    }</li></ol></pre><p>as though the argument to <code class="inline">given</code>
 were an element of the hash
<code class="inline"><span class="i">%foo</span></code>
, interpreting the braces as hash-element syntax.</p>
<p>However, their are many, many other differences.  For example,
this works in Perl 5:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@primary</span> = <span class="s">(</span><span class="q">&quot;red&quot;</span><span class="cm">,</span> <span class="q">&quot;blue&quot;</span><span class="cm">,</span> <span class="q">&quot;green&quot;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">@primary</span> ~~ <span class="q">&quot;red&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;primary smartmatches red&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">&quot;red&quot;</span> ~~ <span class="i">@primary</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;red smartmatches primary&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;that&#39;s all, folks!&quot;</span><span class="sc">;</span></li></ol></pre><p>But it doesn't work at all in Perl 6.  Instead, you should
use the (parallelizable) <code class="inline"><span class="w">any</span></code>
 operator:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/if.html">if</a> <span class="i">any</span><span class="s">(</span><span class="i">@primary</span><span class="s">)</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&quot;red&quot;</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;primary smartmatches red&quot;</span><span class="sc">;</span></li><li>   <span class="s">}</span></li><li></li><li>   <a class="l_k" href="functions/if.html">if</a> <span class="q">&quot;red&quot;</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="i">any</span><span class="s">(</span><span class="i">@primary</span><span class="s">)</span> <span class="s">{</span></li><li>       <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;red smartmatches primary&quot;</span><span class="sc">;</span></li><li>   <span class="s">}</span></li></ol></pre><p>The table of smartmatches in <a href="perlop.html#Smartmatch-Operator">Smartmatch Operator in perlop</a> is not
identical to that proposed by the Perl 6 specification, mainly due to
differences between Perl 6's and Perl 5's data models, but also because
the Perl 6 spec has changed since Perl 5 rushed into early adoption.</p>
<p>In Perl 6, <code class="inline">when<span class="s">(</span><span class="s">)</span></code>
 will always do an implicit smartmatch with its
argument, while in Perl 5 it is convenient (albeit potentially confusing) to
suppress this implicit smartmatch in various rather loosely-defined
situations, as roughly outlined above.  (The difference is largely because
Perl 5 does not have, even internally, a boolean type.)</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="#Declarations">Declarations
   </a><li><a href="#Comments">Comments
 </a><li><a href="#Simple-Statements">Simple Statements
   </a><li><a href="#Truth-and-Falsehood">Truth and Falsehood
       </a><li><a href="#Statement-Modifiers">Statement Modifiers
    
   </a><li><a href="#Compound-Statements">Compound Statements
    
         </a><li><a href="#Loop-Control">Loop Control
     </a><li><a href="#For-Loops">For Loops
 </a><li><a href="#Foreach-Loops">Foreach Loops
 </a><li><a href="#Basic-BLOCKs">Basic BLOCKs
</a><li><a href="#Switch-Statements">Switch Statements</a><li><a href="#Goto">Goto
</a><li><a href="#The-Ellipsis-Statement">The Ellipsis Statement










</a><li><a href="#PODs%3a-Embedded-Documentation">PODs: Embedded Documentation
 </a><li><a href="#Plain-Old-Comments-(Not!)">Plain Old Comments (Not!)
    </a><li><a href="#Experimental-Details-on-given-and-when">Experimental Details on given and when</a></ul></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    = 'perlsyn';
  perldoc.pageAddress = 'perlsyn.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>