/
perlintro.html
804 lines (678 loc) · 76.9 KB
/
perlintro.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="perlintro - perldoc.perl.org">
<link rel="icon" href="/public/img/favicon.ico">
<title>perlintro - perldoc.perl.org</title>
<link href="/public/css/main.min.css" rel="stylesheet">
<link rel="canonical" href="/perlintro.html">
<link href='https://fonts.googleapis.com/css?family=Lato:400,100,300,700,900' rel='stylesheet' type='text/css'>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-1892152-2', 'auto'); // JJ's account
ga('create', 'UA-50555-3', 'auto', 'perlOrg'); // perl.org account
ga('require', 'outboundLinkTracker', {
events: ['click', 'auxclick', 'contextmenu'],
});
ga('require', 'maxScrollTracker');
ga('send', 'pageview');
ga('perlOrg.send', 'pageview');
</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
<script async src="/public/js/tracking.min.js"></script>
</head>
<body class="body container-fluid ">
<div class="wrapper">
<nav class="navbar navbar-expand-md navbar-dark bg-primary bg-perl fixed-top justify-content-between">
<a class="navbar-brand" href="/">
<object class="logo" data="/public/img/logo_perl_doc.svg" type="image/svg+xml" name="Perl Documentation Logo">Perl Documentation Logo</object>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse justify-content-end">
<ul class="nav navbar-nav navbar-right">
<li class="nav-item dropdown">
<a id="navbarDropdown" href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Perl versions</a>
<div class="dropdown-menu" id="dropdown-menu-links" aria-labelledby="navbarDropdown">
<p class='dropdown-item major-version'>
5.28
</p>
<a class='dropdown-item minor-version' href="/5.28.2">5.28.2</a>
<a class='dropdown-item minor-version' href="/5.28.1">5.28.1</a>
<a class='dropdown-item minor-version' href="/5.28.0">5.28.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.26
</p>
<a class='dropdown-item minor-version' href="/5.26.3">5.26.3</a>
<a class='dropdown-item minor-version' href="/5.26.2">5.26.2</a>
<a class='dropdown-item minor-version' href="/5.26.1">5.26.1</a>
<a class='dropdown-item minor-version' href="/5.26.0">5.26.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.24
</p>
<a class='dropdown-item minor-version' href="/5.24.4">5.24.4</a>
<a class='dropdown-item minor-version' href="/5.24.3">5.24.3</a>
<a class='dropdown-item minor-version' href="/5.24.2">5.24.2</a>
<a class='dropdown-item minor-version' href="/5.24.1">5.24.1</a>
<a class='dropdown-item minor-version' href="/5.24.0">5.24.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.22
</p>
<a class='dropdown-item minor-version' href="/5.22.3">5.22.3</a>
<a class='dropdown-item minor-version' href="/5.22.2">5.22.2</a>
<a class='dropdown-item minor-version' href="/5.22.1">5.22.1</a>
<a class='dropdown-item minor-version' href="/5.22.0">5.22.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.20
</p>
<a class='dropdown-item minor-version' href="/5.20.3">5.20.3</a>
<a class='dropdown-item minor-version' href="/5.20.2">5.20.2</a>
<a class='dropdown-item minor-version' href="/5.20.1">5.20.1</a>
<a class='dropdown-item minor-version' href="/5.20.0">5.20.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.18
</p>
<a class='dropdown-item minor-version' href="/5.18.4">5.18.4</a>
<a class='dropdown-item minor-version' href="/5.18.3">5.18.3</a>
<a class='dropdown-item minor-version' href="/5.18.2">5.18.2</a>
<a class='dropdown-item minor-version' href="/5.18.1">5.18.1</a>
<a class='dropdown-item minor-version' href="/5.18.0">5.18.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.16
</p>
<a class='dropdown-item minor-version' href="/5.16.3">5.16.3</a>
<a class='dropdown-item minor-version' href="/5.16.2">5.16.2</a>
<a class='dropdown-item minor-version' href="/5.16.1">5.16.1</a>
<a class='dropdown-item minor-version' href="/5.16.0">5.16.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.14
</p>
<a class='dropdown-item minor-version' href="/5.14.4">5.14.4</a>
<a class='dropdown-item minor-version' href="/5.14.3">5.14.3</a>
<a class='dropdown-item minor-version' href="/5.14.2">5.14.2</a>
<a class='dropdown-item minor-version' href="/5.14.1">5.14.1</a>
<a class='dropdown-item minor-version' href="/5.14.0">5.14.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.12
</p>
<a class='dropdown-item minor-version' href="/5.12.5">5.12.5</a>
<a class='dropdown-item minor-version' href="/5.12.4">5.12.4</a>
<a class='dropdown-item minor-version' href="/5.12.3">5.12.3</a>
<a class='dropdown-item minor-version' href="/5.12.2">5.12.2</a>
<a class='dropdown-item minor-version' href="/5.12.1">5.12.1</a>
<a class='dropdown-item minor-version' href="/5.12.0">5.12.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.10
</p>
<a class='dropdown-item minor-version' href="/5.10.1">5.10.1</a>
<a class='dropdown-item minor-version' href="/5.10.0">5.10.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.8
</p>
<a class='dropdown-item minor-version' href="/5.8.9">5.8.9</a>
<a class='dropdown-item minor-version' href="/5.8.8">5.8.8</a>
<a class='dropdown-item minor-version' href="/5.8.7">5.8.7</a>
<a class='dropdown-item minor-version' href="/5.8.6">5.8.6</a>
<a class='dropdown-item minor-version' href="/5.8.5">5.8.5</a>
<a class='dropdown-item minor-version' href="/5.8.4">5.8.4</a>
<a class='dropdown-item minor-version' href="/5.8.3">5.8.3</a>
<a class='dropdown-item minor-version' href="/5.8.2">5.8.2</a>
<a class='dropdown-item minor-version' href="/5.8.1">5.8.1</a>
<a class='dropdown-item minor-version' href="/5.8.0">5.8.0</a>
<div class="dropdown-divider"></div>
<p class='dropdown-item major-version'>
5.6
</p>
<a class='dropdown-item minor-version' href="/5.6.2">5.6.2</a>
<a class='dropdown-item minor-version' href="/5.6.1">5.6.1</a>
<a class='dropdown-item minor-version' href="/5.6.0">5.6.0</a>
</div>
</li>
<li class="nav-item dropdown">
<a id="navbarDropdown1" href="#" class="dropdown-toggle nav-link" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Manuals</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown1">
<a class="dropdown-item" href="/5.28.2/index-overview.html">Overview</a>
<a class="dropdown-item" href="/5.28.2/index-tutorials.html">Tutorials</a>
<a class="dropdown-item" href="/5.28.2/index-faq.html">FAQs</a>
<a class="dropdown-item" href="/5.28.2/index-history.html">History / Changes</a>
<a class="dropdown-item" href="/5.28.2/index-licence.html">License</a>
<a class="dropdown-item" href="/5.28.2/index-language.html">Language</a>
<a class="dropdown-item" href="/5.28.2/index-functions.html">Functions</a>
<a class="dropdown-item" href="/5.28.2/perlop.html">Operators</a>
<a class="dropdown-item" href="/5.28.2/perlvar.html">Special Variables</a>
<a class="dropdown-item" href="/5.28.2/index-pragmas.html">Pragmas</a>
<a class="dropdown-item" href="/5.28.2/index-utilities.html">Utilities</a>
<a class="dropdown-item" href="/5.28.2/index-internals.html">Internals</a>
<a class="dropdown-item" href="/5.28.2/index-platforms.html">Platform Specific</a>
</div>
</li>
<li class="nav-item dropdown">
<a id="navbarDropdown3" href="#" class="nav-link dropdown-toggle " data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Modules</a>
<div aria-labelledby="navbarDropdown3" class="dropdown-menu letters-wrap">
<a class="dropdown-item" href="/5.28.2/index-functions.html">A-Z</a>
<a class="dropdown-item" href="/5.28.2/index-functions-by-cat.html">By Category</a>
<div class="dropdown-divider"></div>
<div class="letter-container">
<a class="dropdown-item letters" href="/5.28.2/index-modules-A.html">A</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-B.html">B</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-C.html">C</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-D.html">D</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-E.html">E</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-F.html">F</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-G.html">G</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-H.html">H</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-I.html">I</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-J.html">J</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-L.html">L</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-M.html">M</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-N.html">N</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-O.html">O</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-P.html">P</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-S.html">S</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-T.html">T</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-U.html">U</a>
<a class="dropdown-item letters" href="/5.28.2/index-modules-X.html">X</a>
</div>
</div>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</nav>
<main class="row main-content pb-5 pt-5">
<div class="col-sm-8 offset-sm-2">
<div class="row">
<div class="col-sm-12">
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-overview.html">Overview</a> >
perlintro
</div>
</div>
</div>
<div class="row">
<article class="col-sm-12 content">
<div class="documentation-wrapper">
<div id="perl_version">
<h1 class='page-title'> Perl 5 version documentation</h1>
</div>
<h1>perlintro</h1>
<!-- -->
<ul><li><a href="#NAME">NAME</a></li><li><a href="#DESCRIPTION">DESCRIPTION</a></li><ul><li><a href="#What-is-Perl%3f">What is Perl?</a></li><li><a href="#Running-Perl-programs">Running Perl programs</a></li><li><a href="#Safety-net">Safety net</a></li><li><a href="#Basic-syntax-overview">Basic syntax overview</a></li><li><a href="#Perl-variable-types">Perl variable types</a></li><li><a href="#Variable-scoping">Variable scoping</a></li><li><a href="#Conditional-and-looping-constructs">Conditional and looping constructs</a></li><li><a href="#Builtin-operators-and-functions">Builtin operators and functions</a></li><li><a href="#Files-and-I%2fO">Files and I/O</a></li><li><a href="#Regular-expressions">Regular expressions</a></li><li><a href="#Writing-subroutines">Writing subroutines</a></li><li><a href="#OO-Perl">OO Perl</a></li><li><a href="#Using-Perl-modules">Using Perl modules</a></li></ul><li><a href="#AUTHOR">AUTHOR</a></li></ul><a id="NAME"></a><h2 class='h2' >NAME</h2>
<p>perlintro -- a brief introduction and overview of Perl</p>
<a id="DESCRIPTION"></a><h2 class='h2' >DESCRIPTION</h2>
<p>This document is intended to give you a quick overview of the Perl
programming language, along with pointers to further documentation. It
is intended as a "bootstrap" guide for those who are new to the
language, and provides just enough information for you to be able to
read other peoples' Perl and understand roughly what it's doing, or
write your own simple scripts.</p>
<p>This introductory document does not aim to be complete. It does not
even aim to be entirely accurate. In some cases perfection has been
sacrificed in the goal of getting the general idea across. You are
<i>strongly</i> advised to follow this introduction with more information
from the full Perl manual, the table of contents to which can be found
in <a href="https://search.cpan.org/perldoc/perltoc">perltoc</a>.</p>
<p>Throughout this document you'll see references to other parts of the
Perl documentation. You can read that documentation using the <code class="inline"><span class="w">perldoc</span></code>
command or whatever method you're using to read this document.</p>
<p>Throughout Perl's documentation, you'll find numerous examples intended
to help explain the discussed features. Please keep in mind that many
of them are code fragments rather than complete programs.</p>
<p>These examples often reflect the style and preference of the author of
that piece of the documentation, and may be briefer than a corresponding
line of code in a real program. Except where otherwise noted, you
should assume that <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
statements
appear earlier in the "program", and that any variables used have
already been declared, even if those declarations have been omitted
to make the example easier to read.</p>
<p>Do note that the examples have been written by many different authors over
a period of several decades. Styles and techniques will therefore differ,
although some effort has been made to not vary styles too widely in the
same sections. Do not consider one style to be better than others - "There's
More Than One Way To Do It" is one of Perl's mottos. After all, in your
journey as a programmer, you are likely to encounter different styles.</p>
<a id="What-is-Perl%3f"></a><h3 class='h3' >What is Perl?</h3>
<p>Perl is a general-purpose programming language originally developed for
text manipulation and now used for a wide range of tasks including
system administration, web development, network programming, GUI
development, and more.</p>
<p>The language is intended to be practical (easy to use, efficient,
complete) rather than beautiful (tiny, elegant, minimal). Its major
features are that it's easy to use, supports both procedural and
object-oriented (OO) programming, has powerful built-in support for text
processing, and has one of the world's most impressive collections of
third-party modules.</p>
<p>Different definitions of Perl are given in <a href="perl.html">perl</a>, <a href="https://search.cpan.org/perldoc/perlfaq1">perlfaq1</a> and
no doubt other places. From this we can determine that Perl is different
things to different people, but that lots of people think it's at least
worth writing about.</p>
<a id="Running-Perl-programs"></a><h3 class='h3' >Running Perl programs</h3>
<p>To run a Perl program from the Unix command line:</p>
<pre class="verbatim"><ol><li> <span class="w">perl</span> <span class="w">progname</span>.<span class="w">pl</span></li></ol></pre><p>Alternatively, put this as the first line of your script:</p>
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/env perl</span></li></ol></pre><p>... and run the script as <i>/path/to/script.pl</i>. Of course, it'll need
to be executable first, so <code class="inline"><a class="l_k" href="functions/chmod.html">chmod</a> <span class="n">755</span> <span class="w">script</span>.<span class="w">pl</span></code>
(under Unix).</p>
<p>(This start line assumes you have the <strong>env</strong> program. You can also put
directly the path to your perl executable, like in <code class="inline">#!/usr/bin/perl</code>
).</p>
<p>For more information, including instructions for other platforms such as
Windows and Mac OS, read <a href="perlrun.html">perlrun</a>.</p>
<a id="Safety-net"></a><h3 class='h3' >Safety net</h3>
<p>Perl by default is very forgiving. In order to make it more robust
it is recommended to start every program with the following lines:</p>
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>The two additional lines request from perl to catch various common
problems in your code. They check different things so you need both. A
potential problem caught by <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
will cause your code to stop
immediately when it is encountered, while <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></code>
will merely
give a warning (like the command-line switch <strong>-w</strong>) and let your code run.
To read more about them check their respective manual pages at <a href="strict.html">strict</a>
and <a href="warnings.html">warnings</a>.</p>
<a id="Basic-syntax-overview"></a><h3 class='h3' >Basic syntax overview</h3>
<p>A Perl script or program consists of one or more statements. These
statements are simply written in the script in a straightforward
fashion. There is no need to have a <code class="inline"><span class="i">main</span><span class="s">(</span><span class="s">)</span></code>
function or anything of
that kind.</p>
<p>Perl statements end in a semi-colon:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hello, world"</span><span class="sc">;</span></li></ol></pre><p>Comments start with a hash symbol and run to the end of the line</p>
<pre class="verbatim"><ol><li> <span class="c"># This is a comment</span></li></ol></pre><p>Whitespace is irrelevant:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a></li><li> <span class="q">"Hello, world"</span></li><li> <span class="sc">;</span></li></ol></pre><p>... except inside quoted strings:</p>
<pre class="verbatim"><ol><li> <span class="c"># this would print with a linebreak in the middle</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hello</span></li><li> <span class="q"> world"</span><span class="sc">;</span></li></ol></pre><p>Double quotes or single quotes may be used around literal strings:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hello, world"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">'Hello, world'</span><span class="sc">;</span></li></ol></pre><p>However, only double quotes "interpolate" variables and special
characters such as newlines (<code class="inline">\<span class="w">n</span></code>
):</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hello, $name\n"</span><span class="sc">;</span> <span class="c"># works fine</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">'Hello, $name\n'</span><span class="sc">;</span> <span class="c"># prints $name\n literally</span></li></ol></pre><p>Numbers don't need quotes around them:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="n">42</span><span class="sc">;</span></li></ol></pre><p>You can use parentheses for functions' arguments or omit them
according to your personal taste. They are only required
occasionally to clarify issues of precedence.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">"Hello, world\n"</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hello, world\n"</span><span class="sc">;</span></li></ol></pre><p>More detailed information about Perl syntax can be found in <a href="perlsyn.html">perlsyn</a>.</p>
<a id="Perl-variable-types"></a><h3 class='h3' >Perl variable types</h3>
<p>Perl has three main variable types: scalars, arrays, and hashes.</p>
<ul>
<li><a id="Scalars"></a><strong>Scalars</strong>
<p>A scalar represents a single value:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$animal</span> = <span class="q">"camel"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$answer</span> = <span class="n">42</span><span class="sc">;</span></li></ol></pre><p>Scalar values can be strings, integers or floating point numbers, and Perl
will automatically convert between them as required. There is no need
to pre-declare your variable types, but you have to declare them using
the <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> keyword the first time you use them. (This is one of the
requirements of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
.)</p>
<p>Scalar values can be used in various ways:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animal</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"The animal is $animal\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"The square of $answer is "</span><span class="cm">,</span> <span class="i">$answer</span> * <span class="i">$answer</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p>There are a number of "magic" scalars with names that look like
punctuation or line noise. These special variables are used for all
kinds of purposes, and are documented in <a href="perlvar.html">perlvar</a>. The only one you
need to know about for now is <code class="inline"><span class="i">$_</span></code>
which is the "default variable".
It's used as the default argument to a number of functions in Perl, and
it's set implicitly by certain looping constructs.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> <span class="c"># prints contents of $_ by default</span></li></ol></pre></li>
<li><a id="Arrays"></a><strong>Arrays</strong>
<p>An array represents a list of values:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@animals</span> = <span class="s">(</span><span class="q">"camel"</span><span class="cm">,</span> <span class="q">"llama"</span><span class="cm">,</span> <span class="q">"owl"</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@numbers</span> = <span class="s">(</span><span class="n">23</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">69</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@mixed</span> = <span class="s">(</span><span class="q">"camel"</span><span class="cm">,</span> <span class="n">42</span><span class="cm">,</span> <span class="n">1.23</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Arrays are zero-indexed. Here's how you get at elements in an array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animals</span>[<span class="n">0</span>]<span class="sc">;</span> <span class="c"># prints "camel"</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$animals</span>[<span class="n">1</span>]<span class="sc">;</span> <span class="c"># prints "llama"</span></li></ol></pre><p>The special variable <code class="inline"><span class="i">$#array</span></code>
tells you the index of the last element
of an array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$mixed</span>[<span class="i">$#mixed</span>]<span class="sc">;</span> <span class="c"># last element, prints 1.23</span></li></ol></pre><p>You might be tempted to use <code class="inline"><span class="i">$#array</span> + <span class="n">1</span></code>
to tell you how many items there
are in an array. Don't bother. As it happens, using <code class="inline"><span class="i">@array</span></code>
where Perl
expects to find a scalar value ("in scalar context") will give you the number
of elements in the array:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">@animals</span> < <span class="n">5</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>The elements we're getting from the array start with a <code class="inline"><span class="i">$</span></code>
because
we're getting just a single value out of the array; you ask for a scalar,
you get a scalar.</p>
<p>To get multiple values from an array:</p>
<pre class="verbatim"><ol><li> <span class="i">@animals</span>[<span class="n">0</span><span class="cm">,</span><span class="n">1</span>]<span class="sc">;</span> <span class="c"># gives ("camel", "llama");</span></li><li> <span class="i">@animals</span>[<span class="n">0</span>..<span class="n">2</span>]<span class="sc">;</span> <span class="c"># gives ("camel", "llama", "owl");</span></li><li> <span class="i">@animals</span>[<span class="n">1</span>..<span class="i">$#animals</span>]<span class="sc">;</span> <span class="c"># gives all except the first element</span></li></ol></pre><p>This is called an "array slice".</p>
<p>You can do various useful things to lists:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@animals</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@backwards</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <span class="i">@numbers</span><span class="sc">;</span></li></ol></pre><p>There are a couple of special arrays too, such as <code class="inline"><span class="i">@ARGV</span></code>
(the command
line arguments to your script) and <code class="inline"><span class="i">@_</span></code>
(the arguments passed to a
subroutine). These are documented in <a href="perlvar.html">perlvar</a>.</p>
</li>
<li><a id="Hashes"></a><strong>Hashes</strong>
<p>A hash represents a set of key/value pairs:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%fruit_color</span> = <span class="s">(</span><span class="q">"apple"</span><span class="cm">,</span> <span class="q">"red"</span><span class="cm">,</span> <span class="q">"banana"</span><span class="cm">,</span> <span class="q">"yellow"</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can use whitespace and the <code class="inline"><span class="cm">=></span></code>
operator to lay them out more
nicely:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%fruit_color</span> = <span class="s">(</span></li><li> <span class="w">apple</span> <span class="cm">=></span> <span class="q">"red"</span><span class="cm">,</span></li><li> <span class="w">banana</span> <span class="cm">=></span> <span class="q">"yellow"</span><span class="cm">,</span></li><li> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To get at hash elements:</p>
<pre class="verbatim"><ol><li> <span class="i">$fruit_color</span>{<span class="q">"apple"</span>}<span class="sc">;</span> <span class="c"># gives "red"</span></li></ol></pre><p>You can get at lists of keys and values with <code class="inline"><a class="l_k" href="functions/keys.html">keys()</a></code> and
<code class="inline"><a class="l_k" href="functions/values.html">values()</a></code>.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@fruits</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%fruit_colors</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@colors</span> = <a class="l_k" href="functions/values.html">values</a> <span class="i">%fruit_colors</span><span class="sc">;</span></li></ol></pre><p>Hashes have no particular internal order, though you can sort the keys
and loop through them.</p>
<p>Just like special scalars and arrays, there are also special hashes.
The most well known of these is <code class="inline"><span class="i">%ENV</span></code>
which contains environment
variables. Read all about it (and other special variables) in
<a href="perlvar.html">perlvar</a>.</p>
</li>
</ul>
<p>Scalars, arrays and hashes are documented more fully in <a href="perldata.html">perldata</a>.</p>
<p>More complex data types can be constructed using references, which allow
you to build lists and hashes within lists and hashes.</p>
<p>A reference is a scalar value and can refer to any other Perl data
type. So by storing a reference as the value of an array or hash
element, you can easily create lists and hashes within lists and
hashes. The following example shows a 2 level hash of hash
structure using anonymous hash references.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$variables</span> = <span class="s">{</span></li><li> <span class="w">scalar</span> <span class="cm">=></span> <span class="s">{</span></li><li> <span class="w">description</span> <span class="cm">=></span> <span class="q">"single item"</span><span class="cm">,</span></li><li> <span class="w">sigil</span> <span class="cm">=></span> <span class="q">'$'</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="cm">,</span></li><li> <span class="w">array</span> <span class="cm">=></span> <span class="s">{</span></li><li> <span class="w">description</span> <span class="cm">=></span> <span class="q">"ordered list of items"</span><span class="cm">,</span></li><li> <span class="w">sigil</span> <span class="cm">=></span> <span class="q">'@'</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="cm">,</span></li><li> <span class="w">hash</span> <span class="cm">=></span> <span class="s">{</span></li><li> <span class="w">description</span> <span class="cm">=></span> <span class="q">"key/value pairs"</span><span class="cm">,</span></li><li> <span class="w">sigil</span> <span class="cm">=></span> <span class="q">'%'</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"</span><span class="sc">;</span></li></ol></pre><p>Exhaustive information on the topic of references can be found in
<a href="perlreftut.html">perlreftut</a>, <a href="perllol.html">perllol</a>, <a href="perlref.html">perlref</a> and <a href="perldsc.html">perldsc</a>.</p>
<a id="Variable-scoping"></a><h3 class='h3' >Variable scoping</h3>
<p>Throughout the previous section all the examples have used the syntax:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$var</span> = <span class="q">"value"</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> is actually not required; you could just use:</p>
<pre class="verbatim"><ol><li> <span class="i">$var</span> = <span class="q">"value"</span><span class="sc">;</span></li></ol></pre><p>However, the above usage will create global variables throughout your
program, which is bad programming practice. <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> creates lexically
scoped variables instead. The variables are scoped to the block
(i.e. a bunch of statements surrounded by curly-braces) in which they
are defined.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <span class="q">"foo"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$some_condition</span> = <span class="n">1</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$some_condition</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$y</span> = <span class="q">"bar"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="sc">;</span> <span class="c"># prints "foo"</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span><span class="sc">;</span> <span class="c"># prints "bar"</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="sc">;</span> <span class="c"># prints "foo"</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span><span class="sc">;</span> <span class="c"># prints nothing; $y has fallen out of scope</span></li></ol></pre><p>Using <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> in combination with a <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
at the top of
your Perl scripts means that the interpreter will pick up certain common
programming errors. For instance, in the example above, the final
<code class="inline"><a class="l_k" href="functions/print.html">print</a> <span class="i">$y</span></code>
would cause a compile-time error and prevent you from
running the program. Using <code class="inline"><span class="w">strict</span></code>
is highly recommended.</p>
<a id="Conditional-and-looping-constructs"></a><h3 class='h3' >Conditional and looping constructs</h3>
<p>Perl has most of the usual conditional and looping constructs. As of Perl
5.10, it even has a case/switch statement (spelled <code class="inline"><a class="l_k" href="functions/given.html">given</a></code>/<code class="inline"><a class="l_k" href="functions/when.html">when</a></code>). See
<a href="perlsyn.html#Switch-Statements">Switch Statements in perlsyn</a> for more details.</p>
<p>The conditions can be any Perl expression. See the list of operators in
the next section for information on comparison and boolean logic operators,
which are commonly used in conditional statements.</p>
<ul>
<li><a id="if"></a><strong>if</strong>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span> <a class="l_k" href="functions/elsif.html">elsif</a> <span class="s">(</span> <span class="w">other</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span></li></ol></pre><p>There's also a negated version of it:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span></li></ol></pre><p>This is provided as a more readable version of <code class="inline">if (!<i>condition</i>)</code>.</p>
<p>Note that the braces are required in Perl, even if you've only got one
line in the block. However, there is a clever way of making your one-line
conditional blocks more English like:</p>
<pre class="verbatim"><ol><li> <span class="c"># the traditional way</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$zippy</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Yow!"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># the Perlish post-condition way</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Yow!"</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$zippy</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"We have no bananas"</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$bananas</span><span class="sc">;</span></li></ol></pre></li>
<li><a id="while"></a><strong>while</strong>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span></li></ol></pre><p>There's also a negated version, for the same reason we have <code class="inline"><a class="l_k" href="functions/unless.html">unless</a></code>:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/until.html">until</a> <span class="s">(</span> <span class="w">condition</span> <span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span></li></ol></pre><p>You can also use <code class="inline"><a class="l_k" href="functions/while.html">while</a></code> in a post-condition:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"LA LA LA\n"</span> <a class="l_k" href="functions/while.html">while</a> <span class="n">1</span><span class="sc">;</span> <span class="c"># loops forever</span></li></ol></pre></li>
<li><a id="for"></a><strong>for</strong>
<p>Exactly like C:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$i</span> <= <span class="i">$max</span><span class="sc">;</span> <span class="i">$i</span>++<span class="s">)</span> <span class="s">{</span></li><li> ...</li><li> <span class="s">}</span></li></ol></pre><p>The C style for loop is rarely needed in Perl since Perl provides
the more friendly list scanning <code class="inline"><a class="l_k" href="functions/foreach.html">foreach</a></code> loop.</p>
</li>
<li><a id="foreach"></a><strong>foreach</strong>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/foreach.html">foreach</a> <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"This element is $_\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$list</span>[<span class="i">$_</span>] <a class="l_k" href="functions/foreach.html">foreach</a> <span class="n">0</span> .. <span class="i">$max</span><span class="sc">;</span></li><li></li><li> <span class="c"># you don't have to use the default $_ either...</span></li><li> <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"The value of $key is $hash{$key}\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/foreach.html">foreach</a></code> keyword is actually a synonym for the <code class="inline"><a class="l_k" href="functions/for.html">for</a></code>
keyword. See <code class="inline"><a href="perlsyn.html#Foreach-Loops">Foreach Loops in perlsyn</a></code>.</p>
</li>
</ul>
<p>For more detail on looping constructs (and some that weren't mentioned in
this overview) see <a href="perlsyn.html">perlsyn</a>.</p>
<a id="Builtin-operators-and-functions"></a><h3 class='h3' >Builtin operators and functions</h3>
<p>Perl comes with a wide selection of builtin functions. Some of the ones
we've already seen include <code class="inline"><a class="l_k" href="functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="functions/sort.html">sort</a></code> and <code class="inline"><a class="l_k" href="functions/reverse.html">reverse</a></code>. A list of
them is given at the start of <a href="perlfunc.html">perlfunc</a> and you can easily read
about any given function by using <code class="inline">perldoc -f <i>functionname</i></code>.</p>
<p>Perl operators are documented in full in <a href="perlop.html">perlop</a>, but here are a few
of the most common ones:</p>
<ul>
<li><a id="Arithmetic"></a><strong>Arithmetic</strong>
<pre class="verbatim"><ol><li> + <span class="w">addition</span></li><li> - <span class="w">subtraction</span></li><li> * <span class="w">multiplication</span></li><li> / <span class="w">division</span></li></ol></pre></li>
<li><a id="Numeric-comparison"></a><strong>Numeric comparison</strong>
<pre class="verbatim"><ol><li> == <span class="w">equality</span></li><li> != <span class="w">inequality</span></li><li> < <span class="w">less</span> <span class="w">than</span></li><li> > <span class="w">greater</span> <span class="w">than</span></li><li> <= <span class="w">less</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li><li> >= <span class="w">greater</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li></ol></pre></li>
<li><a id="String-comparison"></a><strong>String comparison</strong>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/eq.html">eq</a> <span class="w">equality</span></li><li> <a class="l_k" href="functions/ne.html">ne</a> <span class="w">inequality</span></li><li> <a class="l_k" href="functions/lt.html">lt</a> <span class="w">less</span> <span class="w">than</span></li><li> <a class="l_k" href="functions/gt.html">gt</a> <span class="w">greater</span> <span class="w">than</span></li><li> <a class="l_k" href="functions/le.html">le</a> <span class="w">less</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li><li> <a class="l_k" href="functions/ge.html">ge</a> <span class="w">greater</span> <span class="w">than</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">equal</span></li></ol></pre><p>(Why do we have separate numeric and string comparisons? Because we don't
have special variable types, and Perl needs to know whether to sort
numerically (where 99 is less than 100) or alphabetically (where 100 comes
before 99).</p>
</li>
<li><a id="Boolean-logic"></a><strong>Boolean logic</strong>
<pre class="verbatim"><ol><li> && <a class="l_k" href="functions/and.html">and</a></li><li> || <a class="l_k" href="functions/or.html">or</a></li><li> ! <a class="l_k" href="functions/not.html">not</a></li></ol></pre><p>(<code class="inline"><a class="l_k" href="functions/and.html">and</a></code>, <code class="inline"><a class="l_k" href="functions/or.html">or</a></code> and <code class="inline"><a class="l_k" href="functions/not.html">not</a></code> aren't just in the above table as descriptions
of the operators. They're also supported as operators in their own
right. They're more readable than the C-style operators, but have
different precedence to <code class="inline">&&</code>
and friends. Check <a href="perlop.html">perlop</a> for more
detail.)</p>
</li>
<li><a id="Miscellaneous"></a><strong>Miscellaneous</strong>
<pre class="verbatim"><ol><li> = <span class="w">assignment</span></li><li> . <span class="w">string</span> <span class="w">concatenation</span></li><li> <span class="w">x</span> <span class="w">string</span> <span class="w">multiplication</span></li><li> .. <span class="w">range</span> <span class="w">operator</span> <span class="s">(</span><span class="w">creates</span> <span class="w">a</span> <span class="w">list</span> <span class="w">of</span> <span class="w">numbers</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">strings</span><span class="s">)</span></li></ol></pre></li>
</ul>
<p>Many operators can be combined with a <code class="inline">=</code>
as follows:</p>
<pre class="verbatim"><ol><li> <span class="i">$a</span> += <span class="n">1</span><span class="sc">;</span> <span class="c"># same as $a = $a + 1</span></li><li> <span class="i">$a</span> -= <span class="n">1</span><span class="sc">;</span> <span class="c"># same as $a = $a - 1</span></li><li> <span class="i">$a</span> .= <span class="q">"\n"</span><span class="sc">;</span> <span class="c"># same as $a = $a . "\n";</span></li></ol></pre><a id="Files-and-I%2fO"></a><h3 class='h3' >Files and I/O</h3>
<p>You can open a file for input or output using the <code class="inline"><a class="l_k" href="functions/open.html">open()</a></code> function.
It's documented in extravagant detail in <a href="perlfunc.html">perlfunc</a> and <a href="perlopentut.html">perlopentut</a>,
but in short:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$in</span><span class="cm">,</span> <span class="q">"<"</span><span class="cm">,</span> <span class="q">"input.txt"</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't open input.txt: $!"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$out</span><span class="cm">,</span> <span class="q">">"</span><span class="cm">,</span> <span class="q">"output.txt"</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't open output.txt: $!"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$log</span><span class="cm">,</span> <span class="q">">>"</span><span class="cm">,</span> <span class="q">"my.log"</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't open my.log: $!"</span><span class="sc">;</span></li></ol></pre><p>You can read from an open filehandle using the <code class="inline"><></code>
operator. In
scalar context it reads a single line from the filehandle, and in list
context it reads the whole file in, assigning each line to an element of
the list:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> = <span class="q"><$in></span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@lines</span> = <span class="q"><$in></span><span class="sc">;</span></li></ol></pre><p>Reading in the whole file at one time is called slurping. It can
be useful but it may be a memory hog. Most text file processing
can be done a line at a time with Perl's looping constructs.</p>
<p>The <code class="inline"><></code>
operator is most often seen in a <code class="inline"><a class="l_k" href="functions/while.html">while</a></code> loop:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q"><$in></span><span class="s">)</span> <span class="s">{</span> <span class="c"># assigns each line in turn to $_</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Just read in this line: $_"</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>We've already seen how to print to standard output using <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code>.
However, <code class="inline"><a class="l_k" href="functions/print.html">print()</a></code> can also take an optional first argument specifying
which filehandle to print to:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">"This is your final warning.\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$out</span> <span class="i">$record</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$log</span> <span class="i">$logmessage</span><span class="sc">;</span></li></ol></pre><p>When you're done with your filehandles, you should <code class="inline"><a class="l_k" href="functions/close.html">close()</a></code> them
(though to be honest, Perl will clean up after you if you forget):</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/close.html">close</a> <span class="i">$in</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">"$in: $!"</span><span class="sc">;</span></li></ol></pre><a id="Regular-expressions"></a><h3 class='h3' >Regular expressions</h3>
<p>Perl's regular expression support is both broad and deep, and is the
subject of lengthy documentation in <a href="perlrequick.html">perlrequick</a>, <a href="perlretut.html">perlretut</a>, and
elsewhere. However, in short:</p>
<ul>
<li><a id="Simple-matching"></a><strong>Simple matching</strong>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/foo/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span> <span class="c"># true if $_ contains "foo"</span></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$a</span> =~ <span class="q">/foo/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span> <span class="c"># true if $a contains "foo"</span></li></ol></pre><p>The <code class="inline"><span class="q">//</span></code>
matching operator is documented in <a href="perlop.html">perlop</a>. It operates on
<code class="inline"><span class="i">$_</span></code>
by default, or can be bound to another variable using the <code class="inline">=~</code>
binding operator (also documented in <a href="perlop.html">perlop</a>).</p>
</li>
<li><a id="Simple-substitution"></a><strong>Simple substitution</strong>
<pre class="verbatim"><ol><li> <span class="q">s/foo/bar/</span><span class="sc">;</span> <span class="c"># replaces foo with bar in $_</span></li><li> <span class="i">$a</span> =~ <span class="q">s/foo/bar/</span><span class="sc">;</span> <span class="c"># replaces foo with bar in $a</span></li><li> <span class="i">$a</span> =~ <span class="q">s/foo/bar/g</span><span class="sc">;</span> <span class="c"># replaces ALL INSTANCES of foo with bar</span></li><li> <span class="c"># in $a</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> substitution operator is documented in <a href="perlop.html">perlop</a>.</p>
</li>
<li><a id="More-complex-regular-expressions"></a><strong>More complex regular expressions</strong>
<p>You don't just have to match on fixed strings. In fact, you can match
on just about anything you could dream of by using more complex regular
expressions. These are documented at great length in <a href="perlre.html">perlre</a>, but for
the meantime, here's a quick cheat sheet:</p>
<pre class="verbatim"><ol><li> . <span class="w">a</span> <span class="w">single</span> <span class="w">character</span></li><li> \<span class="q">s a whitespace character</span> <span class="s">(</span><span class="w">space</span><span class="cm">,</span> <span class="w">tab</span><span class="cm">,</span> <span class="w">newline</span><span class="cm">,</span></li><li> ...<span class="s">)</span></li><li> \<span class="w">S</span> <span class="w">non</span>-<span class="w">whitespace</span> <span class="w">character</span></li><li> \<span class="w">d</span> <span class="w">a</span> <span class="w">digit</span> <span class="s">(</span><span class="n">0</span>-<span class="n">9</span><span class="s">)</span></li><li> \<span class="w">D</span> <span class="w">a</span> <span class="w">non</span>-<span class="w">digit</span></li><li> \<span class="w">w</span> <span class="w">a</span> <span class="w">word</span> <span class="w">character</span> <span class="s">(</span><span class="w">a</span>-z<span class="cm">,</span> <span class="w">A</span>-<span class="w">Z</span><span class="cm">,</span> <span class="n">0</span>-<span class="n">9</span><span class="cm">,</span> <span class="w">_</span><span class="s">)</span></li><li> \<span class="w">W</span> <span class="w">a</span> <span class="w">non</span>-<span class="w">word</span> <span class="w">character</span></li><li> <span class="s">[</span><span class="w">aeiou</span><span class="s">]</span> <span class="w">matches</span> <span class="w">a</span> <span class="w">single</span> <span class="w">character</span> <span class="w">in</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a> <span class="w">set</span></li><li> <span class="s">[</span>^<span class="w">aeiou</span><span class="s">]</span> <span class="w">matches</span> <span class="w">a</span> <span class="w">single</span> <span class="w">character</span> <span class="w">outside</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a></li><li> <span class="i">set</span></li><li> <span class="s">(</span><span class="w">foo</span>|<span class="w">bar</span>|<span class="w">baz</span><span class="s">)</span> <span class="w">matches</span> <span class="w">any</span> <span class="w">of</span> <span class="w">the</span> <span class="w">alternatives</span> <span class="w">specified</span></li><li></li><li> ^ <span class="w">start</span> <span class="w">of</span> <span class="w">string</span></li><li> <span class="i">$ end</span> <span class="w">of</span> <span class="w">string</span></li></ol></pre><p>Quantifiers can be used to specify how many of the previous thing you
want to match on, where "thing" means either a literal character, one
of the metacharacters listed above, or a group of characters or
metacharacters in parentheses.</p>
<pre class="verbatim"><ol><li> <span class="i">* zero</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> + <span class="w">one</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> ? <span class="w">zero</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">one</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="s">}</span> <span class="w">matches</span> <span class="w">exactly</span> <span class="n">3</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="cm">,</span><span class="n">6</span><span class="s">}</span> <span class="w">matches</span> <span class="w">between</span> <span class="n">3</span> <a class="l_k" href="functions/and.html">and</a> <span class="n">6</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li><li> <span class="s">{</span><span class="n">3</span><span class="cm">,</span><span class="s">}</span> <span class="w">matches</span> <span class="n">3</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">of</span> <span class="w">the</span> <span class="w">previous</span> <span class="w">thing</span></li></ol></pre><p>Some brief examples:</p>
<pre class="verbatim"><ol><li> <span class="q">/^\d+/</span> <span class="w">string</span> <span class="w">starts</span> <span class="w">with</span> <span class="w">one</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">more</span> <span class="w">digits</span></li><li> <span class="q">/^$/</span> <span class="w">nothing</span> <span class="w">in</span> <span class="w">the</span> <span class="w">string</span> <span class="s">(</span><span class="w">start</span> <a class="l_k" href="functions/and.html">and</a> <span class="w">end</span> <span class="w">are</span></li><li> <span class="w">adjacent</span><span class="s">)</span></li><li> /<span class="s">(</span>\<span class="w">d</span>\<span class="q">s){3}/ three digits, each followed by a whitespace</span></li><li> <span class="q"> character (eg "3 4 5 ")</span></li><li> <span class="q"> /(a.)</span>+<span class="q">/ matches a string in which every odd-numbered</span></li><li> <span class="q"> letter is a (eg "abacadaf")</span></li><li></li><li> <span class="q"> # This loop reads from STDIN, and prints non-blank lines:</span></li><li> <span class="q"> while (<>) {</span></li><li> <span class="q"> next if /</span>^<span class="i">$/</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a id="Parentheses-for-capturing"></a><strong>Parentheses for capturing</strong>
<p>As well as grouping, parentheses serve a second purpose. They can be
used to capture the results of parts of the regexp match for later use.
The results end up in <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
and so on.</p>
<pre class="verbatim"><ol><li> <span class="c"># a cheap and nasty way to break an email address up into parts</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$email</span> =~ <span class="q">/([^@]+)@(.+)/</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Username is $1\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Hostname is $2\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre></li>
<li><a id="Other-regexp-features"></a><strong>Other regexp features</strong>
<p>Perl regexps also support backreferences, lookaheads, and all kinds of
other complex details. Read all about them in <a href="perlrequick.html">perlrequick</a>,
<a href="perlretut.html">perlretut</a>, and <a href="perlre.html">perlre</a>.</p>
</li>
</ul>
<a id="Writing-subroutines"></a><h3 class='h3' >Writing subroutines</h3>
<p>Writing subroutines is easy:</p>
<pre class="verbatim"><ol><li><a name="logger"></a> sub <span class="m">logger</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$logfile</span><span class="cm">,</span> <span class="q">">>"</span><span class="cm">,</span> <span class="q">"my.log"</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">"Could not open my.log: $!"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$logfile</span> <span class="i">$logmessage</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Now we can use the subroutine just as any other built-in function:</p>
<pre class="verbatim"><ol><li> <span class="i">logger</span><span class="s">(</span><span class="q">"We have a logger subroutine!"</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>What's that <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code>? Well, the arguments to a subroutine are available
to us as a special array called <code class="inline"><span class="i">@_</span></code>
(see <a href="perlvar.html">perlvar</a> for more on that).
The default argument to the <code class="inline"><a class="l_k" href="functions/shift.html">shift</a></code> function just happens to be <code class="inline"><span class="i">@_</span></code>
.
So <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></code>
shifts the first item off the list of
arguments and assigns it to <code class="inline"><span class="i">$logmessage</span></code>
.</p>
<p>We can manipulate <code class="inline"><span class="i">@_</span></code>
in other ways too:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$logmessage</span><span class="cm">,</span> <span class="i">$priority</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span> <span class="c"># common</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$logmessage</span> = <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span> <span class="c"># uncommon, and ugly</span></li></ol></pre><p>Subroutines can also return values:</p>
<pre class="verbatim"><ol><li><a name="square"></a> sub <span class="m">square</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$num</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$result</span> = <span class="i">$num</span> * <span class="i">$num</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>Then use it like:</p>
<pre class="verbatim"><ol><li> <span class="i">$sq</span> = <span class="i">square</span><span class="s">(</span><span class="n">8</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>For more information on writing subroutines, see <a href="perlsub.html">perlsub</a>.</p>
<a id="OO-Perl"></a><h3 class='h3' >OO Perl</h3>
<p>OO Perl is relatively simple and is implemented using references which
know what sort of object they are based on Perl's concept of packages.
However, OO Perl is largely beyond the scope of this document.
Read <a href="perlootut.html">perlootut</a> and <a href="perlobj.html">perlobj</a>.</p>
<p>As a beginning Perl programmer, your most common use of OO Perl will be
in using third-party modules, which are documented below.</p>
<a id="Using-Perl-modules"></a><h3 class='h3' >Using Perl modules</h3>
<p>Perl modules provide a range of features to help you avoid reinventing
the wheel, and can be downloaded from CPAN ( <a href="http://www.cpan.org/">http://www.cpan.org/</a> ). A
number of popular modules are included with the Perl distribution
itself.</p>
<p>Categories of modules range from text manipulation to network protocols
to database integration to graphics. A categorized list of modules is
also available from CPAN.</p>
<p>To learn how to install modules you download from CPAN, read
<a href="perlmodinstall.html">perlmodinstall</a>.</p>
<p>To learn how to use a particular module, use <code class="inline">perldoc <i>Module::Name</i></code>.
Typically you will want to <code class="inline">use <i>Module::Name</i></code>, which will then give
you access to exported functions or an OO interface to the module.</p>
<p><a href="perlfaq.html">perlfaq</a> contains questions and answers related to many common
tasks, and often provides suggestions for good CPAN modules to use.</p>
<p><a href="perlmod.html">perlmod</a> describes Perl modules in general. <a href="https://search.cpan.org/perldoc/perlmodlib">perlmodlib</a> lists the
modules which came with your Perl installation.</p>
<p>If you feel the urge to write Perl modules, <a href="perlnewmod.html">perlnewmod</a> will give you
good advice.</p>
<a id="AUTHOR"></a><h2 class='h2' >AUTHOR</h2>
<p>Kirrily "Skud" Robert <skud@cpan.org></p>
<ul><li><a href="#NAME">NAME</a></li><li><a href="#DESCRIPTION">DESCRIPTION</a></li><ul><li><a href="#What-is-Perl%3f">What is Perl?</a></li><li><a href="#Running-Perl-programs">Running Perl programs</a></li><li><a href="#Safety-net">Safety net</a></li><li><a href="#Basic-syntax-overview">Basic syntax overview</a></li><li><a href="#Perl-variable-types">Perl variable types</a></li><li><a href="#Variable-scoping">Variable scoping</a></li><li><a href="#Conditional-and-looping-constructs">Conditional and looping constructs</a></li><li><a href="#Builtin-operators-and-functions">Builtin operators and functions</a></li><li><a href="#Files-and-I%2fO">Files and I/O</a></li><li><a href="#Regular-expressions">Regular expressions</a></li><li><a href="#Writing-subroutines">Writing subroutines</a></li><li><a href="#OO-Perl">OO Perl</a></li><li><a href="#Using-Perl-modules">Using Perl modules</a></li></ul><li><a href="#AUTHOR">AUTHOR</a></li></ul>
</div>
</article>
</div>
</div>
</main>
</div>
<footer class="footer row bg-light pt-5 pb-5">
<div class="col-sm-8 offset-sm-2">
<div class="row">
<div class="col-md-4">
<ul class="list-unstyled">
<li>
<h4>Site info</h4>
</li>
<li>Docs mantained by
<a href="//lists.cpan.org/showlist.cgi?name=perl5-porters" rel="noopener">Perl 5 Porters</a>
</li>
<li>Site development sponsored by
<a href="//opusvl.com" rel="noopener"><img class="opus-logo" src="/public/img/opusvl_logo.svg" alt="OpusVL Logo"></a>
</li>
</ul>
</div>
<div class="col-md-8">
<div class="row">
<div class="col-md-6 col-lg-3">
<ul class=" list-unstyled">
<li>
<h4>Manual</h4>
</li>
<li>
<a href="/5/28/2/index-overview.html">Overview</a>
</li>
<li>
<a href="/5/28/2/index-tutorials.html">Tutorials</a>
</li>
<li>
<a href="/5/28/2/index-faq.html">FAQs</a>
</li>
<li>
<a href="/5/28/2/index-history.html">Changes</a>
</li>
</ul>
</div>
<div class="col-md-6 col-lg-3">
<ul class=" list-unstyled">
<li>
<h4>Reference</h4>
</li>
<li>
<a href="/5/28/2/index-language.html">Language</a>
</li>
<li>
<a href="/5/28/2/index-functions.html">Functions</a>
</li>
<li>
<a href="/5/28/2/perlop.html">Operators</a>
</li>
<li>
<a href="/5/28/2/perlvar.html">Variables</a>
</li>
</ul>
</div>
<div class="col-md-6 col-lg-3">
<ul class=" list-unstyled">
<li>
<h4>Modules</h4>
</li>
<li>
<a href="/5/28/2/index-modules-A.html">Modules</a>
</li>
<li>
<a href="/5/28/2/index-pragmas.html">Pragmas</a>
</li>
<li>
<a href="/5/28/2/index-utilities.html">Utilities</a>
</li>
</ul>
</div>
<div class="col-md-6 col-lg-3">
<ul class=" list-unstyled">
<li>
<h4>Misc</h4>
</li>
<li>
<a href="/5/28/2/index-licence.html">License</a>
</li>
<li>
<a href="/5/28/2/index-internals.html">Internals</a>
</li>
<li>
<a href="/5/28/2/index-platforms.html">Platforms</a>
</li>
</ul>
</div>
</div>
</div>
<div class=" col-12 footer-inf text-center">
<small>perldoc.perl.org - Official documentation for the Perl programming language</small>
</div>
</div>
</div>
</footer>
<div class="beta-wrapper beta-bottom-right ">
<div class="beta beta-blue ">
<a class="beta-link " href="https://github.com/OpusVL/perldoc.perl.org/issues/new" rel="noopener">
<span class="beta-text">
This is a Beta release.
</span>
<span class="beta-text">
For any issues please raise a ticket on GitHub
</span>
</a>
</div>
</div>
<script src="/public/js/main.min.js"></script>
</body>
</html>