-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl4.html
318 lines (305 loc) · 11.4 KB
/
perl4.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
<!DOCTYPE html>
<head>
<!--
NAIM AYA NAI NAIMA NAI
TNAIM AYA MAY YATNAI MAY
NAIMAY ATN MAYATNA ATN
NAIMAYA TNA NAIMA. ATN NAIMAYA.TNAI. IMAY ATN NAI MAY NAIMA. NAIMAY
IMA YATNAIM "YAT AIM MAY "ATN "AIM AIMA YAT ATN AIM "YAT ATN
AYA TNAIMA .NAIMAYA AYA AYA TNA IMA NAIM AYA AYA TNA .NAIMAYA AIM
YAT NAIMA TNA IMA TNA YAT NAI MAY TNAIMAYATNA IMAY ATN TNA IMA AYAT.
YAT NAIM "YATNAIM IMA ATN AIM AYA IMAY ATN "AIMAY "YATNAIM "NAIM
TNA
IMA YATN
"AIMA"
CREATED BY NAIM AYAT | NaimAyat.me
-->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "http://NaimAyat.me/perl2.html"
},
"headline": "Perl Tutorial Part 3: Regular Expressions",
"image": {
"@type": "ImageObject",
"url": "http://naimayat.me/favicons/android-chrome-96x96.png",
"height": 60,
"width": 700
},
"datePublished": "2017-08-03T20:00:00+08:00",
"dateModified": "2015-08-04T00:00:00+08:00",
"author": {
"@type": "Person",
"name": "Naim Ayat"
},
"publisher": {
"@type": "Organization",
"name": "Naim Ayat",
"logo": {
"@type": "ImageObject",
"url": "http://naimayat.me/favicons/android-chrome-96x96.png",
"width": 60,
"height": 60
}
},
"description": "Part three of Naim Ayat's tutorial for the Perl programming language."
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"name": "Naim Ayat",
"url": "http://NaimAyat.me",
"sameAs": ["https://www.linkedin.com/in/NaimAyat", "https://www.github.com/NaimAyat", "https://www.twitter.com/NaimAyat", "https://www.instagram.com/NaimAyat", "https://www.facebook.com/NaimAyat"],
"logo": "http://NaimAyat.me/img/NaimAyat.png",
"contactPoint": [{
"@type": "ContactPoint",
"telephone": "+1-707-495-0005",
"email": "[email protected]",
"contactType": "sales"
}]
}
</script>
<script type='application/ld+json'>
{
"@context": "http://www.schema.org",
"@type": "person",
"name": "Naim Ayat",
"url": "http://naimayat.me",
"image": "https://pbs.twimg.com/profile_images/861748279838507008/74RQ3BcV_400x400.jpg",
"sameAs": ["https://www.linkedin.com/in/NaimAyat", "https://www.github.com/NaimAyat", "https://www.twitter.com/NaimAyat", "https://www.instagram.com/NaimAyat", "https://www.facebook.com/NaimAyat"],
"email": "[email protected]",
"description": "Computer science student at UCLA.",
"alumniOf": ["University of California, Los Angeles", "Stanford University", "Sonoma State University"]
}
</script>
<script type='application/ld+json'>
{
"@context": "http://schema.org",
"@type": "WebSite",
"name": "Naim Ayat",
"alternateName": "NaimAyat.me",
"url": "http://naimayat.me"
}
</script>
<meta charset="utf-8" />
<meta name="description" content="Perl programming tutorials by Naim Ayat.">
<meta name="keywords" content="Naim,Ayat,NaimAyat,NaimAyat.me,Portfolio,Computer,Science,Programming,HTML,CSS,XML,XHTML,JavaScript,Cpp,C,Perl,Python,Java">
<meta name="author" content="Naim Ayat">
<meta property="og:image" content="http://naimayat.me/favicons/og_small.png">
<meta property='og:title' content="Naim Ayat | Perl Tutorial 4">
<meta property='og:description' content="Perl programming tutorials by Naim Ayat.">
<meta property='og:url' content="http://naimayat.me/perl4">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@NaimAyat">
<meta name="twitter:creator" content="@NaimAyat">
<meta name="twitter:title" content="Naim Ayat | Perl Tutorial 4">
<meta name="twitter:description" content="Perl programming tutorials by Naim Ayat.">
<meta name="twitter:image" content="http://naimayat.me/favicons/og.png">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Naim Ayat | Perl Tutorial 4</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<link href="css/font-awesome.min.css" rel="stylesheet"/>
<link rel="shortcut icon" type="image/png" href="img/favicon2.png"/>
<link rel="apple-touch-icon" sizes="76x76" href="/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png">
<link rel="manifest" href="/favicons/manifest.json">
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#000000">
<meta name="theme-color" content="#ffffff">
</head>
<body vlink="white" link="white">
<header>
<div id="header-inner">
<a href="/index.html" id="logo">NAIM AYAT</a>
<nav>
<a href="#" id="menu-icon"></a>
<ul>
<li><a href="/index.html">HOME</a></li>
<li><div class="dropdown">
<a href="#"><div class="dropbtn">EXPERIENCE</div></a>
<div class="dropdown-content">
<a href="/wdc.html">WESTERN DIGITAL</a>
<a href="/ziprecruiter.html">ZIPRECRUITER</a>
<a href="/perl.html">PERL TUTORIALS</a>
<a href="/projects.html">PROJECTS</a>
</div>
</div></li>
<li><a href="/contact.html">CONTACT</a></li>
</ul>
</nav>
</div>
</header>
<!--END HEADER-->
<section class ="banner-outer-perl-content">
<div class="banner-perl-content">
<h2>Perl Tutorial Part 4:<br>Working with Files</h2><br>
<h1>READING FILES</h1>
<br>
To open a filehandle for reading:
<br><br>
<div id="code">
   open FH, $filename or die $!;
</div><br>
This opens the file specified in the command line, loops through it and feeds each
line to the special variable $_.
<br><br>
We can also open a filehandle with a string literal:
<br><br>
<div id="code">
   open FH, 'file.txt' or die $!;
</div><br>
To read lines from a file, just use <FH> as if it were <STDIN>. Like
<STDIN> reads one line of input, <FH> reads one line from a filehandle.
<br><br>
ARGV reads the names of files from the command line and opens them all.
If there aren't any, it reads from STDIN.
<br><br>
@ARGV contains any text after the program's name on the command line.
<ARGV> takes each file in turn. It can be abbreviated to just <>.
Similarly, $ARGV holds the name of the file currently being read.
<br><br>
<div id="code">
   while (<FH>);
</div><br>
The above statement will read in every line at once. This is very memory-intensive and should be
avoided. Here's an example of how to efficiently store every line in an array:
<br><br>
<div id="code">
   open INPUT, "somefile.txt" or die $!;<br>
   my @data;<br>
   @data = <INPUT> ; <br>
</div><br>
The diamond operator <FILEHANDLE> reads in a line from the file.
We control the definition of a line by altering the record separator, $/.
<br><br>
<h1>WRITING TO FILES</h1>
<br>
To open a file for writing (and overwrite it):
<br><br>
<div id="code">
   open FH, "> $filename" or die $!;
</div><br>
To open a file for writing (and append to it instead of overwriting):
<br><br>
<div id="code">
   open FH, ">> $filename" or die $!;
</div><br>
When we want to write to the file, we use a slightly modified print:
<br><br>
<div id="code">
   print FH "something";
</div><br>
To change the default print to print FILEHANDLE:
<br><br>
<div id="code">
   select FILEHANDLE;
</div><br>
To turn off buffering:
<br><br>
<div id="code">
   $| = 1;
</div><br>
Coversely, to turn on buffering (default):
<br><br>
<div id="code">
   $| = 0;
</div><br>
<h1>FILE TESTS</h1>
<br>
<table>
<tr>
<th>FLAG</th>
<th>MEANING</th>
</tr>
<tr>
<td>-e</td>
<td>True if the file exists.</td>
</tr>
<tr>
<td>-f</td>
<td>True if the file is a plain file – not a directory.</td>
</tr>
<tr>
<td>-d</td>
<td>True if the file is a directory.</td>
</tr>
<tr>
<td>-z</td>
<td>True if the file has zero size.</td>
</tr>
<tr>
<td>-s</td>
<td>True if the file has nonzero size – returns file size in bytes.</td>
</tr>
<tr>
<td>-r</td>
<td>True if the file is readable by you.</td>
</tr>
<tr>
<td>-w</td>
<td>True if the file is writable by you.</td>
</tr>
<tr>
<td>-x</td>
<td>True if the file is executable by you.</td>
</tr>
<tr>
<td>-o</td>
<td>True if the file is owned by you.</td>
</tr>
</table><br>
Syntax example:
<br><br>
<div id="code">
   if (-e $filename) {statement}
</div><br>
<h1>DIRECTORIES</h1>
<br>
Glob takes a string and returns the matching files. * matches any text.
To get all the files in a directory:
<br><br>
<div id="code">
   my @files = glob("*")
</div><br>
Open directories with opendir:
<br><br>
<div id="code">
   opendir DH, "." or die $!;
</div><br>
Read directories with readdir:
<br><br>
<div id="code">
   while ($_ = readdir(DH)) {statement}
</div><br>
The next part of this tutorial will be published here soon. Until then, you can view <a href="https://github.com/NaimAyat/Perl_Tutorials" target="_blank">the entire series on GitHub</a>.<br><br>
<a href="/perl1.html">Part 1: Data Types</a><br>
<a href="/perl2.html">Part 2: Logical Operators & Loops</a><br>
<a href="/perl3.html">Part 3: Regular Expressions</a><br>
<a href="/perl4.html" class="current">Part 4: Working with Files</a><br>
</section>
<!--END BANNER-->
<footer>
<ul class="social">
<li><a href="https://www.linkedin.com/in/NaimAyat" target="_blank"><i class="fa fa-linkedin"></i></a></li>
        
<li><a href="https://www.github.com/NaimAyat" target="_blank"><i class="fa fa-github"></i></a></li>
        
<li><a href="https://www.twitter.com/NaimAyat" target="_blank"><i class="fa fa-twitter"></i></a></li>
        
<li><a href="https://www.instagram.com/NaimAyat" target="_blank"><i class="fa fa-instagram"></i></a></li>
        
<li><a href="https://www.facebook.com/NaimAyat" target="_blank"><i class="fa fa-facebook"></i></a></li>
</ul>
</footer>
<!--END SOCIAL FOOTER-->
<footer class="second">
<a href="https://github.com/NaimAyat/NaimAyat.github.io" id= "open-source" target="_blank">© 2019 NAIM AYAT</a>
</footer>
<!--END SECOND FOOTER-->
</body>
</html>