1 From alice@schiffwood.demon.co.uk Mon Feb 10 03:57:43 2003
2 Return-Path: <alice@schiffwood.demon.co.uk>
3 Delivered-To: courses@linuxchix.org
4 Received: from localhost (localhost [127.0.0.1])
5 by www.linuxchix.org (Postfix) with ESMTP
6 id EFC482730F4; Mon, 10 Feb 2003 03:57:42 +1100 (EST)
7 Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85])
8 by www.linuxchix.org (Postfix) with ESMTP
9 id C2DEE27309D; Mon, 10 Feb 2003 03:57:41 +1100 (EST)
10 Received: from schiffwood.demon.co.uk ([193.237.181.189] helo=arnie)
11 by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2)
12 id 18hulv-0001JM-0Z; Sun, 09 Feb 2003 16:57:39 +0000
13 Content-Type: text/plain;
15 From: Alice Wood <alice@schiffwood.demon.co.uk>
16 Reply-To: alice@schiffwood.demon.co.uk
17 To: programming@linuxchix.org, courses@linuxchix.org
18 Date: Sun, 9 Feb 2003 16:51:14 +0000
19 User-Agent: KMail/1.4.1
20 References: <002801c2cd5c$2d9fd510$1201a8c0@qbert> <200302052133.31075.alice@schiffwood.demon.co.uk> <01fc01c2cf95$a6805460$017641db@phoenix>
21 In-Reply-To: <01fc01c2cf95$a6805460$017641db@phoenix>
23 Content-Transfer-Encoding: quoted-printable
24 Message-Id: <200302091651.14555.alice@schiffwood.demon.co.uk>
25 X-Virus-Scanned: by AMaViS new-20020517
26 Subject: [Courses] Re: [prog] Perl course
27 Sender: courses-admin@linuxchix.org
28 Errors-To: courses-admin@linuxchix.org
29 X-BeenThere: courses@linuxchix.org
30 X-Mailman-Version: 2.0.1
32 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
33 List-Post: <mailto:courses@linuxchix.org>
34 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
35 <mailto:courses-request@linuxchix.org?subject=subscribe>
36 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
37 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
38 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
39 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
43 On Saturday 08 February 2003 16:44, Sachin wrote:
45 > It seems that lots =
47 > people are interested in learning it(which includes mine).Exactly when =
56 I cannot believe the response my post has had. I would like to thank ever=
58 who has offered help. So yes I will be going ahead with this. I would lik=
60 start on March 1st, and post lessons/exercises every second Saturday. I=20
61 realise that some people will think this is a little slow at the beginnin=
63 tend to agree, but given that the course will get more difficult and peop=
65 cannot always make weekly deadlines, two weeks seems more practical. Also=
67 want to start March 1st as I haven't written anything yet and this gives =
71 I would like to use Learning Perl as the course book (ISBN:0-596-00132-0)=
73 this is generally known as the Llama book, and Randal Schwartz has given=20
74 permission to use bits and pieces out of this. If people do not want to g=
76 the expense of buying a book, there is information online at=20
77 http://learn.perl.org where there is a downloadable version of Beginning =
79 which should be useful (note this is not the same book as Learning Perl),=
81 also there are notes at http://www.perltraining.com.au. which are very=20
82 helpful. It is not absolutely necessary to have the Learning Perl book, b=
84 if you haven't got any material at all, this is where I would start.
86 My aim is to send an email to the courses list every two weeks with a=20
87 tutorial, exercises and useful links in it. I will also hope to post to a=
89 permanent site, at the same time, an HTML version which may be slightly=20
90 expanded, so if you miss a post it should be easy to catch up.
92 Any comments / questions please to me at the addy above, otherwise I'll s=
94 you bright and early March 1st for the first lesson.
100 From admin@surgo.net Mon Feb 10 04:04:03 2003
101 Return-Path: <admin@surgo.net>
102 Delivered-To: courses@linuxchix.org
103 Received: from localhost (localhost [127.0.0.1])
104 by www.linuxchix.org (Postfix) with ESMTP id DEF7E2730F4
105 for <courses@linuxchix.org>; Mon, 10 Feb 2003 04:04:02 +1100 (EST)
106 Received: from Daedalus (pcp01415323pcs.potshe01.pa.comcast.net [68.81.44.95])
107 by www.linuxchix.org (Postfix) with ESMTP id 9800127309B
108 for <courses@linuxchix.org>; Mon, 10 Feb 2003 04:04:01 +1100 (EST)
109 Received: from [68.81.44.211] (helo=pcp01415439pcs.potshe01.pa.comcast.net ident=venthorn)
110 by Daedalus with esmtp (Exim 3.36 #1 (Debian))
112 for <courses@linuxchix.org>; Sun, 09 Feb 2003 11:01:08 -0500
113 From: Morgon Kanter <admin@surgo.net>
114 To: courses@linuxchix.org
115 Date: Sun, 9 Feb 2003 12:03:39 -0500
116 User-Agent: KMail/1.5
117 References: <002801c2cd5c$2d9fd510$1201a8c0@qbert> <01fc01c2cf95$a6805460$017641db@phoenix> <200302091651.14555.alice@schiffwood.demon.co.uk>
118 In-Reply-To: <200302091651.14555.alice@schiffwood.demon.co.uk>
120 Content-Type: text/plain;
122 Content-Transfer-Encoding: 7bit
123 Content-Disposition: inline
124 Message-Id: <200302091203.39703.admin@surgo.net>
125 X-Virus-Scanned: by AMaViS new-20020517
126 Subject: [Courses] Re: [prog] Perl course
127 Sender: courses-admin@linuxchix.org
128 Errors-To: courses-admin@linuxchix.org
129 X-BeenThere: courses@linuxchix.org
130 X-Mailman-Version: 2.0.1
132 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
133 List-Post: <mailto:courses@linuxchix.org>
134 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
135 <mailto:courses-request@linuxchix.org?subject=subscribe>
136 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
137 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
138 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
139 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
143 I'm definately interested in learning. I was supposed to get the llama book
144 for Xmas, but the post office lost it *mutter*.
148 grab my public key from http://www.surgo.net/pubkey.asc
152 From suzo@bigpond.net.au Sun Feb 23 15:54:33 2003
153 Return-Path: <suzo@bigpond.net.au>
154 Delivered-To: courses@linuxchix.org
155 Received: from localhost (localhost [127.0.0.1])
156 by www.linuxchix.org (Postfix) with ESMTP
157 id DA94F27310F; Sun, 23 Feb 2003 15:54:32 +1100 (EST)
158 Received: from mta03bw.bigpond.com (mta03bw.bigpond.com [139.134.6.86])
159 by www.linuxchix.org (Postfix) with ESMTP
160 id C72C2273106; Sun, 23 Feb 2003 15:54:31 +1100 (EST)
161 Received: from leviathan ([144.135.24.69]) by
162 mta03bw.bigpond.com (Netscape Messaging Server 4.15 mta03bw Jul
163 16 2002 22:47:55) with SMTP id HAQWAT00.A5H; Sun, 23 Feb 2003
165 Received: from CPE-144-137-74-179.nsw.bigpond.net.au ([144.137.74.179]) by bwmam01.mailsvc.email.bigpond.com(MailRouter V3.0n 8/15668083); 23 Feb 2003 14:54:29
166 Message-ID: <003401c2daf7$ab301eb0$0100000a@leviathan>
167 From: "Sue Stones" <suzo@bigpond.net.au>
168 To: <alice@schiffwood.demon.co.uk>, <programming@linuxchix.org>,
169 <courses@linuxchix.org>
170 References: <002801c2cd5c$2d9fd510$1201a8c0@qbert> <200302052133.31075.alice@schiffwood.demon.co.uk> <01fc01c2cf95$a6805460$017641db@phoenix> <200302091651.14555.alice@schiffwood.demon.co.uk>
171 Date: Sun, 23 Feb 2003 15:54:37 +1100
172 Organization: Vyette's House
174 Content-Type: text/plain;
176 Content-Transfer-Encoding: 7bit
178 X-MSMail-Priority: Normal
179 X-Mailer: Microsoft Outlook Express 6.00.2720.3000
180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
181 X-Virus-Scanned: by AMaViS new-20020517
182 Subject: [Courses] Re: [prog] Perl course
183 Sender: courses-admin@linuxchix.org
184 Errors-To: courses-admin@linuxchix.org
185 X-BeenThere: courses@linuxchix.org
186 X-Mailman-Version: 2.0.1
188 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
189 List-Post: <mailto:courses@linuxchix.org>
190 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
191 <mailto:courses-request@linuxchix.org?subject=subscribe>
192 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
193 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
194 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
195 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
199 How much difference is there between the 2nd and 3rd editions?
204 ----- Original Message -----
205 From: "Alice Wood" <alice@schiffwood.demon.co.uk>
206 To: <programming@linuxchix.org>; <courses@linuxchix.org>
207 Sent: Monday, February 10, 2003 3:51 AM
208 Subject: Re: [prog] Perl course
211 On Saturday 08 February 2003 16:44, Sachin wrote:
213 > It seems that lots of
214 > people are interested in learning it(which includes mine).Exactly when are
215 > you going to start.
222 I cannot believe the response my post has had. I would like to thank
224 who has offered help. So yes I will be going ahead with this. I would like
226 start on March 1st, and post lessons/exercises every second Saturday. I
227 realise that some people will think this is a little slow at the beginning.
229 tend to agree, but given that the course will get more difficult and people
230 cannot always make weekly deadlines, two weeks seems more practical. Also, I
231 want to start March 1st as I haven't written anything yet and this gives
235 I would like to use Learning Perl as the course book (ISBN:0-596-00132-0),
236 this is generally known as the Llama book, and Randal Schwartz has given
237 permission to use bits and pieces out of this. If people do not want to go
239 the expense of buying a book, there is information online at
240 http://learn.perl.org where there is a downloadable version of Beginning
242 which should be useful (note this is not the same book as Learning Perl),
243 also there are notes at http://www.perltraining.com.au. which are very
244 helpful. It is not absolutely necessary to have the Learning Perl book, but
245 if you haven't got any material at all, this is where I would start.
247 My aim is to send an email to the courses list every two weeks with a
248 tutorial, exercises and useful links in it. I will also hope to post to a
249 permanent site, at the same time, an HTML version which may be slightly
250 expanded, so if you miss a post it should be easy to catch up.
252 Any comments / questions please to me at the addy above, otherwise I'll see
253 you bright and early March 1st for the first lesson.
258 _______________________________________________
259 Programming mailing list
260 Programming@linuxchix.org
261 http://mailman.linuxchix.org/mailman/listinfo/programming
266 From alice@schiffwood.demon.co.uk Mon Feb 24 07:26:53 2003
267 Return-Path: <alice@schiffwood.demon.co.uk>
268 Delivered-To: courses@linuxchix.org
269 Received: from localhost (localhost [127.0.0.1])
270 by www.linuxchix.org (Postfix) with ESMTP
271 id E79A727313F; Mon, 24 Feb 2003 07:26:52 +1100 (EST)
272 Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89])
273 by www.linuxchix.org (Postfix) with ESMTP
274 id 61F992730F1; Mon, 24 Feb 2003 07:26:51 +1100 (EST)
275 Received: from schiffwood.demon.co.uk ([193.237.181.189] helo=arnie)
276 by anchor-post-31.mail.demon.net with esmtp (Exim 3.35 #1)
277 id 18n2hy-0000CK-0V; Sun, 23 Feb 2003 20:26:47 +0000
278 Content-Type: text/plain;
280 From: Alice Wood <alice@schiffwood.demon.co.uk>
281 Reply-To: alice@schiffwood.demon.co.uk
282 To: "Sue Stones" <suzo@bigpond.net.au>, <programming@linuxchix.org>,
283 <courses@linuxchix.org>
284 Subject: Re: [Courses] Re: [prog] Perl course
285 Date: Sun, 23 Feb 2003 20:22:52 +0000
286 User-Agent: KMail/1.4.1
287 References: <002801c2cd5c$2d9fd510$1201a8c0@qbert> <200302091651.14555.alice@schiffwood.demon.co.uk> <003401c2daf7$ab301eb0$0100000a@leviathan>
288 In-Reply-To: <003401c2daf7$ab301eb0$0100000a@leviathan>
290 Content-Transfer-Encoding: quoted-printable
291 Message-Id: <200302232022.52659.alice@schiffwood.demon.co.uk>
292 X-Virus-Scanned: by AMaViS new-20020517
293 Sender: courses-admin@linuxchix.org
294 Errors-To: courses-admin@linuxchix.org
295 X-BeenThere: courses@linuxchix.org
296 X-Mailman-Version: 2.0.1
298 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
299 List-Post: <mailto:courses@linuxchix.org>
300 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
301 <mailto:courses-request@linuxchix.org?subject=subscribe>
302 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
303 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
304 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
305 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
309 On Sunday 23 February 2003 04:54, Sue Stones wrote:
310 > How much difference is there between the 2nd and 3rd editions?
316 Sorry it has taken so long to reply to you, I got too involved in my=20
319 The answer to the above question seems to be quite a lot. I have an elect=
321 version of 2 and a paper version of 3. Below is a summary of the differen=
323 as noted in the preface to 3.
325 Completely new text, taken from teaching the Perl course (it has to be sa=
327 that the text does occasionally suffer from a lack of clear explanation, =
329 may help with this, along with LinuxChix)
331 The regular expressions section has been split into three parts to make i=
335 The artificial control structures chapter has gone and parts have been=20
336 incorporated into other chapters.
338 Subroutines are brought in earlier.
340 Element syntax is before aggregate syntax for both arrays and hashes.
342 The exercises are different, although not for the first couple of chapter=
345 Information is included on use strict, warnings and modules.
347 Book 3 is also address more to general programmers which tends to make it=
353 I will be basing the course on book 3 obviously, but as I have access to =
355 book 2 I will try to flag up different pages if I can.
364 Project Gutenberg newsletter volunteer
366 From giljacobs@woza.co.za Tue Feb 11 21:49:40 2003
367 Return-Path: <giljacobs@woza.co.za>
368 Delivered-To: courses@linuxchix.org
369 Received: from localhost (localhost [127.0.0.1])
370 by www.linuxchix.org (Postfix) with ESMTP id D9FF02730F4
371 for <Courses@linuxchix.org>; Tue, 11 Feb 2003 21:49:39 +1100 (EST)
372 Received: from mailer.icl-za.com (mailer.icl-za.com [163.203.222.200])
373 by www.linuxchix.org (Postfix) with ESMTP id 16C7A2730CD
374 for <Courses@linuxchix.org>; Tue, 11 Feb 2003 21:49:38 +1100 (EST)
375 Received: from gogga ([163.203.223.142] helo=gogga.icl-za.com)
376 by mailer.icl-za.com with esmtp (Exim 4.10)
378 for Courses@linuxchix.org; Tue, 11 Feb 2003 12:49:27 +0200
379 Received: (from root@localhost)
380 by gogga.icl-za.com (8.11.6/8.11.6) id h1BApQV19466
381 for <Courses@linuxchix.org>; Tue, 11 Feb 2003 12:51:26 +0200
382 Received: from G (user10037.vip-za.com [163.203.135.53])
383 by gogga.icl-za.com (8.11.6/8.11.6) with SMTP id h1BApP119461
384 for <Courses@linuxchix.org>; Tue, 11 Feb 2003 12:51:26 +0200
385 Message-ID: <000e01c2d1bb$e70a59e0$3587cba3@C.Jacobs>
386 Reply-To: "Gil Jacobs" <giljacobs@woza.co.za>
387 From: "Gil Jacobs" <giljacobs@woza.co.za>
388 To: <Courses@linuxchix.org>
389 Date: Tue, 11 Feb 2003 12:54:06 +0200
391 Content-Type: multipart/alternative;
392 boundary="----=_NextPart_000_000B_01C2D1CC.A9544140"
394 X-MSMail-Priority: Normal
395 X-Mailer: Microsoft Outlook Express 6.00.2600.0000
396 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
397 X-Virus-Scanned: by AMaViS new-20020517
398 Subject: [Courses] Perl Course
399 Sender: courses-admin@linuxchix.org
400 Errors-To: courses-admin@linuxchix.org
401 X-BeenThere: courses@linuxchix.org
402 X-Mailman-Version: 2.0.1
404 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
405 List-Post: <mailto:courses@linuxchix.org>
406 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
407 <mailto:courses-request@linuxchix.org?subject=subscribe>
408 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
409 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
410 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
411 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
415 This is a multi-part message in MIME format.
417 ------=_NextPart_000_000B_01C2D1CC.A9544140
418 Content-Type: text/plain;
420 Content-Transfer-Encoding: quoted-printable
422 I'm looking forward to it! Just bought the "Llama" book.
426 This email was scanned by the ICL anti-virus solution
427 ------=_NextPart_000_000B_01C2D1CC.A9544140
428 Content-Type: text/html;
430 Content-Transfer-Encoding: quoted-printable
432 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
434 <META http-equiv=3DContent-Type content=3D"text/html; =
435 charset=3Diso-8859-1">
436 <META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
439 <BODY bgColor=3D#ffffff>
440 <DIV><FONT face=3DArial size=3D2>I'm looking forward to it! Just bought =
443 <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
444 <DIV><FONT face=3DArial size=3D2>Gil jacobs</FONT></DIV><BR><PRE>
445 This email was scanned by the ICL anti-virus solution</pre><BR></BODY></HTML>
447 ------=_NextPart_000_000B_01C2D1CC.A9544140--
450 From jscalf@pegasus.rutgers.edu Sat Feb 15 02:21:29 2003
451 Return-Path: <jscalf@pegasus.rutgers.edu>
452 Delivered-To: courses@linuxchix.org
453 Received: from localhost (localhost [127.0.0.1])
454 by www.linuxchix.org (Postfix) with ESMTP id F307227310A
455 for <Courses@linuxchix.org>; Sat, 15 Feb 2003 02:21:28 +1100 (EST)
456 Received: from ajax.rutgers.edu (ajax.rutgers.edu [128.6.10.9])
457 by www.linuxchix.org (Postfix) with ESMTP id C87DE2730EE
458 for <Courses@linuxchix.org>; Sat, 15 Feb 2003 02:21:27 +1100 (EST)
459 Received: from pegasus.rutgers.edu (new.pegasus-back.rutgers.edu [192.168.0.42])
460 by ajax.rutgers.edu (Postfix) with ESMTP id 181D01104D
461 for <Courses@linuxchix.org>; Fri, 14 Feb 2003 10:21:23 -0500 (EST)
462 Received: by pegasus.rutgers.edu (Postfix, from userid 88966)
463 id EA41172249; Fri, 14 Feb 2003 10:22:06 -0500 (EST)
464 Date: Fri, 14 Feb 2003 10:22:06 -0500 (EST)
465 From: Jennifer Scalf <jscalf@pegasus.rutgers.edu>
466 Cc: Courses@linuxchix.org
467 Subject: Re: [Courses] Perl Course
468 In-Reply-To: <000e01c2d1bb$e70a59e0$3587cba3@C.Jacobs>
469 Message-ID: <Pine.SOL.4.44.0302141021330.13491-100000@pegasus.rutgers.edu>
471 Content-Type: TEXT/PLAIN; charset=US-ASCII
472 X-Virus-Scanned: by AMaViS
473 X-Virus-Scanned: by AMaViS new-20020517
474 Sender: courses-admin@linuxchix.org
475 Errors-To: courses-admin@linuxchix.org
476 X-BeenThere: courses@linuxchix.org
477 X-Mailman-Version: 2.0.1
479 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
480 List-Post: <mailto:courses@linuxchix.org>
481 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
482 <mailto:courses-request@linuxchix.org?subject=subscribe>
483 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
484 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
485 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
486 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
490 I also just got the llama book and will be watching for your course.
496 On Tue, 11 Feb 2003, Gil Jacobs wrote:
498 > I'm looking forward to it! Just bought the "Llama" book.
502 > This email was scanned by the ICL anti-virus solution
505 From alice@schiffwood.demon.co.uk Sat Mar 1 22:47:46 2003
506 Return-Path: <alice@schiffwood.demon.co.uk>
507 Delivered-To: courses@linuxchix.org
508 Received: from localhost (localhost [127.0.0.1])
509 by www.linuxchix.org (Postfix) with ESMTP id 55A80273184
510 for <courses@linuxchix.org>; Sat, 1 Mar 2003 22:47:46 +1100 (EST)
511 Received: from anchor-post-39.mail.demon.net (anchor-post-39.mail.demon.net [194.217.242.80])
512 by www.linuxchix.org (Postfix) with ESMTP id 0689227313B
513 for <courses@linuxchix.org>; Sat, 1 Mar 2003 22:47:43 +1100 (EST)
514 Received: from schiffwood.demon.co.uk ([193.237.181.189] helo=arnie)
515 by anchor-post-39.mail.demon.net with esmtp (Exim 3.36 #2)
517 for courses@linuxchix.org; Sat, 01 Mar 2003 11:47:37 +0000
518 Content-Type: text/plain;
520 From: Alice Wood <alice@schiffwood.demon.co.uk>
521 Reply-To: alice@schiffwood.demon.co.uk
522 To: courses@linuxchix.org
523 Date: Sat, 1 Mar 2003 11:26:26 +0000
524 User-Agent: KMail/1.4.1
526 Content-Transfer-Encoding: quoted-printable
527 Message-Id: <200303011126.26229.alice@schiffwood.demon.co.uk>
528 X-Virus-Scanned: by AMaViS new-20020517
529 Subject: [Courses] Perl course part 1
530 Sender: courses-admin@linuxchix.org
531 Errors-To: courses-admin@linuxchix.org
532 X-BeenThere: courses@linuxchix.org
533 X-Mailman-Version: 2.0.1
535 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
536 List-Post: <mailto:courses@linuxchix.org>
537 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
538 <mailto:courses-request@linuxchix.org?subject=subscribe>
539 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
540 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
541 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
542 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
546 LinuxChix Perl Course Part 1: Getting Started
550 1) Shebang/Hashbang line
551 2) Documentation and help
553 4) Where to find out more...
555 -----------------------------------
557 1) Shebang/Hashbang line
559 # (aka "octothorp", or "sharp" or "pound sign" or "hash")
560 normally begins a comment. The comment is from the #
561 to the end of the line.
563 #i.e. This would be a single-line comment.
565 <code goes here>#And so is this.
576 There is one exception to this comment assignment and that is
577 when the comment is followed immediately by a ! ( aka
578 "exclamation mark", or "bang"). The combination #! is known as
579 the "she-bang" (a contraction of sharp-bang)or "hash-bang" and it
580 is the top line of the script in both Perl and shell scripts.
581 It tells the interpreter where to find the program that should
582 be used for interpreting this script.
584 To find out what your she-bang line should read, you need to go to
585 the command line and type "whence perl" (korn shell), "type
586 perl" or "which perl" (bash).
588 Typing "type perl" into my command line gives me
590 perl is /usr/bin/perl
592 Then create your she-bang line accordingly:
596 #!/usr/bin/local/perl
598 This means that I need to use #!/usr/bin/perl as my top line.
600 Notice that, unlike other lines of Perl code, this one does NOT
603 -----------------------------------------
605 2) Documentation and help
607 There are various methods of getting help with Perl, two of them are
608 supplied with Perl on your machine. The first is the series of 'man' page=
610 Typing "man perl" on the command line or in a terminal with give you a 30=
613 introduction page with help on how to get to various types of information
614 The second is the 'perldoc' series which can be accessed by typing=20
615 "perldoc perldoc". Perldoc is aimed at helping with functions and modules=
617 To start the 'man' series is probably more useful, and can be helpful rea=
619 However, this is not for the faint-hearted as they are extremely long.
621 Updated versions and other documentation can be found at
622 http://www.perldoc.com
624 Note: Some Linux installations come with tkman, this is a browser style
625 version of the man pages.
627 -----------------------------------------
631 1) Try "type perl" in your command line to establish what your top line
634 2) Use your answer to the above to try the traditional Hello World progra=
636 Type the following in using any text editor. When you come to save the pr=
638 you will need to save it with .pl as the extension.
645 print "Hello, world!\n";
647 To run your program type
649 perl <programname.pl>
651 3) Use the man pages to find out what Perl stands for.
654 -----------------------------------------
656 4) Where to find out more...
658 An archive of lessons is being created at
660 http://www.schiffwood.demon.co.uk/index.html
662 Here there are links to useful websites and other books.
664 Part 2 of the course will follow on 15th March 2003
665 -----------------------------------------
667 Note: The author uses Perl version 5.6.1. All exercises should therefore=20
668 work on later versions. If you wish to find out which version of Perl you=
670 running type "perl -v" into your command line.
672 If you have any comments regarding this perl lesson, suggestions for futu=
674 topics, useful website addresses, useful reference material or you just w=
676 to say what you thought, please send your email to=20
678 alice at schiffwood.demon.co.uk
680 I will be pleased to hear from you.
682 Credits and acknowledgements
684 Thanks to Meryll Larkin and Jacinta Richardson for their help with puttin=
686 together this material.=20
687 Sources of material include Learning Perl, Randal Schwartx & Tom Phoenix,
688 & Perl Training Australia (http://www.perltraining.com.au)
690 From rachel@xtreme.com Sat Mar 15 09:20:33 2003
691 Return-Path: <rachel@xtreme.com>
692 Delivered-To: courses@linuxchix.org
693 Received: from localhost (localhost [127.0.0.1])
694 by www.linuxchix.org (Postfix) with ESMTP id 9873D272D3B
695 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:20:33 +1100 (EST)
696 Received: from euphoria.xtreme.com (euphoria.xtreme.com [64.124.80.50])
697 by www.linuxchix.org (Postfix) with SMTP id 57FDB272D1B
698 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:20:01 +1100 (EST)
699 Received: (qmail 22762 invoked from network); 14 Mar 2003 22:09:21 -0000
700 Received: from localhost (HELO qbert) (127.0.0.1)
701 by localhost with SMTP; 14 Mar 2003 22:09:21 -0000
702 From: "Rachel McConnell" <rachel@xtreme.com>
703 To: <courses@linuxchix.org>
704 Subject: RE: [Courses] Perl course part 1
705 Date: Fri, 14 Mar 2003 14:18:35 -0800
706 Message-ID: <001601c2ea77$a7fa4600$1201a8c0@qbert>
708 Content-Type: text/plain;
710 Content-Transfer-Encoding: 7bit
711 X-Priority: 3 (Normal)
712 X-MSMail-Priority: Normal
713 X-Mailer: Microsoft Outlook, Build 10.0.2616
714 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
715 In-Reply-To: <200303011126.26229.alice@schiffwood.demon.co.uk>
717 X-Virus-Scanned: by AMaViS new-20020517
718 X-BeenThere: courses@linuxchix.org
719 X-Mailman-Version: 2.1
721 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
722 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
723 List-Post: <mailto:courses@linuxchix.org>
724 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
725 <mailto:courses-request@linuxchix.org?subject=subscribe>
726 List-Archive: <http://linuxchix.org/pipermail/courses>
727 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
728 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
729 X-List-Received-Date: Fri, 14 Mar 2003 22:20:34 -0000
733 Hello Perl Course Teacher and Fellow Students,
735 I have done my part 1 coursework just in time for part 2, and I'm very
736 excited about moving forward. I have a perl script that prints, not
737 merely Hello World, but "Hello world from my badass self!" - a much
738 superior phrasing, as you will all agree. I won't post it unless asked,
739 I'm sure everyone else has one just like it.
741 However I couldn't find what P E R L stood for on the man pages - There
742 are a LOT of man pages... however I found, in perlfaq1:
744 "never write 'PERL', because perl isn't really an acronym, apocryphal
745 folklore and post-facto expansions notwithstanding."
747 So i cheated & googled it. the post-facto expansion i found was,
748 Practical Expression and Reporting Language. anyone find something
751 Teacher, I ran into discussions of CPAN which appears to be something
752 like rpm's, only for just perl... will you discuss CPAN at some point
757 PS - I haven't taken a course on this list before (and haven't checked
758 the archives, Bad Me!) so am not sure if it's Entirely Appropriate to
759 post this message, but what the hell!
761 From sarah@ph34rbob.org Sat Mar 15 09:37:52 2003
762 Return-Path: <sarah@ph34rbob.org>
763 Delivered-To: courses@linuxchix.org
764 Received: from localhost (localhost [127.0.0.1])
765 by www.linuxchix.org (Postfix) with ESMTP id 9C206272D2B
766 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:37:52 +1100 (EST)
767 Received: from smtp805.mail.sc5.yahoo.com (smtp805.mail.sc5.yahoo.com
769 by www.linuxchix.org (Postfix) with SMTP id 59003272CD8
770 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:37:51 +1100 (EST)
771 Received: from adsl-67-122-213-237.dsl.snfc21.pacbell.net (HELO
772 ?192.168.2.102?) (953teresita@sbcglobal.net@67.122.213.237 with plain)
773 by smtp-sbc-v1.mail.vip.sc5.yahoo.com with SMTP;
774 14 Mar 2003 22:37:31 -0000
775 Subject: RE: [Courses] [Perl] course part 1
776 From: Sarah Huffman <sarah@ph34rbob.org>
777 To: courses@linuxchix.org
778 In-Reply-To: <001601c2ea77$a7fa4600$1201a8c0@qbert>
779 References: <001601c2ea77$a7fa4600$1201a8c0@qbert>
780 Content-Type: text/plain
781 Content-Transfer-Encoding: 7bit
782 X-Mailer: Ximian Evolution 1.0.8 (1.0.8-10)
783 Date: 14 Mar 2003 14:35:36 -0800
784 Message-Id: <1047681336.2079.14.camel@walrus>
786 X-Virus-Scanned: by AMaViS new-20020517
787 X-Topics: Perl programming course
788 X-BeenThere: courses@linuxchix.org
789 X-Mailman-Version: 2.1
791 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
792 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
793 List-Post: <mailto:courses@linuxchix.org>
794 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
795 <mailto:courses-request@linuxchix.org?subject=subscribe>
796 List-Archive: <http://linuxchix.org/pipermail/courses>
797 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
798 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
799 X-List-Received-Date: Fri, 14 Mar 2003 22:37:53 -0000
803 On Fri, 2003-03-14 at 14:18, Rachel McConnell wrote:
805 > However I couldn't find what P E R L stood for on the man pages - There
806 > are a LOT of man pages... however I found, in perlfaq1:
808 Larry Wall has something to say about it, which can be found here-
809 http://www.linuxjournal.com/article.php?sid=3394.
814 From alice@schiffwood.demon.co.uk Sat Mar 1 22:47:45 2003
815 Return-Path: <alice@schiffwood.demon.co.uk>
816 Delivered-To: courses@linuxchix.org
817 Received: from localhost (localhost [127.0.0.1])
818 by www.linuxchix.org (Postfix) with ESMTP id 7BD7A273187
819 for <courses@linuxchix.org>; Sat, 1 Mar 2003 22:47:45 +1100 (EST)
820 Received: from anchor-post-39.mail.demon.net (anchor-post-39.mail.demon.net [194.217.242.80])
821 by www.linuxchix.org (Postfix) with ESMTP id 08408273184
822 for <courses@linuxchix.org>; Sat, 1 Mar 2003 22:47:43 +1100 (EST)
823 Received: from schiffwood.demon.co.uk ([193.237.181.189] helo=arnie)
824 by anchor-post-39.mail.demon.net with esmtp (Exim 3.36 #2)
826 for courses@linuxchix.org; Sat, 01 Mar 2003 11:47:40 +0000
827 Content-Type: text/plain;
829 From: Alice Wood <alice@schiffwood.demon.co.uk>
830 Reply-To: alice@schiffwood.demon.co.uk
831 To: courses@linuxchix.org
832 Date: Sat, 1 Mar 2003 11:42:09 +0000
833 User-Agent: KMail/1.4.1
835 Content-Transfer-Encoding: quoted-printable
836 Message-Id: <200303011142.09390.alice@schiffwood.demon.co.uk>
837 X-Virus-Scanned: by AMaViS new-20020517
838 Subject: [Courses] Perl course overview
839 Sender: courses-admin@linuxchix.org
840 Errors-To: courses-admin@linuxchix.org
841 X-BeenThere: courses@linuxchix.org
842 X-Mailman-Version: 2.0.1
844 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
845 List-Post: <mailto:courses@linuxchix.org>
846 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
847 <mailto:courses-request@linuxchix.org?subject=subscribe>
848 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
849 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
850 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
851 List-Archive: <http://mailman.linuxchix.org/pipermail/courses/>
857 Welcome to the Perl course. You should receive two mails today, this one =
859 introduction and a second containing part one of the course. I am writing=
861 this to give you an idea of how the course will (hopefully) plan out and=
863 what I hope to do in future installments.
865 The course will roughly follow the Learning Perl book, and I will refer t=
867 that where appropriate in the mails I send. The first few lessons are pla=
872 2) Scalar data and chomp
873 3) Control structures including if and while
875 5) Arrays: reverse and sort
877 Given the amount of material we have to cover this may extend.
879 I am publishing the lessons on http://www.schiffwood.demon.co.uk as they =
881 put onto the mailing list and there should be a mirror on linuxchix soon.=
883 website also includes helpful websites and a list of useful books is also=
885 developing, if you have any suggestions then please put them forward.
887 Please feel free to let me know your thoughts on the course. As the exerc=
889 develop I hope to include different versions of answers in the lessons, s=
891 you come up with something different then send it in. I look forward to=20
892 getting to know everyone during the course.
900 Project Gutenberg newsletter editor
902 From sarah@ph34rbob.org Sat Mar 15 09:37:52 2003
903 Return-Path: <sarah@ph34rbob.org>
904 Delivered-To: courses@linuxchix.org
905 Received: from localhost (localhost [127.0.0.1])
906 by www.linuxchix.org (Postfix) with ESMTP id 9C206272D2B
907 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:37:52 +1100 (EST)
908 Received: from smtp805.mail.sc5.yahoo.com (smtp805.mail.sc5.yahoo.com
910 by www.linuxchix.org (Postfix) with SMTP id 59003272CD8
911 for <courses@linuxchix.org>; Sat, 15 Mar 2003 09:37:51 +1100 (EST)
912 Received: from adsl-67-122-213-237.dsl.snfc21.pacbell.net (HELO
913 ?192.168.2.102?) (953teresita@sbcglobal.net@67.122.213.237 with plain)
914 by smtp-sbc-v1.mail.vip.sc5.yahoo.com with SMTP;
915 14 Mar 2003 22:37:31 -0000
916 Subject: RE: [Courses] [Perl] course part 1
917 From: Sarah Huffman <sarah@ph34rbob.org>
918 To: courses@linuxchix.org
919 In-Reply-To: <001601c2ea77$a7fa4600$1201a8c0@qbert>
920 References: <001601c2ea77$a7fa4600$1201a8c0@qbert>
921 Content-Type: text/plain
922 Content-Transfer-Encoding: 7bit
923 X-Mailer: Ximian Evolution 1.0.8 (1.0.8-10)
924 Date: 14 Mar 2003 14:35:36 -0800
925 Message-Id: <1047681336.2079.14.camel@walrus>
927 X-Virus-Scanned: by AMaViS new-20020517
928 X-Topics: Perl programming course
929 X-BeenThere: courses@linuxchix.org
930 X-Mailman-Version: 2.1
932 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
933 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
934 List-Post: <mailto:courses@linuxchix.org>
935 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
936 <mailto:courses-request@linuxchix.org?subject=subscribe>
937 List-Archive: <http://linuxchix.org/pipermail/courses>
938 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
939 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
940 X-List-Received-Date: Fri, 14 Mar 2003 22:37:53 -0000
944 On Fri, 2003-03-14 at 14:18, Rachel McConnell wrote:
946 > However I couldn't find what P E R L stood for on the man pages - There
947 > are a LOT of man pages... however I found, in perlfaq1:
949 Larry Wall has something to say about it, which can be found here-
950 http://www.linuxjournal.com/article.php?sid=3394.
955 From alice@schiffwood.demon.co.uk Sat Mar 15 23:04:09 2003
956 Return-Path: <alice@schiffwood.demon.co.uk>
957 Delivered-To: courses@linuxchix.org
958 Received: from localhost (localhost [127.0.0.1])
959 by www.linuxchix.org (Postfix) with ESMTP id 367FB272D28
960 for <courses@linuxchix.org>; Sat, 15 Mar 2003 23:04:09 +1100 (EST)
961 Received: from anchor-post-39.mail.demon.net (anchor-post-39.mail.demon.net
963 by www.linuxchix.org (Postfix) with ESMTP id 246A6272CD3
964 for <courses@linuxchix.org>; Sat, 15 Mar 2003 23:04:03 +1100 (EST)
965 Received: from schiffwood.demon.co.uk ([193.237.181.189] helo=arnie)
966 by anchor-post-39.mail.demon.net with esmtp (Exim 3.36 #2)
968 for courses@linuxchix.org; Sat, 15 Mar 2003 12:04:00 +0000
969 Content-Type: text/plain;
971 From: Alice Wood <alice@schiffwood.demon.co.uk>
972 To: courses@linuxchix.org
973 Date: Sat, 15 Mar 2003 12:02:50 +0000
974 User-Agent: KMail/1.4.1
976 Content-Transfer-Encoding: quoted-printable
977 Message-Id: <200303151202.51066.alice@schiffwood.demon.co.uk>
978 X-Virus-Scanned: by AMaViS new-20020517
979 Subject: [Courses] LinuxChix Perl Course Part 2
980 X-BeenThere: courses@linuxchix.org
981 X-Mailman-Version: 2.1
983 Reply-To: alice@schiffwood.demon.co.uk
984 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
985 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
986 List-Post: <mailto:courses@linuxchix.org>
987 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
988 <mailto:courses-request@linuxchix.org?subject=subscribe>
989 List-Archive: <http://linuxchix.org/pipermail/courses>
990 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
991 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
992 X-List-Received-Date: Sat, 15 Mar 2003 12:04:09 -0000
996 LinuxChix Perl Course Part 2: Scalar Data
1000 1) Good programming guide
1003 4) Where to find out more...
1005 -----------------------------------
1007 1) Good Programming Guide
1014 As beginners it is always a good idea to try and start off with good habi=
1016 that as we become more experienced, we are regularly building into a prog=
1018 pointers to help us when we have a problem. Good programs will be=20
1019 appropriately commented and have lots of white space (gaps between sectio=
1023 It is also possible to use tools that are built into Perl to help you spo=
1025 potential problems. The first is an addition to the top line.
1029 The -w switch allows Perl to print warnings if the variables in programs =
1031 _probably_ not being used correctly, if there is a function infinitely de=
1033 in recursion or when it spots something suspicious in the program. There =
1035 many switches available for use in Perl. further information about these =
1037 be found on the Perlrun manpage.
1041 It is useful to put this in as the second line to a program (see below).=20
1046 This will enforce good programming rules throughout your program. For exa=
1056 Your mistake will be caught automatically.
1058 ----------------------------------
1062 There are four basic data types that Perl uses.
1064 scalars - A single chunk of data
1065 arrays - A bunch of scalars, indexed by a number
1066 hashes - A bunch of scalars, indexed by another scalar known as a key
1067 handles - A pointer enabling the opening of resources from the operating=20
1068 system (files, directories, etc.)
1070 A scalar is denoted by $. It may be used to represent integers, strings,=20
1071 floats, any block of information. The syntax for creating a scalar is
1073 $variable =3D <value>;
1075 Where the value may be numeric, string, reference or boolean.
1076 There is no size restriction on a scalar, so you may make your scalar as =
1078 as necessary. Perl is similar to shell scripting when assigning values to=
1082 Below are some examples of scalars
1084 $scalar1 =3D 'this is a string';=09# simple scalar assigned 'this is a st=
1086 $value =3D 10.0;=09=09=09# simple scalar assigned 10 (number is rounded)
1087 $value2 =3D '10.0'=09=09# number that acts as a string, number is not rou=
1089 $scalar2 =3D "this is $value";=09# interpolation, assigned 'this is 10.0'
1090 $scalar3 =3D 'this is $value';=09# non-interpolation, assigned 'this is $=
1092 $empty =3D '';=09=09=09# empty string
1094 Note the use of quotes here. The single quote (') indicates that the text=
1096 be used verbatim and the double quote (") indicates the text will be=20
1098 It is possible to operate on scalars.
1101 $suzy =3D $luigi + 4;
1103 $suzy =3D $suzy - 3; # alternatively, $suzy -=3D 3;
1105 The scalars may also be operated on as strings.
1108 $suzy =3D $suzy x 3; # this will print suzysuzysuzy
1110 $suzy =3D $suzy . 'space'; # this will append 'space' to the string held =
1114 ---------------------------------
1118 a) Create a program 'newline.pl' containing the following:
1120 print"Hi there.\nThis is my second program.\n"
1122 Run this and then replace the \n with a space or an Enter and compare the=
1126 What is the purpose of \n?
1128 b) Write a program called 'average.pl' to calculate the average of the=20
1133 (Remember an average is where you add the numbers together and divide by =
1135 amount you have, this is learning perl not maths 101! Although, the maths=
1137 normally the hardest part.)
1139 c) Modify the program called 'repetition.pl' to work more efficiently.=20
1140 (Remember there's more than one way to do it!)
1151 ----------------------------------
1153 Answers to previous exercises
1155 1) You should have found that your top line was something like as stated =
1157 part 1 of the lesson.
1159 2) 'Hello World' should have appeared on your screen.
1161 3) Practical Extraction and Report Language, or Pathologically Eclectic=20
1162 Rubbish Lister depending on your point of view.
1164 -----------------------------------
1166 4) Where to find out more...
1168 An archive of lessons is being created at
1170 http://www.schiffwood.demon.co.uk/index.html
1172 Also, there are links to useful websites and other books.
1174 The archive should appear on the LinuxChix website soon.
1176 Part 3 of the course will follow on 29th March 2003
1178 -----------------------------------------
1180 Note: The author uses Perl version 5.6.1. All exercises should therefore=20
1181 work on later versions. If you wish to find out which version of Perl you=
1183 running type "perl -v" into your command line.
1185 If you have any comments regarding this perl lesson, suggestions for futu=
1187 topics, useful website addresses, useful reference material or you just w=
1189 to say what you thought, please send your email to=20
1191 alice at schiffwood.demon.co.uk
1193 I will be pleased to hear from you.
1195 Credits and acknowledgements
1197 Thanks to Meryll Larkin and Jacinta Richardson for their help with puttin=
1199 together this material.=20
1200 Sources of material include Learning Perl, Randal Schwartx & Tom Phoenix,
1201 Perl Training Australia (http://www.perltraining.com.au),
1202 Perl 5 Developer's Guide, Peschko & DeWolfe
1204 Suzy is the name of my cat.
1209 Project Gutenberg newsletter editor
1210 From morgon@surgo.net Sun Mar 16 03:39:25 2003
1211 Return-Path: <morgon@surgo.net>
1212 Delivered-To: courses@linuxchix.org
1213 Received: from localhost (localhost [127.0.0.1])
1214 by www.linuxchix.org (Postfix) with ESMTP id A8E54272D1B
1215 for <courses@linuxchix.org>; Sun, 16 Mar 2003 03:39:25 +1100 (EST)
1216 Received: from Daedalus (pcp01415442pcs.potshe01.pa.comcast.net
1218 by www.linuxchix.org (Postfix) with ESMTP id 8D515272CD1
1219 for <courses@linuxchix.org>; Sun, 16 Mar 2003 03:39:24 +1100 (EST)
1220 Received: from [68.81.44.211] (helo=surgo ident=venthorn)
1221 by Daedalus with smtp (Exim 3.36 #1 (Debian))
1223 for <courses@linuxchix.org>; Sat, 15 Mar 2003 10:36:37 -0500
1224 Date: Sat, 15 Mar 2003 11:35:39 -0500
1225 From: Morgon Kanter <morgon@surgo.net>
1226 To: courses@linuxchix.org
1227 Message-Id: <20030315113539.340648aa.morgon@surgo.net>
1228 X-Mailer: Sylpheed version 0.7.4 (GTK+ 1.2.10; i386-debian-linux-gnu)
1230 Content-Type: text/plain; charset=US-ASCII
1231 Content-Transfer-Encoding: 7bit
1232 X-Virus-Scanned: by AMaViS new-20020517
1233 X-Topics: Perl programming course
1234 Subject: [Courses] [Perl] A bit of clarification on what Alice said about
1236 X-BeenThere: courses@linuxchix.org
1237 X-Mailman-Version: 2.1
1239 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1240 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1241 List-Post: <mailto:courses@linuxchix.org>
1242 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1243 <mailto:courses-request@linuxchix.org?subject=subscribe>
1244 List-Archive: <http://linuxchix.org/pipermail/courses>
1245 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1246 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1247 X-List-Received-Date: Sat, 15 Mar 2003 16:39:26 -0000
1248 Content-Length: 1514
1251 > Note the use of quotes here. The single quote (') indicates that the text will
1252 > be used verbatim and the double quote (") indicates the text will be
1255 This would be a bit confusing for me, so let me clarify.
1257 A string enclosed ' ' means it is literally what you put it. Any character
1258 other than a single quote or a backslash-single-quote (\') stands for itself
1259 inside of the string.
1261 $var = ' ' # A string with one space
1262 $var = '\'' # A string with a single quote inside of it
1263 $var = 'yo\n' # A string with yo followed by a \n
1265 st' # A string that reads te NEWLINE st
1267 Now on the other hand, in a double-quoted string, you can use escape characters
1268 etc like in C and other languages. Here is a brief list.
1273 \f # Form feed (AKA page breaks on some printers)
1275 \a # Rings the bell!
1276 \e # Escape (think of the escape key on your keyboard)
1279 \' # Single quote in a single-quoted string
1280 \l # Lowercase next letter
1281 \L # Lowercase all the following letters until a \E
1282 \u # Uppercase next letter
1283 \U # Same as \L but uppercases
1284 \Q # Quote non-word characters by adding a \ until a \E is read
1285 \E # Terminates \L, \U and \Q
1286 \xyz # Any character's octal value
1287 \xFF # Any character's hex value
1288 \cC # A so-called "control" character, in this example ^C (ctrl-C)
1291 $var = "yo" # The string reads yo
1292 $var = "yo\n" # The string reads yo NEWLINE
1294 I hope that cleared some things up for any that were confused
1297 From morgon@surgo.net Sun Mar 16 03:39:23 2003
1298 Return-Path: <morgon@surgo.net>
1299 Delivered-To: courses@linuxchix.org
1300 Received: from localhost (localhost [127.0.0.1])
1301 by www.linuxchix.org (Postfix) with ESMTP id B70E9272D1B
1302 for <courses@linuxchix.org>; Sun, 16 Mar 2003 03:39:23 +1100 (EST)
1303 Received: from Daedalus (pcp01415442pcs.potshe01.pa.comcast.net
1305 by www.linuxchix.org (Postfix) with ESMTP id B4870272CD1
1306 for <courses@linuxchix.org>; Sun, 16 Mar 2003 03:39:22 +1100 (EST)
1307 Received: from [68.81.44.211] (helo=surgo ident=venthorn)
1308 by Daedalus with smtp (Exim 3.36 #1 (Debian))
1310 for <courses@linuxchix.org>; Sat, 15 Mar 2003 10:36:38 -0500
1311 Date: Sat, 15 Mar 2003 11:38:39 -0500
1312 From: Morgon Kanter <morgon@surgo.net>
1313 To: courses@linuxchix.org
1314 Message-Id: <20030315113839.5fb5deb7.morgon@surgo.net>
1315 X-Mailer: Sylpheed version 0.7.4 (GTK+ 1.2.10; i386-debian-linux-gnu)
1317 Content-Type: text/plain; charset=US-ASCII
1318 Content-Transfer-Encoding: 7bit
1319 X-Virus-Scanned: by AMaViS new-20020517
1320 X-Topics: Perl programming course
1321 Subject: [Courses] [Perl] exercise answers...
1322 X-BeenThere: courses@linuxchix.org
1323 X-Mailman-Version: 2.1
1325 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1326 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1327 List-Post: <mailto:courses@linuxchix.org>
1328 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1329 <mailto:courses-request@linuxchix.org?subject=subscribe>
1330 List-Archive: <http://linuxchix.org/pipermail/courses>
1331 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1332 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1333 X-List-Received-Date: Sat, 15 Mar 2003 16:39:24 -0000
1337 1) As I said in my previous post, its to put a newline on the screen
1340 $var = 23 + 28 + 31 + 17 + 1;
1349 From laurel@sdf.lonestar.org Tue Mar 18 05:05:32 2003
1350 Return-Path: <laurel@sdf.lonestar.org>
1351 Delivered-To: courses@linuxchix.org
1352 Received: from localhost (localhost [127.0.0.1])
1353 by www.linuxchix.org (Postfix) with ESMTP id 0AC47272CDC
1354 for <courses@linuxchix.org>; Tue, 18 Mar 2003 05:05:31 +1100 (EST)
1355 Received: from sdf.lonestar.org (otaku.freeshell.org [216.162.208.193])
1356 by www.linuxchix.org (Postfix) with ESMTP id 37738272CDB
1357 for <courses@linuxchix.org>; Tue, 18 Mar 2003 05:05:13 +1100 (EST)
1358 Received: (from laurel@localhost)
1359 by sdf.lonestar.org (8.12.8/8.12.8) id h2HI4Kiq002738
1360 for courses@linuxchix.org; Mon, 17 Mar 2003 18:04:20 GMT
1361 Date: Mon, 17 Mar 2003 10:04:20 -0800
1362 From: Laurel Fan <laurel@sdf.lonestar.org>
1363 To: courses@linuxchix.org
1364 Subject: Re: [Courses] [Perl] exercise answers...
1365 Message-ID: <20030317180420.GA17197@sdf.lonestar.org>
1366 Mail-Followup-To: courses@linuxchix.org
1367 References: <20030315113839.5fb5deb7.morgon@surgo.net>
1369 Content-Type: text/plain; charset=us-ascii
1370 Content-Disposition: inline
1371 In-Reply-To: <20030315113839.5fb5deb7.morgon@surgo.net>
1372 User-Agent: Mutt/1.4i
1373 X-Virus-Scanned: by AMaViS new-20020517
1374 X-Topics: Perl programming course
1375 X-BeenThere: courses@linuxchix.org
1376 X-Mailman-Version: 2.1
1378 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1379 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1380 List-Post: <mailto:courses@linuxchix.org>
1381 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1382 <mailto:courses-request@linuxchix.org?subject=subscribe>
1383 List-Archive: <http://linuxchix.org/pipermail/courses>
1384 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1385 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1386 X-List-Received-Date: Mon, 17 Mar 2003 18:05:32 -0000
1390 On the subject of 'verbatim' and "interpreted" strings:
1392 On Sat, Mar 15, 2003 at 11:38:39AM -0500, Morgon Kanter wrote:
1394 > $var = 23 + 28 + 31 + 17 + 1;
1396 > print $var . "\n";
1398 Here you could also do:
1402 because $variables within "double quote strings" are also interpreted.
1405 laurel@sdf.lonestar.org
1406 http://dreadnought.gorgorg.org
1407 From alice@schiffwood.demon.co.uk Sat Mar 29 07:52:01 2003
1408 Return-Path: <alice@schiffwood.demon.co.uk>
1409 Delivered-To: courses@linuxchix.org
1410 Received: from localhost (localhost [127.0.0.1])
1411 by www.linuxchix.org (Postfix) with ESMTP id AEA74272CB2
1412 for <courses@linuxchix.org>; Sat, 29 Mar 2003 07:52:01 +1100 (EST)
1413 Received: from mta06-svc.ntlworld.com (mta06-svc.ntlworld.com [62.253.162.46])
1414 by www.linuxchix.org (Postfix) with ESMTP id AFAF3272C8E
1415 for <courses@linuxchix.org>; Sat, 29 Mar 2003 07:51:59 +1100 (EST)
1416 Received: from arnie.home.net ([80.4.18.223]) by mta06-svc.ntlworld.com
1417 (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP
1418 id <20030328205156.WFFQ20605.mta06-svc.ntlworld.com@arnie.home.net>
1419 for <courses@linuxchix.org>; Fri, 28 Mar 2003 20:51:56 +0000
1420 Content-Type: text/plain;
1421 charset="iso-8859-1"
1422 From: Alice Wood <alice@schiffwood.demon.co.uk>
1423 To: courses@linuxchix.org
1424 Date: Fri, 28 Mar 2003 20:52:32 +0000
1425 User-Agent: KMail/1.4.3
1427 Content-Transfer-Encoding: quoted-printable
1428 Message-Id: <200303281934.19420.alice@schiffwood.co.uk>
1429 X-Virus-Scanned: by AMaViS new-20020517
1430 X-Topics: Perl programming course
1431 Subject: [Courses] [Perl] Important Information
1432 X-BeenThere: courses@linuxchix.org
1433 X-Mailman-Version: 2.1
1435 Reply-To: alice@schiffwood.demon.co.uk
1436 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1437 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1438 List-Post: <mailto:courses@linuxchix.org>
1439 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1440 <mailto:courses-request@linuxchix.org?subject=subscribe>
1441 List-Archive: <http://linuxchix.org/pipermail/courses>
1442 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1443 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1444 X-List-Received-Date: Fri, 28 Mar 2003 20:52:02 -0000
1450 Due to a small family emergency I am nowhere near my usual computer at th=
1452 moment and so will be unable to post this weeks lesson / episode. I will =
1454 the lesson as soon as I am able to do so, and will aim to be back on trac=
1464 Project Gutenberg newsletter editor
1465 From alice@schiffwood.co.uk Sat Mar 29 06:33:56 2003
1466 Return-Path: <alice@schiffwood.co.uk>
1467 Delivered-To: courses@linuxchix.org
1468 Received: from localhost (localhost [127.0.0.1])
1469 by www.linuxchix.org (Postfix) with ESMTP id 07CA8272CB3
1470 for <courses@linuxchix.org>; Sat, 29 Mar 2003 06:33:55 +1100 (EST)
1471 Received: from mta06-svc.ntlworld.com (mta06-svc.ntlworld.com [62.253.162.46])
1472 by www.linuxchix.org (Postfix) with ESMTP id 25D15272C8D
1473 for <courses@linuxchix.org>; Sat, 29 Mar 2003 06:33:54 +1100 (EST)
1474 Received: from arnie.home.net ([80.4.18.223]) by mta06-svc.ntlworld.com
1475 (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP
1476 id <20030328193343.QLQS20605.mta06-svc.ntlworld.com@arnie.home.net>
1477 for <courses@linuxchix.org>; Fri, 28 Mar 2003 19:33:43 +0000
1478 Content-Type: text/plain;
1480 From: Alice Wood <alice@schiffwood.co.uk>
1481 To: courses@linuxchix.org
1482 Date: Fri, 28 Mar 2003 19:34:19 +0000
1483 User-Agent: KMail/1.4.3
1485 Content-Transfer-Encoding: quoted-printable
1486 Message-Id: <200303281934.19420.alice@schiffwood.co.uk>
1487 X-Virus-Scanned: by AMaViS new-20020517
1488 X-Mailman-Approved-At: Mon, 31 Mar 2003 11:03:52 +1000
1489 X-Topics: Perl programming course
1490 Subject: [Courses] [Perl] Important Information
1491 X-BeenThere: courses@linuxchix.org
1492 X-Mailman-Version: 2.1
1494 Reply-To: alice@schiffwood.co.uk
1495 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1496 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1497 List-Post: <mailto:courses@linuxchix.org>
1498 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1499 <mailto:courses-request@linuxchix.org?subject=subscribe>
1500 List-Archive: <http://linuxchix.org/pipermail/courses>
1501 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1502 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1503 X-List-Received-Date: Fri, 28 Mar 2003 19:33:56 -0000
1509 Due to a small family emergency I am nowhere near my usual computer at th=
1511 moment and so will be unable to post this weeks lesson / episode. I will =
1513 the lesson as soon as I am able to do so, and will aim to be back on trac=
1523 Project Gutenberg newsletter editor
1524 From suzo@bigpond.net.au Sun Mar 30 18:55:23 2003
1525 Return-Path: <suzo@bigpond.net.au>
1526 Delivered-To: courses@linuxchix.org
1527 Received: from localhost (localhost [127.0.0.1])
1528 by www.linuxchix.org (Postfix) with ESMTP id F0755272CF7
1529 for <courses@linuxchix.org>; Sun, 30 Mar 2003 18:55:22 +1000 (EST)
1530 Received: from mta07ps.bigpond.com (mta07ps.bigpond.com [144.135.25.132])
1531 by www.linuxchix.org (Postfix) with ESMTP id 09080272CE0
1532 for <courses@linuxchix.org>; Sun, 30 Mar 2003 18:55:22 +1000 (EST)
1533 Received: from phoenix ([144.135.25.87]) by mta07ps.bigpond.com
1534 (Netscape Messaging Server 4.15 mta07ps Jul 16 2002 22:47:55)
1535 with SMTP id HCK0S800.EZC; Sun, 30 Mar 2003 18:55:20 +1000
1536 Received: from CPE-144-137-82-141.nsw.bigpond.net.au ([144.137.82.141]) by
1537 psmam07bpa.bigpond.com(MailRouter V3.2g 125/3300945); 30 Mar 2003 18:55:20
1538 Content-Type: text/plain;
1539 charset="iso-8859-1"
1540 From: Sue Stones <suzo@bigpond.net.au>
1541 Organization: Vyette's House
1542 To: alice@schiffwood.demon.co.uk, courses@linuxchix.org
1543 Subject: Re: [Courses] [Perl] Important Information
1544 Date: Sun, 30 Mar 2003 18:56:33 +1000
1545 User-Agent: KMail/1.4.3
1546 References: <200303281934.19420.alice@schiffwood.co.uk>
1547 In-Reply-To: <200303281934.19420.alice@schiffwood.co.uk>
1549 Content-Transfer-Encoding: 8bit
1550 Message-Id: <200303301856.33280.suzo@bigpond.net.au>
1551 X-Virus-Scanned: by AMaViS new-20020517
1552 X-Topics: Perl programming course
1553 X-BeenThere: courses@linuxchix.org
1554 X-Mailman-Version: 2.1
1556 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1557 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1558 List-Post: <mailto:courses@linuxchix.org>
1559 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1560 <mailto:courses-request@linuxchix.org?subject=subscribe>
1561 List-Archive: <http://linuxchix.org/pipermail/courses>
1562 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1563 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1564 X-List-Received-Date: Sun, 30 Mar 2003 08:55:23 -0000
1572 On Sat, 29 Mar 2003 07:52 am, Alice Wood wrote:
1575 > Due to a small family emergency I am nowhere near my usual computer at the
1576 > moment and so will be unable to post this weeks lesson / episode. I will
1577 > post the lesson as soon as I am able to do so, and will aim to be back on
1584 From jscalf@pegasus.rutgers.edu Mon Mar 31 23:57:33 2003
1585 Return-Path: <jscalf@pegasus.rutgers.edu>
1586 Delivered-To: courses@linuxchix.org
1587 Received: from localhost (localhost [127.0.0.1])
1588 by www.linuxchix.org (Postfix) with ESMTP id 8D235272D02
1589 for <courses@linuxchix.org>; Mon, 31 Mar 2003 23:57:33 +1000 (EST)
1590 Received: from ajax.rutgers.edu (ajax.rutgers.edu [128.6.10.9])
1591 by www.linuxchix.org (Postfix) with ESMTP id B44C7272C96
1592 for <courses@linuxchix.org>; Mon, 31 Mar 2003 23:57:32 +1000 (EST)
1593 Received: from pegasus.rutgers.edu (new.pegasus-back.rutgers.edu
1594 [192.168.0.42]) by ajax.rutgers.edu (Postfix) with ESMTP
1595 id EA68110A5C; Mon, 31 Mar 2003 08:57:22 -0500 (EST)
1596 Received: by pegasus.rutgers.edu (Postfix, from userid 88966)
1597 id 0DE7B7222F; Mon, 31 Mar 2003 08:58:22 -0500 (EST)
1598 Date: Mon, 31 Mar 2003 08:58:21 -0500 (EST)
1599 From: Jennifer Scalf <jscalf@pegasus.rutgers.edu>
1600 To: Alice Wood <alice@schiffwood.co.uk>
1601 Subject: Re: [Courses] [Perl] Important Information
1602 In-Reply-To: <200303281934.19420.alice@schiffwood.co.uk>
1603 Message-ID: <Pine.SOL.4.44.0303310857270.7280-100000@pegasus.rutgers.edu>
1605 Content-Type: TEXT/PLAIN; charset=US-ASCII
1606 X-Virus-Scanned: by AMaViS
1607 X-Virus-Scanned: by AMaViS new-20020517
1608 X-Topics: Perl programming course
1609 cc: courses@linuxchix.org
1610 X-BeenThere: courses@linuxchix.org
1611 X-Mailman-Version: 2.1
1613 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1614 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1615 List-Post: <mailto:courses@linuxchix.org>
1616 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1617 <mailto:courses-request@linuxchix.org?subject=subscribe>
1618 List-Archive: <http://linuxchix.org/pipermail/courses>
1619 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1620 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1621 X-List-Received-Date: Mon, 31 Mar 2003 13:57:33 -0000
1626 It's ok, just take care of what you need to we'll still be here :) I hope
1627 everything is ok soon.
1631 On Fri, 28 Mar 2003, Alice Wood wrote:
1635 > Due to a small family emergency I am nowhere near my usual computer at the
1636 > moment and so will be unable to post this weeks lesson / episode. I will post
1637 > the lesson as soon as I am able to do so, and will aim to be back on track
1646 > Project Gutenberg newsletter editor
1647 > _______________________________________________
1648 > Courses mailing list
1649 > Courses@linuxchix.org
1650 > http://mailman.linuxchix.org/mailman/listinfo/courses
1653 From alice@schiffwood.co.uk Sat Apr 12 23:53:46 2003
1654 Return-Path: <alice@schiffwood.co.uk>
1655 Delivered-To: courses@linuxchix.org
1656 Received: from localhost (localhost [127.0.0.1])
1657 by www.linuxchix.org (Postfix) with ESMTP id AD5B6272CD7
1658 for <courses@linuxchix.org>; Sat, 12 Apr 2003 23:53:45 +1000 (EST)
1659 Received: from mta02-svc.ntlworld.com (mta02-svc.ntlworld.com [62.253.162.42])
1660 by www.linuxchix.org (Postfix) with ESMTP id DF5F2272CB4
1661 for <courses@linuxchix.org>; Sat, 12 Apr 2003 23:53:40 +1000 (EST)
1662 Received: from fast.home.net ([80.4.18.223]) by mta02-svc.ntlworld.com
1663 (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP
1664 id <20030412135332.RSZY9882.mta02-svc.ntlworld.com@fast.home.net>
1665 for <courses@linuxchix.org>; Sat, 12 Apr 2003 14:53:32 +0100
1666 Content-Type: text/plain;
1668 From: Alice Wood <alice@schiffwood.co.uk>
1669 To: courses@linuxchix.org
1670 Date: Sun, 13 Apr 2003 02:53:52 +0100
1671 User-Agent: KMail/1.4.3
1673 Content-Transfer-Encoding: quoted-printable
1674 Message-Id: <200304130253.52971.alice@schiffwood.co.uk>
1675 X-Virus-Scanned: by AMaViS new-20020517
1676 X-Topics: Perl programming course
1677 Subject: [Courses] [Perl] Perl course part 3
1678 X-BeenThere: courses@linuxchix.org
1679 X-Mailman-Version: 2.1
1681 Reply-To: alice@schiffwood.co.uk
1682 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1683 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1684 List-Post: <mailto:courses@linuxchix.org>
1685 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1686 <mailto:courses-request@linuxchix.org?subject=subscribe>
1687 List-Archive: <http://linuxchix.org/pipermail/courses>
1688 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1689 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1690 X-List-Received-Date: Sat, 12 Apr 2003 13:53:46 -0000
1691 Content-Length: 5029
1694 LinuxChix Perl Course Part 3: User Input, chomp and chop.
1701 4) Where to find out more...
1703 -----------------------------------------
1705 1) User input from the keyboard:
1707 The line input operator <>, (also known as the diamond operator) allows t=
1709 program to read data (or even
1710 futher code) from file handles. In particular the special filehandle
1711 STDIN can be used to read input from the keyboard. For example:
1715 Each time we use this construct the next complete line of text is read in=
1717 The definition of a "complete line of text" depends on the value of $/
1718 which is newline by default. Read the INPUT_RECORD_SEPARATOR
1719 section of "perldoc perlvar" for more information about $/.
1723 Type the following into a file called mycat.pl
1728 my $line =3D <STDIN>;
1729 print "You wrote: $line";
1732 and execute it from the command line:
1736 You'll notice that the program "hangs" rather that printing and then
1737 returning. This means it is waiting for some user input. Type
1738 something and then press enter and see what happens.
1741 Let me add some input
1742 You wrote: Let me add some input
1745 -----------------------------------------
1749 This is a command that is worth learning early as it is so useful
1750 later on. Chomp can be performed on both scalar variables (ones starting
1751 with $ signs) and lists. When used, chomp removes any trailing string
1752 that corresponds to the current value of $/ (also known as the
1753 INPUT_RECORD_SEPARATOR which you can read more about in "perldoc
1754 perlvar"). Usually $/ is set to newline, but we can change it if we
1757 Chomp is often used to remove the trailing charadcter from input you've
1758 just read in. This is best demonstrated with an example:
1760 Type the following into a file called mycat.pl
1765 my $line =3D <STDIN>;
1766 print "You wrote: [$line]\n";
1769 and execute it from the command line, you should get something like:
1772 Let me add some input
1773 You wrote: [Let me add some input
1778 As you can see when we read in the line, we read in the newline as well. =
1780 can remove this newline by using chomp:
1782 Change your program so that it reads as follows:
1787 my $line =3D <STDIN>;
1789 print "You wrote: [$line]\n";
1792 and execute it from the command line, this time you should get:
1795 Let me add some input
1796 You wrote: [Let me add some input]
1800 "chomp" is also called the "safe chop"
1802 chop will remove any single final character.
1803 chomp will remove only a new line character (i.e. the enter from
1806 Also, this is the common usage of chomp when it is combined with
1807 accepting the input in a single line of code:
1809 chomp(my $input =3D <STDIN>);
1812 -----------------------------------------
1815 a) Try adding the following line to your program, before my $line =3D <ST=
1821 What happens now? Try entering a line without a comma in it followed by =
1823 with a comma in it but not as the last character. Can you explain what i=
1827 b) Write a currency conversion program to calculate the exchange value of=
1830 local currency (or if you are in the UK, use the Euro exchange
1831 rate). your program should ask for the exchange rate.
1833 Answers: All missing answers will be sorted out next time and also
1834 published via the website.
1836 -----------------------------------------
1838 4) Where to find out more...
1840 An archive of lessons is being created at
1842 http://www.schiffwood.demon.co.uk/index.html
1844 Here there are links to useful websites and other books.
1846 Part 4 of the course will follow on 26th April 2003
1847 -----------------------------------------
1849 Note: The author uses Perl version 5.6.1. All exercises should therefore=20
1850 work on later versions. If you wish to find out which version of Perl you=
1852 running type "perl -v" into your command line.
1854 If you have any comments regarding this Perl lesson, suggestions for futu=
1856 topics, useful website addresses, useful reference material or you just w=
1858 to say what you thought, please send your email to=20
1860 alice at schiffwood.co.uk
1862 I will be pleased to hear from you.
1864 Credits and acknowledgements
1866 Sources of material include:
1867 Learning Perl, Randal Schwartx & Tom Phoenix,
1868 Perl Training Australia (http://www.perltraining.com.au)
1869 Perl 5 Developer's guide, Peschko & DeWolfe
1870 http://learn.perl.org
1871 With very great thanks to Jacinta Richardson and Meryll Larkin for the
1872 course help, and Trish Fraser for the webpages and consumer testing.
1875 Project Gutenberg newsletter editor
1876 From morgon@surgo.net Sun Apr 13 12:14:30 2003
1877 Return-Path: <morgon@surgo.net>
1878 Delivered-To: courses@linuxchix.org
1879 Received: from localhost (localhost [127.0.0.1])
1880 by www.linuxchix.org (Postfix) with ESMTP id 6D4E4272D0C
1881 for <courses@linuxchix.org>; Sun, 13 Apr 2003 12:14:30 +1000 (EST)
1882 Received: from Daedalus (pcp03276362pcs.potshe01.pa.comcast.net
1884 by www.linuxchix.org (Postfix) with ESMTP id 810B5272CE0
1885 for <courses@linuxchix.org>; Sun, 13 Apr 2003 12:14:29 +1000 (EST)
1886 Received: from [68.81.44.211] (helo=surgo ident=venthorn)
1887 by Daedalus with smtp (Exim 3.36 #1 (Debian))
1889 for <courses@linuxchix.org>; Sat, 12 Apr 2003 21:11:37 -0400
1890 Date: Sat, 12 Apr 2003 22:13:42 -0400
1891 From: Morgon Kanter <morgon@surgo.net>
1892 To: courses@linuxchix.org
1893 Subject: Re: [Courses] [Perl] Perl course part 3
1894 Message-Id: <20030412221342.6c8a8a3f.morgon@surgo.net>
1895 In-Reply-To: <200304130253.52971.alice@schiffwood.co.uk>
1896 References: <200304130253.52971.alice@schiffwood.co.uk>
1897 X-Mailer: Sylpheed version 0.7.4 (GTK+ 1.2.10; i386-debian-linux-gnu)
1899 Content-Type: text/plain; charset=US-ASCII
1900 Content-Transfer-Encoding: 7bit
1901 X-Virus-Scanned: by AMaViS new-20020517
1902 X-Topics: Perl programming course
1903 X-BeenThere: courses@linuxchix.org
1904 X-Mailman-Version: 2.1
1906 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1907 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1908 List-Post: <mailto:courses@linuxchix.org>
1909 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1910 <mailto:courses-request@linuxchix.org?subject=subscribe>
1911 List-Archive: <http://linuxchix.org/pipermail/courses>
1912 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1913 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1914 X-List-Received-Date: Sun, 13 Apr 2003 02:14:30 -0000
1918 > a) Try adding the following line to your program, before my $line = <STDIN=>;
1922 I suppose you mean $/ = ","; because above won't work without the
1926 Morgon Kanter <morgon@surgo.net> http://www.surgo.net
1927 GPG key ID: 297CEA5B
1928 Please don't CC me on mailing lists, I read them!
1929 From jarich@perltraining.com.au Sun Apr 13 15:59:38 2003
1930 Return-Path: <jarich@perltraining.com.au>
1931 Delivered-To: courses@linuxchix.org
1932 Received: from localhost (localhost [127.0.0.1])
1933 by www.linuxchix.org (Postfix) with ESMTP id 7942F272D0F
1934 for <courses@linuxchix.org>; Sun, 13 Apr 2003 15:59:38 +1000 (EST)
1935 Received: from teddybear.perltraining.com.au (teddybear.perltraining.com.au
1937 by www.linuxchix.org (Postfix) with ESMTP id 892E9272D0B
1938 for <courses@linuxchix.org>; Sun, 13 Apr 2003 15:59:37 +1000 (EST)
1939 Received: by teddybear.perltraining.com.au (Postfix, from userid 1001)
1940 id 988AA73922; Sun, 13 Apr 2003 15:59:36 +1000 (EST)
1941 Received: from localhost (localhost [127.0.0.1])
1942 by teddybear.perltraining.com.au (Postfix) with SMTP
1943 id 6AC46679C5; Sun, 13 Apr 2003 15:59:36 +1000 (EST)
1944 Date: Sun, 13 Apr 2003 15:59:36 +1000 (EST)
1945 From: Jacinta Richardson <jarich@perltraining.com.au>
1946 To: Alice Wood <alice@schiffwood.co.uk>
1947 Subject: Re: [Courses] [Perl] Perl course part 3 (some corrections)
1948 In-Reply-To: <200304130253.52971.alice@schiffwood.co.uk>
1949 Message-ID: <Pine.LNX.3.96.1030413155139.14695A-100000@teddybear.perltraining.com.au>
1951 Content-Type: TEXT/PLAIN; charset=US-ASCII
1952 X-Virus-Scanned: by AMaViS new-20020517
1953 X-Topics: Perl programming course
1954 cc: courses@linuxchix.org
1955 X-BeenThere: courses@linuxchix.org
1956 X-Mailman-Version: 2.1
1958 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
1959 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
1960 List-Post: <mailto:courses@linuxchix.org>
1961 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1962 <mailto:courses-request@linuxchix.org?subject=subscribe>
1963 List-Archive: <http://linuxchix.org/pipermail/courses>
1964 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
1965 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
1966 X-List-Received-Date: Sun, 13 Apr 2003 05:59:38 -0000
1967 Content-Length: 1510
1971 On Sun, 13 Apr 2003, Alice Wood wrote:
1973 A very good part of a Perl course, but in need of a minor correction or
1976 > LinuxChix Perl Course Part 3: User Input, chomp and chop.
1980 > "chomp" is also called the "safe chop"
1982 > chop will remove any single final character.
1983 > chomp will remove only a new line character (i.e. the enter from
1986 Chomp will remove any string specified in $/. Hence if $/ were equal to
1990 $fred = "let's say we have a string like this";
1994 would print "let's say we have a string like ";
1996 The key is, that if $/ is set to "\n" as it is by default then calling
1997 chomp on our string would ONLY remove the trailing newline, if it
1998 existed. If there is not trailing newline, then chomp will remove
1999 nothing. Chomp is not restricited to only removing a single character,
2000 neither is $/ restricted to being set to a single character.
2006 > a) Try adding the following line to your program, before my $line = <STDIN=
2011 This most certainly should be:
2014 note the missing = character.
2024 ("`-''-/").___..--''"`-._ | Jacinta Richardson |
2025 `6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia |
2026 (_Y_.)' ._ ) `._ `. ``-..-' | +613 9354 6001 |
2027 _..`--'_..-_/ /--'_.' ,' | contact@perltraining.com.au |
2028 (il),-'' (li),' ((!.-' | www.perltraining.com.au |
2030 From alice@schiffwood.co.uk Tue Apr 29 04:46:51 2003
2031 Return-Path: <alice@schiffwood.co.uk>
2032 Delivered-To: courses@linuxchix.org
2033 Received: from localhost (localhost [127.0.0.1])
2034 by www.linuxchix.org (Postfix) with ESMTP id F034D272CF3
2035 for <courses@linuxchix.org>; Tue, 29 Apr 2003 04:46:50 +1000 (EST)
2036 Received: from mta01-svc.ntlworld.com (mta01-svc.ntlworld.com [62.253.162.41])
2037 by www.linuxchix.org (Postfix) with ESMTP id 754A1272CF2
2038 for <courses@linuxchix.org>; Tue, 29 Apr 2003 04:46:48 +1000 (EST)
2039 Received: from fast.home.net ([80.4.18.223]) by mta01-svc.ntlworld.com
2040 (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP
2041 id <20030428184638.DKLN2283.mta01-svc.ntlworld.com@fast.home.net>
2042 for <courses@linuxchix.org>; Mon, 28 Apr 2003 19:46:38 +0100
2043 Content-Type: text/plain;
2045 From: Alice Wood <alice@schiffwood.co.uk>
2046 To: courses@linuxchix.org
2047 Date: Mon, 28 Apr 2003 21:46:55 +0100
2048 User-Agent: KMail/1.4.3
2050 Content-Transfer-Encoding: quoted-printable
2051 Message-Id: <200304282146.55657.alice@schiffwood.co.uk>
2052 X-Virus-Scanned: by AMaViS new-20020517
2053 X-Topics: Perl programming course
2054 Subject: [Courses] [Perl] Perl course Part 4
2055 X-BeenThere: courses@linuxchix.org
2056 X-Mailman-Version: 2.1
2058 Reply-To: alice@schiffwood.co.uk
2059 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
2060 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
2061 List-Post: <mailto:courses@linuxchix.org>
2062 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2063 <mailto:courses-request@linuxchix.org?subject=subscribe>
2064 List-Archive: <http://linuxchix.org/pipermail/courses>
2065 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2066 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
2067 X-List-Received-Date: Mon, 28 Apr 2003 18:46:51 -0000
2068 Content-Length: 4314
2071 LinuxChix Perl Course Part 4: Control structures - If, else, while.
2078 4) Where to find out more...
2080 -----------------------------------------
2084 Conditional statements in Perl tell your program which way to proceed
2087 One of the most useful (along with the for loop) is the if statement.
2089 The basic setup for an if statement is
2091 if (conditional statement 1) { #Note the curly bracket here
2093 =09=09=09 <Action code>
2099 =09=09print "You are overdrawn!\n";
2103 The code can be left in this manner, if the condition is not met then
2104 the program will simply ignore that particular block of code.
2107 An alternative to this could be.
2109 if (conditional statement 1) {
2111 =09=09=09 <Action code 1>
2113 } if (conditional statement 2){
2117 } if (conditional statement 3){
2124 An alternative block can be added to the code as follows
2126 if (conditional statement 1){
2128 =09=09=09=09<Action code 1>
2136 =09=09 print "You are overdrawn!"\n;
2138 print "You are in credit."\n;
2142 # There is an elsif command which may also be used in this situation,
2143 we will meet that later.
2145 -----------------------------------------
2149 A while loop will repeat a block of code when a condition is true.
2151 while (conditional statement){
2153 =09=09=09=09Block of code
2157 Example #$money must be greater than 0 to begin with for this example to =
2161 =09=09=09print"You are in credit.\n";
2162 =09=09=09$money--; # -- is the autodecrement
2165 #If we are testing for something negative, than the unless condition
2166 may be more helpful, we will meet this later.
2169 The importance of the use brackets to denote blocks of code cannot be
2170 over emphasised here.=20
2172 In C this is acceptable:
2175 if condition do_this();
2178 in Perl, you must do:
2180 if condition {do_this();}
2183 The only change to this would be where the block is a single line, where =
2187 do_this() if $condition;
2189 -----------------------------------------
2193 Write a program that will ask the user to guess a number. The number
2194 will be predefined at the beginning of your program.
2203 #Rest of your program
2205 If you get into trouble please feel free to mail me for hints.
2206 ---------------------------
2208 Answers to previous exercises
2212 a) The purpose of \n is to force a newline. (The game is given away by
2215 b) Here is a version of average.pl
2220 my $sum =3D 23+28+17+31+1;
2222 my $average =3D $sum/5;
2224 print "The average of the five numbers is: $average\n";
2226 # Note: This is not the most efficient program that could be written,
2227 however I have tried to show the steps involved.
2229 c) This is a version of repetition.pl
2236 All futher answers will be published via the website by the first
2239 -----------------------------------------
2241 4) Where to find out more...
2243 An archive of lessons is being created at
2245 http://www.tyneside.pm.org/courseindex.html
2247 Here there are links to useful websites and other books.
2249 Part 5 of the course will follow on 10th May 2003
2250 -----------------------------------------
2252 Note: The author uses Perl version 5.6.1. All exercises should therefore=20
2253 work on later versions. If you wish to find out which version of Perl you=
2255 running type "perl -v" into your command line.
2257 If you have any comments regarding this Perl lesson, suggestions for futu=
2259 topics, useful website addresses, useful reference material or you just w=
2261 to say what you thought, please send your email to=20
2263 alice at schiffwood dot co dot uk
2265 I will be pleased to hear from you.
2267 Credits and acknowledgements
2269 Sources of material include:
2270 Learning Perl, Randal Schwartx & Tom Phoenix,
2271 Perl Training Australia (http://www.perltraining.com.au)
2272 Perl 5 Developer's guide, Peschko & DeWolfe
2273 http://learn.perl.org
2274 With very great thanks to Jacinta Richardson and Meryll Larkin for the
2275 course help, and Trish Fraser for the webpages and consumer testing.
2278 Project Gutenberg newsletter editor
2279 From morgon@surgo.net Tue Apr 29 05:45:41 2003
2280 Return-Path: <morgon@surgo.net>
2281 Delivered-To: courses@linuxchix.org
2282 Received: from localhost (localhost [127.0.0.1])
2283 by www.linuxchix.org (Postfix) with ESMTP id CEBDD272D37
2284 for <courses@linuxchix.org>; Tue, 29 Apr 2003 05:45:40 +1000 (EST)
2285 Received: from Daedalus (pcp03276362pcs.potshe01.pa.comcast.net
2287 by www.linuxchix.org (Postfix) with ESMTP id BA0CA272D26
2288 for <courses@linuxchix.org>; Tue, 29 Apr 2003 05:45:19 +1000 (EST)
2289 Received: from [68.81.44.211] (helo=surgo ident=venthorn)
2290 by Daedalus with smtp (Exim 3.36 #1 (Debian))
2292 for <courses@linuxchix.org>; Mon, 28 Apr 2003 14:40:17 -0400
2293 Date: Mon, 28 Apr 2003 15:42:42 -0400
2294 From: Morgon Kanter <morgon@surgo.net>
2295 To: courses@linuxchix.org
2296 Subject: Re: [Courses] [Perl] Perl course Part 4
2297 Message-Id: <20030428154242.362d7f51.morgon@surgo.net>
2298 In-Reply-To: <200304282146.55657.alice@schiffwood.co.uk>
2299 References: <200304282146.55657.alice@schiffwood.co.uk>
2300 X-Mailer: Sylpheed version 0.7.4claws (GTK+ 1.2.10; i386-debian-linux-gnu)
2302 Content-Type: text/plain; charset=US-ASCII
2303 Content-Transfer-Encoding: 7bit
2304 X-Virus-Scanned: by AMaViS new-20020517
2305 X-Topics: Perl programming course
2306 X-BeenThere: courses@linuxchix.org
2307 X-Mailman-Version: 2.1
2309 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
2310 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
2311 List-Post: <mailto:courses@linuxchix.org>
2312 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2313 <mailto:courses-request@linuxchix.org?subject=subscribe>
2314 List-Archive: <http://linuxchix.org/pipermail/courses>
2315 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2316 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
2317 X-List-Received-Date: Mon, 28 Apr 2003 19:45:41 -0000
2321 This one time, at band camp, Alice Wood <alice@schiffwood.co.uk> wrote:
2324 > Write a program that will ask the user to guess a number. The number
2325 > will be predefined at the beginning of your program.
2334 > #Rest of your program
2336 Here's the answer (don't look down if you don't want to be spoiled)
2337 --------------- begin number.pl ----------------
2343 print "Enter your guess: ";
2344 my $guess = <STDIN>;
2345 if($guess == $number) {
2349 print "Wrong, try again :(\n";
2352 ------------- end number.pl ---------------
2353 venthorn@surgo:~$ perl number.pl
2358 Enter your guess: 17
2364 You said homosexuals form a small percentage of the population. So
2365 do Jews. Is that a reason to deny someone equality?
2367 From daniel.richter@wimba.com Fri Jul 25 20:08:50 2003
2368 Return-Path: <daniel.richter@wimba.com>
2369 Delivered-To: courses@linuxchix.org
2370 Received: from localhost (localhost [127.0.0.1])
2371 by www.linuxchix.org (Postfix) with ESMTP id 6908D272DFD
2372 for <courses@linuxchix.org>; Fri, 25 Jul 2003 20:08:50 +1000 (EST)
2373 Received: from sodium.wimba.fr (unknown [81.80.215.129])
2374 by www.linuxchix.org (Postfix) with ESMTP id E4FE8272DEE
2375 for <courses@linuxchix.org>; Fri, 25 Jul 2003 20:07:56 +1000 (EST)
2376 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
2377 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h6P9Fwk12955
2378 for <courses@linuxchix.org>; Fri, 25 Jul 2003 11:15:58 +0200
2379 Message-Id: <5.1.0.14.1.20030725121416.030035e8@mail.wimba.com>
2380 X-Sender: daniel@mail.wimba.com
2381 X-Mailer: QUALCOMM Windows Eudora Version 5.1
2382 Date: Fri, 25 Jul 2003 12:22:37 +0200
2383 To: courses@linuxchix.org
2384 From: Dan Richter <daniel.richter@wimba.com>
2385 Subject: [Courses] [Perl] Part 4.5
2387 Content-Type: text/plain; charset="us-ascii"; format=flowed
2388 X-Virus-Scanned: by AMaViS new-20020517
2389 X-Topics: Perl programming course
2390 X-BeenThere: courses@linuxchix.org
2391 X-Mailman-Version: 2.1
2393 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
2394 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
2395 List-Post: <mailto:courses@linuxchix.org>
2396 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2397 <mailto:courses-request@linuxchix.org?subject=subscribe>
2398 List-Archive: <http://linuxchix.org/pipermail/courses>
2399 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2400 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
2401 X-List-Received-Date: Fri, 25 Jul 2003 10:08:50 -0000
2402 Content-Length: 8276
2405 LinuxChix Perl Course Part 4.5: Review
2407 Since this course has encountered a three-month interuption, I thought we
2408 should start back up with a refresher lesson. Refer back to the original
2409 lessons if you get lost, or ask for help from the list.
2411 Note that there's a little new material at the end.
2413 -----------------------------------
2420 4) Control structures
2424 -----------------------------------
2426 1) Getting Started Complete Lesson:
2427 http://linuxchix.org/pipermail/courses/2003-March/001147.html
2429 Your Perl files should start with a "shebang" line. It should look something
2434 The /usr/bin/perl is the location of the Perl interpreter (you should set it
2435 to wherever Perl is on your system). The "-w" switch means "warn me if I do
2436 something that looks like a mistake."
2438 Although this technically comes from lesson two, I thought I would mention
2439 here that the second line of your Perl programs should always be "use
2440 strict;". This has a function similar to the "-w" switch, but "use strict;"
2441 and "-w" cover different mistakes, so you should use them both.
2443 Here is a Hello World program:
2448 print "Hello, world!\n";
2450 The "\n" at the end causes a newline to be printed, because "print" doesn't
2451 automatically output a newline.
2453 -----------------------------------
2455 2) Scalar Data Complete Lesson:
2456 http://linuxchix.org/pipermail/courses/2003-March/001153.html
2458 Now we're going to look at "scalar" variables. Scalar is the simplest type of
2459 variable: it always refers to a single "thing", such as a number or a piece of
2460 text (which can be multiple words). We'll see a more complicated type of
2461 variable when we deal with arrays.
2463 Scalar variables are preceded by a dollar sign, like this:
2467 The word "my" declares the variable. In other words, it says, "make this
2468 variable magically come into existance". You should declare a variable before
2469 using it. You can declare a variable and give it a value at the same time,
2472 my $foo = 'some text'; # Set $foo to be the words "some text".
2474 Here we use strings, but we can also use numbers:
2477 my $seconds = $minutes * 60;
2478 print $seconds; # Output is 720.
2480 Perl includes several operators on strings (text), including "."
2481 (concatenation) and "x" (multiplication):
2484 $text = $text . 'xyz'; # Now $text = 'abcxyz'
2485 my $longtext = $text x 3;
2486 # Now $longtext = 'abcxyzabcxyzabcxyz'
2488 Don't use a plus sign to concatenate strings! The plus sign means numerical
2491 Single and double quotes have different meanings. Inside of double quotes,
2492 variables (e.g., beginning with "$") and special characters (beginning with
2493 "\") are interpreted (replaced with appropriate values). Inside of single
2494 quotes, variables and "\" are not interpolated. (Actually, "\" is interpolated
2495 ONLY if followed by a single quote or another "\".)
2498 my $foo = "Eat $value\n"; # Result is: Eat fish [newline]
2499 my $bar = 'Eat $value\n'; # Result is: Eat $value\n
2501 -----------------------------------
2503 3) User Input Complete Lesson:
2504 http://linuxchix.org/pipermail/courses/2003-April/001170.html
2506 The "diamond operator", or "<>", is placed around a filehandle to read a line
2507 from it. You can define your own filehandles, but the special filehandle STDIN
2508 is always defined for you:
2514 print "You wrote: [$line]\n";
2516 Note that the input includes the newline character at the end of the line. Use
2517 "chomp" to remove it:
2524 print "You wrote: [$line]\n";
2526 You can't write "$line = chomp(<STDIN>)" because "chomp" alters the value
2527 passed in rather than returning the changed input. If you want to be really
2530 chomp(my $input = <STDIN>);
2532 The record separator ("$/") controls how input is broken into readable pieces,
2533 and "chomp" always removes $/ from the end of the line. You can set it as
2538 Now "<STDIN>" will read text up to (and including) the first comma, and
2539 "chomp" will cut that comma off the end of the input.
2541 -----------------------------------
2543 4) Control Structures Complete Lesson:
2544 http://linuxchix.org/pipermail/courses/2003-April/001184.html
2546 Like pretty much every other (imperitive) programming language, Perl uses the
2547 "if" control structure.
2549 if ( <condition> ) {
2550 # Do something here.
2555 if ( <condition> ) {
2556 # Do something here.
2559 # Do something else here.
2562 Perl also has the innovative "unless" keyword. "unless ( <condition> )" has
2563 the same meaning as "if ( not <condition> )".
2565 unless ( $number == 5 ) {
2566 print "The number is not 5.\n";
2569 You can also add an "else" after "unless", but only a truly twisted soul would
2572 The "while" keyword causes commands to be executed as long as a condition is
2580 while ( $x <= 10 ) {
2585 In contrast to "while", "until" loops if the condition is false:
2590 # Count to ten (again).
2597 Both "if" and "unless" can be written after the statement, too:
2599 print "Your account is overdrawn.\n" if $money < 0;
2601 The same is true of "while" and "until".
2603 All of these keywords ("if", "unless", "while" and "until") require braces and
2604 parentheses unless used after the statement.
2606 -----------------------------------
2610 I'd like to point out a few things that haven't been mentioned out before.
2612 Perl allows you to make the same type of "if-assignment" as C:
2614 if ( $x = 10 ) { ... } # Uh oh...
2616 Do you see the error? It should be "==" instead of "=":
2618 if ( $x == 10 ) { ... } # Much better.
2620 If you have faithfully turned on warnings, Perl will warn you of that one, but
2623 if ( $x = $y ) { ... } # Possible mistake, but no warning.
2625 Perl doesn't warn you because assignment may in fact be what you want,
2626 especially in a loop:
2632 while ( defined($line = <STDIN>) ) {
2637 The while loop terminates when there's no more input.
2639 (If you execute this program, enter a few lines and then hit Ctrl+D [hold down
2640 Control and hit D] to indicate that you don't want to input anymore. There's a
2641 chance that Perl will wait for you to hit Ctrl+D before it begins repeating
2644 One other thing that wasn't pointed out before that I think worthy of noting:
2645 even though numbers and text can be stored in the same type of variable, they
2646 use different comparison operators. Operators like "<", ">" and "==" are for
2647 numbers. For text, use:
2651 le (less than or equal to)
2653 ge (greater than or equal to)
2657 my $x = 'Larry Wall';
2658 my $y = 'Alex Nobrain';
2660 print "$x is greater than than $y\n";
2663 If you're wondering why this is, consider this comparison:
2667 if ( $a == $b ) { ... }
2669 Perl is happy to strip the white space off of $a and treat it as a number, but
2670 maybe you intended to treat $b as a string instead. So you have to explicitly
2671 specify whether you want a string comparison or a numerical comparison.
2673 -----------------------------------
2677 This exercise is taken from previous material, but made slightly more
2680 Write a program that reads numbers from the keyboard (one number per line)
2681 until there's no more to read, then outputs the average of all those numbers.
2683 Here are some things to remember:
2684 a) Use "while ( defined($line = <STDIN>) )" to loop until there's no more
2686 b) When entering information, hit Ctrl+D to signal the end of the data.
2687 c) Don't forget "use strict;" and "-w"!
2689 From daniel.richter@wimba.com Thu Jul 31 22:59:33 2003
2690 Return-Path: <daniel.richter@wimba.com>
2691 Delivered-To: courses@linuxchix.org
2692 Received: from localhost (localhost [127.0.0.1])
2693 by www.linuxchix.org (Postfix) with ESMTP id AACF1272D40
2694 for <courses@linuxchix.org>; Thu, 31 Jul 2003 22:59:32 +1000 (EST)
2695 Received: from sodium.wimba.fr (unknown [81.80.215.129])
2696 by www.linuxchix.org (Postfix) with ESMTP id DED8A272D0C
2697 for <courses@linuxchix.org>; Thu, 31 Jul 2003 22:59:29 +1000 (EST)
2698 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
2699 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h6VC8Bk32055
2700 for <courses@linuxchix.org>; Thu, 31 Jul 2003 14:08:11 +0200
2701 Message-Id: <5.1.0.14.1.20030731151058.02b65d68@mail.wimba.com>
2702 X-Sender: daniel@mail.wimba.com
2703 X-Mailer: QUALCOMM Windows Eudora Version 5.1
2704 Date: Thu, 31 Jul 2003 15:15:35 +0200
2705 To: courses@linuxchix.org
2706 From: Dan Richter <daniel.richter@wimba.com>
2707 Subject: [Courses] [Perl] Perl course Part 5: the "tr///" operator
2709 Content-Type: text/plain; charset="us-ascii"; format=flowed
2710 X-Virus-Scanned: by AMaViS new-20020517
2711 X-Topics: Perl programming course
2712 X-BeenThere: courses@linuxchix.org
2713 X-Mailman-Version: 2.1
2715 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
2716 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
2717 List-Post: <mailto:courses@linuxchix.org>
2718 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2719 <mailto:courses-request@linuxchix.org?subject=subscribe>
2720 List-Archive: <http://linuxchix.org/pipermail/courses>
2721 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2722 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
2723 X-List-Received-Date: Thu, 31 Jul 2003 12:59:33 -0000
2724 Content-Length: 6106
2727 LinuxChix Perl Course Part 5: the "tr///" operator
2731 2) The tr/// operator
2733 4) Answers to Previous Exercises
2737 -----------------------------------
2741 It seems that the original plan for this course was to start with features
2742 common to every language, then perhaps get into features unique to Perl.
2743 After thinking long and hard, I have decided that it would be better to do
2744 the opposite: start with features that are unique to Perl. This is surely the
2745 best thing for those of you who already program in other languages, and I
2746 don't think it's detrimental to anyone who is learning Perl as a first
2747 language. (Though learning Perl as a first language might in itself be
2750 So now we're going to look at a Perl language construct that doesn't exist in
2751 most other languages, called "tr". It's generally written with trailing
2752 slashes, for reasons that we will see shortly. Some people call this language
2753 construct a "function," but I believe that "operator" is the correct term,
2754 and it is the term that I will use.
2756 -----------------------------------
2758 2) The tr/// operator
2760 Try running the following Perl program:
2765 my $text = 'some cheese';
2769 What happened to $text?
2771 The "tr///" operator performs a substitution on the individual characters in
2776 $x =~ tr/a/b/; # Replace each "a" with a "b".
2777 $x =~ tr/ /_/; # Convert spaces to underlines.
2778 $x =~ tr/aeiou/AEIOU/; # Capitalise vowels.
2779 $x =~ tr/79/97/; # Exchange "7" and "9".
2781 The only characters that have special meaning to "tr///" are the backslash
2782 and the dash. The latter indicates a range of characters:
2784 $x =~ tr/0-9/QERTYUIOPX/; # Digits to letters.
2785 $x =~ tr/A-Z/a-z/; # Convert to lowercase.
2787 Actually, the slash also has a special meaning to "tr///". The slash is
2788 called the "delimiter", because it indicates the "limit" on the list of
2789 characters to substitute. However, we can use most kinds of punctuation in
2790 place of the slash. For example:
2792 $x =~ tr!aeiou!AEIOU!;
2793 $x =~ tr:aeiou:AEIOU:;
2795 Note that we can also use parentheses, but the syntax changes a little
2796 because parentheses include the idea of containment:
2798 $x =~ tr(aeiou)(AEIOU);
2799 $x =~ tr<aeiou><AEIOU>;
2801 The semantics (meaning) don't change; only the syntax (way of writing it)
2802 changes. But even though the delimiter is abitrary, we still talk about it as
2805 "tr///" returns the number of replacements it made:
2807 my $salary = '$1,000,000.00'; # Dollar sign: use single quote!
2808 my $ego = ($salary =~ tr/0/0/); # Count the zeros in salary.
2810 One more thing: "tr///" has an alias: "y///". This is to please users of the
2811 program "sed", which uses the "y///" command do to basically what "tr///"
2812 does. In Perl, "tr///" and "y///" do exactly the same thing; use whichever
2813 you like. Remember: there is more than one way to do it (TIMTOWTDI).
2815 $text =~ tr/0-9/A-J/; # Convert digits to letters.
2816 $text =~ y/0-9/A-J/; # Does exactly the same thing.
2818 -----------------------------------
2822 "tr///" can take the following options:
2823 c Complement (invert) the searchlist.
2824 d Delete found but unreplaced characters.
2825 s Squash duplicate replaced characters.
2827 These options are specified after the final delimiter, like this:
2829 $x =~ tr/abc/xyz/s; # Note the "s" at the end.
2830 $x =~ tr(abc)(xyz)s; # Same thing, but with parentheses.
2831 $x =~ tr/abc/xy/ds; # Multiple options.
2832 # In the last case, the "z" is missing. You'll see why shortly.
2834 In the last example, we specified both the "d" and the "s" options. The order
2835 of the options isn't important: we could have used "sd" instead of "ds".
2842 ##### The "s" option #####
2843 my $text = 'good cheese';
2844 $text =~ tr/eo/eu/s;
2846 # Output is: gud chese
2848 ##### The "d" option #####
2849 my $big = 'vowels are useful';
2850 $big =~ tr/aeiou/AEI/d;
2852 # The first three vowels are made uppercase.
2853 # The other two, which have no replacement
2854 # character, are deleted because of the "d".
2856 We won't demonstrate the last option, "c", because it's rather complicated.
2857 You can learn more about it using "man perlop" (look under "tr").
2859 -----------------------------------
2861 4) Answers to Previous Exercises
2863 There was only one previous exercise: a program that reads numbers and
2864 outputs the average. Here is one such program:
2872 while ( defined($line = <STDIN>) ) {
2877 my $average = $sum / $n;
2879 print "The average is $sum/$n = $average\n";
2881 -----------------------------------
2885 a) I constantly get spam for "V1agra" or "un1vers1ty dimpl0mas". Write a
2886 program that helps my spam filter by converting all 1's to i's and all 0's to
2889 b) Julius Ceasar is said to have used the famous Ceasar Cypher to encrypt his
2890 communications with Rome. When encrypting, the Ceasar Cypher substitutes
2891 letter-for-letter like this:
2900 Write a program that performs a Ceasar Cypher on its input.
2902 As a test, if you feed the program "Ceasar Cypher", you should get "Egcuct
2903 Earjgt". Be especially careful that the "y" in "cypher" maps to an "a".
2905 -----------------------------------
2909 Part 1: Getting Started
2910 http://linuxchix.org/pipermail/courses/2003-March/001147.html
2913 http://linuxchix.org/pipermail/courses/2003-March/001153.html
2916 http://linuxchix.org/pipermail/courses/2003-April/001170.html
2918 Part 4: Control Structures
2919 http://linuxchix.org/pipermail/courses/2003-April/001184.html
2921 Part 4.5, a review with a little new information at the end:
2922 http://linuxchix.org/pipermail/courses/2003-July/001297.html
2924 From daniel.richter@wimba.com Fri Aug 8 20:33:18 2003
2925 Return-Path: <daniel.richter@wimba.com>
2926 Delivered-To: courses@linuxchix.org
2927 Received: from localhost (localhost [127.0.0.1])
2928 by www.linuxchix.org (Postfix) with ESMTP id 6629A272D74
2929 for <courses@linuxchix.org>; Fri, 8 Aug 2003 20:33:18 +1000 (EST)
2930 Received: from sodium.wimba.fr (unknown [81.80.215.129])
2931 by www.linuxchix.org (Postfix) with ESMTP id 31FA8272D71
2932 for <courses@linuxchix.org>; Fri, 8 Aug 2003 20:33:15 +1000 (EST)
2933 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
2934 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h78AU9k11378
2935 for <courses@linuxchix.org>; Fri, 8 Aug 2003 12:30:10 +0200
2936 Message-Id: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
2937 X-Sender: daniel@mail.wimba.com
2938 X-Mailer: QUALCOMM Windows Eudora Version 5.1
2939 Date: Fri, 08 Aug 2003 12:48:50 +0200
2940 To: courses@linuxchix.org
2941 From: Dan Richter <daniel.richter@wimba.com>
2942 Subject: [Courses] [Perl] Part 6: The "m//" Operator
2944 Content-Type: text/plain; charset="us-ascii"; format=flowed
2945 X-Virus-Scanned: by AMaViS new-20020517
2946 X-Topics: Perl programming course
2947 X-BeenThere: courses@linuxchix.org
2948 X-Mailman-Version: 2.1
2950 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
2951 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
2952 List-Post: <mailto:courses@linuxchix.org>
2953 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2954 <mailto:courses-request@linuxchix.org?subject=subscribe>
2955 List-Archive: <http://linuxchix.org/pipermail/courses>
2956 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
2957 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
2958 X-List-Received-Date: Fri, 08 Aug 2003 10:33:18 -0000
2959 Content-Length: 12569
2962 LinuxChix Perl Course Part 6: The "m//" Operator
2966 2) Introducing the m// Operator
2967 3) A Crash Course on Perl Regular Expressions
2970 6) Answers to Previous Exercises
2971 7) Licensing Announcement
2975 -----------------------------------
2979 Having seen the "tr///" operator, we are now going to look at the "m//"
2980 operator, which stands for "match". Because this requires an understanding of
2981 regular expressions, we give a crash course on that. If you're already
2982 familiar with regular expressions, you should still skim over the crash
2983 course because Perl regular expressions have handy shortcuts that you may not
2986 As you may have noticed, Part 6 is twice as long as Part 5, so you may want
2987 to take a break around the beginning of section 4. Consider yourself lucky:
2988 Part 6 was originally about a third longer, but I finally caved in and moved
2989 some material to Part 7.
2991 -----------------------------------
2993 2) Introducing the m// operator
2995 Try running this program:
3000 my $a = 'I talked to Alice this morning.';
3001 my $b = 'I talked to Bobby this morning.';
3002 print "'$a' matches /Bob/ \n" if $a =~ m/Bob/;
3003 print "'$b' matches /Bob/ \n" if $b =~ m/Bob/;
3007 This example makes it look as though "m//" indicates whether one piece of
3008 text contains another. In fact, "m//" performs a "regular expression" match
3009 (or "pattern match"), which is much, much more powerful. We haven't seen
3010 regular expressions yet, but we're going to look at them in a moment.
3012 By the way, Perl is big on avoiding redundancy. If you use a slash as the
3013 delimiter, you can omit the letter "m" entirely! So the last two statements
3014 of the above program could have been written like this:
3016 print "'$a' matches /Bob/ \n" if $a =~ /Bob/;
3017 print "'$b' matches /Bob/ \n" if $b =~ /Bob/;
3019 In fact, most Perl programmers prefer to write it that way. But remember: it
3020 only works if your delimiter is a slash!
3022 Now we're going to look at regular expressions in more detail. But keep these
3023 four things in mind:
3025 a) "m//" is used to test whether a given string matches or does not match a
3026 certain regular expression.
3028 b) The match does not need to be a separate word: in the previous example,
3029 /Bob/ matched "Bobby".
3031 c) The syntax of "m//" is similar to the syntax of "tr///". So we could have
3032 used "m!Bob!" or "m<Bob>" instead of "m/Bob/".
3034 d) You can omit the "m" if your delimiter is a slash.
3036 -----------------------------------
3038 3) A Crash Course on Perl Regular Expressions
3040 Regular expressions are well-known in Unix circles, though they're not unique
3041 to Unix and they certainly don't require Unix to work. Beware that different
3042 Unix tools (like grep and sed) use different "flavors" of regular
3043 expressions, so the patterns you use in Perl probably aren't directly
3044 portable to other tools.
3046 In Perl regular expressions, the following characters have special meaning:
3047 . * + ? | ^ $ @ ( ) [ ] { } \
3048 and the delimiter (usually slash)
3050 They can all be escaped with a backslash.
3052 Okay, let's start with the easiest special character: the period (dot). The
3053 period matches any single character (except a newline, but we'll see more
3054 about that in Part 7). So /a.c/ matches "abc", "aNc" or "a%c". If you want to
3055 match exactly "a.c", escape the period with a backslash: /a\.c/
3057 Square brackets indicate a list of possible choices:
3058 /a[123]b/ matches exactly three strings: "a1b", "a2b" and "a3b".
3060 A dash inside square brackets indicates a range, just as in "tr///":
3061 /a[1-9]b/ matches "a1b", "a2b", "a3b", ..., "a9b".
3063 To include the dash itself in a list of choices, place the dash at the
3064 beginning or end of the list:
3065 /a[12-]b/ matches "a1b", "a2b" and "a-b".
3067 A dash OUTSIDE square brackets has no special meaning; it's just a literal
3070 A caret at the beginning of a choice negates (inverts) it:
3071 /a[^A-Z]b/ matches "a1b" or "a%b", but NOT "aMb".
3073 A caret anywhere else in a choice means a literal caret:
3074 /a[A^Z]b/ matches "aAb", "a^b" or "aZb".
3076 The characters "* + ?" don't stand for any character, but instead indicate
3077 that the previous character should appear a certain number of times:
3078 * previous character appears zero or more times
3079 + previous character appears one or more times
3080 ? previous character appears zero or one times
3083 /ab*c/ matches "ac", "abc", "abbc" and "abbbbbbbbbc"
3084 /ab+c/ same as /a*b/ except doesn't match "ac"
3085 /ab?c/ matches "ac" and "abc", but NOT "abbc"
3087 Note that if "*" or "+" follows a range or a dot, the match is interpreted
3089 /a[123]*b/ matches "a22b" and "a22222b", but also "a12312322321b"
3090 /a.+b/ matches an "a" followed by a "b", with anything in between
3092 Also note that most special characters lose their special meaning when inside
3093 a range. For example, /[.*]/ matches a literal period or a literal astrisk.
3095 The characters "^" and "$" indicate the beginning and end of the string,
3097 /^a/ an "a" followed by anything (or nothing)
3098 /a$/ anything ending in "a"
3099 /^a$/ matches only "a"
3100 /^a[^z]*b$/ matches anything starting with "a", ending with "b" and not
3102 Remember that unless you explicitly specify a "^" or a "$", the match can
3103 include any part of the string. So, for example, /ab*c/ matches "abbc", but
3106 In addition to representing the end of the string, the character "$" can also
3107 be used for variable interpolation:
3108 my $pattern = '[aeiou]';
3109 print "Found vowels\n" if $str =~ m/$pattern/;
3111 Parentheses are used for grouping:
3112 /(abc)+/ matches "abc" and "abcabc", but NOT "abcc" or "aabbcc"
3113 /a(bc)?d/ matches "ad" and "abcd", but NOT "abd" or "acd".
3115 The vertical bar means "either but not both":
3116 /a|b/ matches "a" and "b".
3117 /ab|cd/ matches "ab" and "cd"
3119 The vertical bar has low precidence, meaning that /AB|CD/ means "AB or CD"
3120 rather than "A, then either B or C, then D". To override this, use
3122 /a(b|c)d/ matches "abd" or "acd" but NOT "abcd"
3124 Finally, Perl includes many useful escape-sequences that only have meaning in
3125 regular expressions, such as:
3126 \s whitespace (same as [ \t\n\r])
3127 \w a "word" character (same as [A-Za-z0-9_])
3128 \d a digit (same as [0-9])
3130 Example: /^\s*\d+\s*$/ means "beginning of string, possible leading
3131 whitespace, one or more digits, possible trailing whitespace, end of string".
3132 In other words, it means a non-negative integer.
3134 Capitalizing each of these means "the opposite":
3136 \W non-word characters
3139 In addition, here is a very handy "zero-length" escape sequence:
3142 \B not a word boundry
3144 They are zero-length because they don't represent a character, just the
3145 boundry between characters. This may not sound important right now, but you
3146 will find it useful with time.
3148 A word boundry is defined as a \w next to a \W (in either order). For
3149 example, /\Bing\b/ matches "playing" and "king", but not "jingle" (doesn't
3150 match \b) or "abc ing" (doesn't match \B).
3152 -----------------------------------
3156 Now that we have seen the details of regular expressions, we can use "m//"
3159 Regular expressions provide a very concise way of describing whether a piece
3160 of text is "what you want". For example:
3162 print "Unix\n" if $os =~ m/Linux|Unix|(Free|Open)?BSD|Solaris/;
3164 Remember: you can omit the "m" if you use a slash as the delimiter:
3166 print "Unix\n" if $os =~ /Linux|Unix|(Free|Open)?BSD|Solaris/;
3168 In Perl, you can surround a part of a regular expression with parentheses so
3169 as to extract the information:
3171 # Match digits-colon-digits-colon-digits
3172 if ( $time =~ /(\d+):(\d+):(\d+)/ ) {
3173 $hour = $1; # First set of parentheses
3174 $minute = $2; # Second set of parentheses
3175 $second = $3; # Third set of parentheses
3178 We can compress the above into one line:
3180 ($hour, $minute, $second) = ( $time =~ /(\d+):(\d+):(\d+)/ );
3182 The parentheses are necessary because "m//" returns the list ($1,$2,$3).
3183 We'll learn more about lists at a later date.
3185 If the parentheses in a regular expression are nested, it's the order of the
3186 LEFT parenthesis that counts:
3188 # Same as the previous example, but takes into account
3189 # the possibility that the seconds are not specified.
3190 if ( $time =~ /(\d+):(\d+)(:(\d+))?/ ) {
3191 $hour = $1; # First set of parentheses
3192 $minute = $2; # Second set of parentheses
3193 $second = $4; # Fourth set of parentheses
3196 In the above example, the third set of parentheses is used to associate both
3197 the colon and the digits with "?" - either both should be specified, or
3198 neither, but not one or the other. We could have set something equal to $3 as
3199 well, but that would be something like ":59", which probably isn't very
3202 By the way, since our regular expression explicitly allows the user to not
3203 specify the number of seconds, we should anticipate the possibility that the
3204 seconds are excluded. We can test this using the "defined" function on "$4"
3205 or "$second", but a quicker way is to do this:
3209 This will set "$second" to zero if $4 is undefined or zero.
3211 If you have enabled warnings (and you should have), Perl will complain if you
3212 use an undefined value for most operations, so be sure to anticipate possible
3213 undefined values in regular expressions.
3215 -----------------------------------
3219 Many URIs[*] (but not all) can be divided into protocol, authority and path
3220 (in that order). For example, consider the following URIs:
3222 ftp://example.com/foo/bar
3223 gopher://abc.example.com/xyz
3224 telnet://example.com/
3226 The protocol is the part before the colon, the authority is the part in the
3227 middle (the domain name), and the path is whatever comes after the authority.
3228 For example, in the first URI, the protocol is "ftp", the authority is
3229 "example.com", and the path is "/foo/bar". Note that the "://" between the
3230 protocol and the authority is not part of the protocol or the authority.
3232 Write a Perl program that uses "m//" to split a URI into protocol, authority
3233 and path, or outputs a message saying that you gave it a URI that it can't
3236 [*] According to RFC 2396, URLs are structural (they tell you how to get what
3237 you want) whereas URNs are logical (a permanent, abstract mapping of strings
3238 to resources), and URIs include both URLs and URNs. The consensus in Perl
3239 seems to be to use the term "URI".
3241 -----------------------------------
3243 6) Answers to Previous Exercises
3245 a) A program to convert all 1's to i's and all 0's to o's:
3250 while ( defined(my $line = <STDIN>) ) {
3261 while ( defined(my $line = <STDIN>) ) {
3262 $line =~ tr/A-Za-z/C-ZABc-zab/;
3266 -----------------------------------
3268 7) Licensing Announcement
3270 This course material, including the previously released parts, is copyright
3271 Alice Wood and Dan Richter, and is retroactively released under the same
3272 license as Perl itself (Artistic License or GPL, your choice). This is the
3273 license of choice to make it easy for other people to integrate your Perl
3274 code/documentation into their own projects. It is not generally used in
3275 projects not related to Perl.
3277 By the way, Alice Wood is the author of the first four parts of this course,
3278 and I got her permission to release those parts under that license as well.
3280 -----------------------------------
3284 Part 1: Getting Started
3285 http://linuxchix.org/pipermail/courses/2003-March/001147.html
3288 http://linuxchix.org/pipermail/courses/2003-March/001153.html
3291 http://linuxchix.org/pipermail/courses/2003-April/001170.html
3293 Part 4: Control Structures
3294 http://linuxchix.org/pipermail/courses/2003-April/001184.html
3296 Part 4.5, a review with a little new information at the end:
3297 http://linuxchix.org/pipermail/courses/2003-July/001297.html
3299 Part 5: The "tr///" Operator:
3300 http://linuxchix.org/pipermail/courses/2003-July/001302.html
3302 -----------------------------------
3306 Works cited: "man perlre" and "man perlop"
3308 Thanks to Jacinta Richardson for fact checking.
3310 From sneha@vit.ac.in Tue Aug 12 19:30:17 2003
3311 Return-Path: <sneha@vit.ac.in>
3312 Delivered-To: courses@linuxchix.org
3313 Received: from localhost (localhost [127.0.0.1])
3314 by www.linuxchix.org (Postfix) with ESMTP id A148F272D7B
3315 for <courses@linuxchix.org>; Tue, 12 Aug 2003 19:30:16 +1000 (EST)
3316 Received: from email.vit.ac.in (unknown [210.212.241.140])
3317 by www.linuxchix.org (Postfix) with ESMTP id 16CA3272CDB
3318 for <courses@linuxchix.org>; Tue, 12 Aug 2003 19:30:09 +1000 (EST)
3319 Received: from HEXBDC ([192.168.20.68]) by email.vit.ac.in with SMTP
3320 (Microsoft Exchange Internet Mail Service Version 5.5.2653.13)
3321 id QX6BPQL3; Tue, 12 Aug 2003 15:09:53 +0530
3322 Message-ID: <003501c36fa2$c6818d30$4414a8c0@vit.ac.in>
3323 From: "sneha" <sneha@vit.ac.in>
3324 To: <courses@linuxchix.org>
3325 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
3326 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
3329 X-MSMail-Priority: Normal
3330 X-Mailer: Microsoft Outlook Express 5.00.2314.1300
3331 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
3332 X-Virus-Scanned: by AMaViS new-20020517
3333 Content-Type: text/plain;
3334 charset="iso-8859-1"
3335 Content-Transfer-Encoding: quoted-printable
3336 X-Content-Filtered-By: Mailman/MimeDel 2.1
3337 X-Topics: Perl programming course
3338 X-BeenThere: courses@linuxchix.org
3339 X-Mailman-Version: 2.1
3341 Reply-To: sneha <sneha@vit.ac.in>
3342 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
3343 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
3344 List-Post: <mailto:courses@linuxchix.org>
3345 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3346 <mailto:courses-request@linuxchix.org?subject=subscribe>
3347 List-Archive: <http://linuxchix.org/pipermail/courses>
3348 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3349 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
3350 Date: Tue, 12 Aug 2003 09:30:17 -0000
3351 X-Original-Date: Sun, 31 Aug 2003 15:02:18 +0530
3352 X-List-Received-Date: Tue, 12 Aug 2003 09:30:17 -0000
3353 Content-Length: 13561
3359 ***********************************************************
3362 my $url =3D 'ftp://example.com/foo/bar';
3366 ($protocol,$authority,$path)=3D($url=3D~m#(\w*)://(.*\.\w*)/(.*)#);
3367 print "Protocol =3D > $protocol\n";
3368 print "Authority =3D > $authority\n";
3369 print "Path =3D > $path\n";
3370 ************************************************************ =
3375 ----- Original Message -----=20
3376 From: Dan Richter=20
3377 To: courses@linuxchix.org=20
3378 Sent: Friday, August 08, 2003 4:18 PM
3379 Subject: [Courses] [Perl] Part 6: The "m//" Operator
3382 LinuxChix Perl Course Part 6: The "m//" Operator
3386 2) Introducing the m// Operator
3387 3) A Crash Course on Perl Regular Expressions
3390 6) Answers to Previous Exercises
3391 7) Licensing Announcement
3395 -----------------------------------
3399 Having seen the "tr///" operator, we are now going to look at the "m//"
3400 operator, which stands for "match". Because this requires an =
3402 regular expressions, we give a crash course on that. If you're already
3403 familiar with regular expressions, you should still skim over the crash
3404 course because Perl regular expressions have handy shortcuts that you =
3408 As you may have noticed, Part 6 is twice as long as Part 5, so you may =
3410 to take a break around the beginning of section 4. Consider yourself =
3412 Part 6 was originally about a third longer, but I finally caved in and =
3414 some material to Part 7.
3416 -----------------------------------
3418 2) Introducing the m// operator
3420 Try running this program:
3425 my $a =3D 'I talked to Alice this morning.';
3426 my $b =3D 'I talked to Bobby this morning.';
3427 print "'$a' matches /Bob/ \n" if $a =3D~ m/Bob/;
3428 print "'$b' matches /Bob/ \n" if $b =3D~ m/Bob/;
3432 This example makes it look as though "m//" indicates whether one piece =
3434 text contains another. In fact, "m//" performs a "regular expression" =
3436 (or "pattern match"), which is much, much more powerful. We haven't seen
3437 regular expressions yet, but we're going to look at them in a moment.
3439 By the way, Perl is big on avoiding redundancy. If you use a slash as =
3441 delimiter, you can omit the letter "m" entirely! So the last two =
3443 of the above program could have been written like this:
3445 print "'$a' matches /Bob/ \n" if $a =3D~ /Bob/;
3446 print "'$b' matches /Bob/ \n" if $b =3D~ /Bob/;
3448 In fact, most Perl programmers prefer to write it that way. But =
3450 only works if your delimiter is a slash!
3452 Now we're going to look at regular expressions in more detail. But keep =
3454 four things in mind:
3456 a) "m//" is used to test whether a given string matches or does not =
3458 certain regular expression.
3460 b) The match does not need to be a separate word: in the previous =
3462 /Bob/ matched "Bobby".
3464 c) The syntax of "m//" is similar to the syntax of "tr///". So we could =
3466 used "m!Bob!" or "m<Bob>" instead of "m/Bob/".
3468 d) You can omit the "m" if your delimiter is a slash.
3470 -----------------------------------
3472 3) A Crash Course on Perl Regular Expressions
3474 Regular expressions are well-known in Unix circles, though they're not =
3476 to Unix and they certainly don't require Unix to work. Beware that =
3478 Unix tools (like grep and sed) use different "flavors" of regular
3479 expressions, so the patterns you use in Perl probably aren't directly
3480 portable to other tools.
3482 In Perl regular expressions, the following characters have special =
3484 . * + ? | ^ $ @ ( ) [ ] { } \
3485 and the delimiter (usually slash)
3487 They can all be escaped with a backslash.
3489 Okay, let's start with the easiest special character: the period (dot). =
3491 period matches any single character (except a newline, but we'll see =
3493 about that in Part 7). So /a.c/ matches "abc", "aNc" or "a%c". If you =
3495 match exactly "a.c", escape the period with a backslash: /a\.c/
3497 Square brackets indicate a list of possible choices:
3498 /a[123]b/ matches exactly three strings: "a1b", "a2b" and "a3b".
3500 A dash inside square brackets indicates a range, just as in "tr///":
3501 /a[1-9]b/ matches "a1b", "a2b", "a3b", ..., "a9b".
3503 To include the dash itself in a list of choices, place the dash at the
3504 beginning or end of the list:
3505 /a[12-]b/ matches "a1b", "a2b" and "a-b".
3507 A dash OUTSIDE square brackets has no special meaning; it's just a =
3511 A caret at the beginning of a choice negates (inverts) it:
3512 /a[^A-Z]b/ matches "a1b" or "a%b", but NOT "aMb".
3514 A caret anywhere else in a choice means a literal caret:
3515 /a[A^Z]b/ matches "aAb", "a^b" or "aZb".
3517 The characters "* + ?" don't stand for any character, but instead =
3519 that the previous character should appear a certain number of times:
3520 * previous character appears zero or more times
3521 + previous character appears one or more times
3522 ? previous character appears zero or one times
3525 /ab*c/ matches "ac", "abc", "abbc" and "abbbbbbbbbc"
3526 /ab+c/ same as /a*b/ except doesn't match "ac"
3527 /ab?c/ matches "ac" and "abc", but NOT "abbc"
3529 Note that if "*" or "+" follows a range or a dot, the match is =
3532 /a[123]*b/ matches "a22b" and "a22222b", but also "a12312322321b"
3533 /a.+b/ matches an "a" followed by a "b", with anything in between
3535 Also note that most special characters lose their special meaning when =
3537 a range. For example, /[.*]/ matches a literal period or a literal =
3540 The characters "^" and "$" indicate the beginning and end of the string,
3542 /^a/ an "a" followed by anything (or nothing)
3543 /a$/ anything ending in "a"
3544 /^a$/ matches only "a"
3545 /^a[^z]*b$/ matches anything starting with "a", ending with "b" and =
3548 Remember that unless you explicitly specify a "^" or a "$", the match =
3550 include any part of the string. So, for example, /ab*c/ matches "abbc", =
3554 In addition to representing the end of the string, the character "$" can =
3556 be used for variable interpolation:
3557 my $pattern =3D '[aeiou]';
3558 print "Found vowels\n" if $str =3D~ m/$pattern/;
3560 Parentheses are used for grouping:
3561 /(abc)+/ matches "abc" and "abcabc", but NOT "abcc" or "aabbcc"
3562 /a(bc)?d/ matches "ad" and "abcd", but NOT "abd" or "acd".
3564 The vertical bar means "either but not both":
3565 /a|b/ matches "a" and "b".
3566 /ab|cd/ matches "ab" and "cd"
3568 The vertical bar has low precidence, meaning that /AB|CD/ means "AB or =
3570 rather than "A, then either B or C, then D". To override this, use
3572 /a(b|c)d/ matches "abd" or "acd" but NOT "abcd"
3574 Finally, Perl includes many useful escape-sequences that only have =
3576 regular expressions, such as:
3577 \s whitespace (same as [ \t\n\r])
3578 \w a "word" character (same as [A-Za-z0-9_])
3579 \d a digit (same as [0-9])
3581 Example: /^\s*\d+\s*$/ means "beginning of string, possible leading
3582 whitespace, one or more digits, possible trailing whitespace, end of =
3584 In other words, it means a non-negative integer.
3586 Capitalizing each of these means "the opposite":
3588 \W non-word characters
3591 In addition, here is a very handy "zero-length" escape sequence:
3594 \B not a word boundry
3596 They are zero-length because they don't represent a character, just the
3597 boundry between characters. This may not sound important right now, but =
3599 will find it useful with time.
3601 A word boundry is defined as a \w next to a \W (in either order). For
3602 example, /\Bing\b/ matches "playing" and "king", but not "jingle" =
3604 match \b) or "abc ing" (doesn't match \B).
3606 -----------------------------------
3610 Now that we have seen the details of regular expressions, we can use =
3614 Regular expressions provide a very concise way of describing whether a =
3616 of text is "what you want". For example:
3618 print "Unix\n" if $os =3D~ m/Linux|Unix|(Free|Open)?BSD|Solaris/;
3620 Remember: you can omit the "m" if you use a slash as the delimiter:
3622 print "Unix\n" if $os =3D~ /Linux|Unix|(Free|Open)?BSD|Solaris/;
3624 In Perl, you can surround a part of a regular expression with =
3626 as to extract the information:
3628 # Match digits-colon-digits-colon-digits
3629 if ( $time =3D~ /(\d+):(\d+):(\d+)/ ) {
3630 $hour =3D $1; # First set of parentheses
3631 $minute =3D $2; # Second set of parentheses
3632 $second =3D $3; # Third set of parentheses
3635 We can compress the above into one line:
3637 ($hour, $minute, $second) =3D ( $time =3D~ /(\d+):(\d+):(\d+)/ );
3639 The parentheses are necessary because "m//" returns the list ($1,$2,$3).
3640 We'll learn more about lists at a later date.
3642 If the parentheses in a regular expression are nested, it's the order of =
3644 LEFT parenthesis that counts:
3646 # Same as the previous example, but takes into account
3647 # the possibility that the seconds are not specified.
3648 if ( $time =3D~ /(\d+):(\d+)(:(\d+))?/ ) {
3649 $hour =3D $1; # First set of parentheses
3650 $minute =3D $2; # Second set of parentheses
3651 $second =3D $4; # Fourth set of parentheses
3654 In the above example, the third set of parentheses is used to associate =
3656 the colon and the digits with "?" - either both should be specified, or
3657 neither, but not one or the other. We could have set something equal to =
3659 well, but that would be something like ":59", which probably isn't very
3662 By the way, since our regular expression explicitly allows the user to =
3664 specify the number of seconds, we should anticipate the possibility that =
3666 seconds are excluded. We can test this using the "defined" function on =
3668 or "$second", but a quicker way is to do this:
3670 $second =3D $4 || 0;
3672 This will set "$second" to zero if $4 is undefined or zero.
3674 If you have enabled warnings (and you should have), Perl will complain =
3676 use an undefined value for most operations, so be sure to anticipate =
3678 undefined values in regular expressions.
3680 -----------------------------------
3684 Many URIs[*] (but not all) can be divided into protocol, authority and =
3686 (in that order). For example, consider the following URIs:
3688 ftp://example.com/foo/bar
3689 gopher://abc.example.com/xyz
3690 telnet://example.com/
3692 The protocol is the part before the colon, the authority is the part in =
3694 middle (the domain name), and the path is whatever comes after the =
3696 For example, in the first URI, the protocol is "ftp", the authority is
3697 "example.com", and the path is "/foo/bar". Note that the "://" between =
3699 protocol and the authority is not part of the protocol or the authority.
3701 Write a Perl program that uses "m//" to split a URI into protocol, =
3703 and path, or outputs a message saying that you gave it a URI that it =
3707 [*] According to RFC 2396, URLs are structural (they tell you how to get =
3709 you want) whereas URNs are logical (a permanent, abstract mapping of =
3711 to resources), and URIs include both URLs and URNs. The consensus in =
3713 seems to be to use the term "URI".
3715 -----------------------------------
3717 6) Answers to Previous Exercises
3719 a) A program to convert all 1's to i's and all 0's to o's:
3724 while ( defined(my $line =3D <STDIN>) ) {
3725 $line =3D~ tr/01/oi/;
3735 while ( defined(my $line =3D <STDIN>) ) {
3736 $line =3D~ tr/A-Za-z/C-ZABc-zab/;
3740 -----------------------------------
3742 7) Licensing Announcement
3744 This course material, including the previously released parts, is =
3746 Alice Wood and Dan Richter, and is retroactively released under the same
3747 license as Perl itself (Artistic License or GPL, your choice). This is =
3749 license of choice to make it easy for other people to integrate your =
3751 code/documentation into their own projects. It is not generally used in
3752 projects not related to Perl.
3754 By the way, Alice Wood is the author of the first four parts of this =
3756 and I got her permission to release those parts under that license as =
3759 -----------------------------------
3763 Part 1: Getting Started
3764 http://linuxchix.org/pipermail/courses/2003-March/001147.html
3767 http://linuxchix.org/pipermail/courses/2003-March/001153.html
3770 http://linuxchix.org/pipermail/courses/2003-April/001170.html
3772 Part 4: Control Structures
3773 http://linuxchix.org/pipermail/courses/2003-April/001184.html
3775 Part 4.5, a review with a little new information at the end:
3776 http://linuxchix.org/pipermail/courses/2003-July/001297.html
3778 Part 5: The "tr///" Operator:
3779 http://linuxchix.org/pipermail/courses/2003-July/001302.html
3781 -----------------------------------
3785 Works cited: "man perlre" and "man perlop"
3787 Thanks to Jacinta Richardson for fact checking.
3789 _______________________________________________
3790 Courses mailing list
3791 Courses@linuxchix.org
3792 http://mailman.linuxchix.org/mailman/listinfo/courses
3793 From daniel.richter@wimba.com Tue Aug 12 22:40:07 2003
3794 Return-Path: <daniel.richter@wimba.com>
3795 Delivered-To: courses@linuxchix.org
3796 Received: from localhost (localhost [127.0.0.1])
3797 by www.linuxchix.org (Postfix) with ESMTP id 96C7A272D81
3798 for <courses@linuxchix.org>; Tue, 12 Aug 2003 22:40:07 +1000 (EST)
3799 Received: from sodium.wimba.fr (unknown [81.80.215.129])
3800 by www.linuxchix.org (Postfix) with ESMTP id 4845A272D1E
3801 for <courses@linuxchix.org>; Tue, 12 Aug 2003 22:40:05 +1000 (EST)
3802 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
3803 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h7CCaik03177
3804 for <courses@linuxchix.org>; Tue, 12 Aug 2003 14:36:47 +0200
3805 Message-Id: <5.1.0.14.1.20030812144201.02c9bd58@mail.wimba.com>
3806 X-Sender: daniel@mail.wimba.com
3807 X-Mailer: QUALCOMM Windows Eudora Version 5.1
3808 Date: Tue, 12 Aug 2003 14:55:17 +0200
3809 To: courses@linuxchix.org
3810 From: Dan Richter <daniel.richter@wimba.com>
3811 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
3812 In-Reply-To: <003501c36fa2$c6818d30$4414a8c0@vit.ac.in>
3813 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
3815 Content-Type: text/plain; charset="us-ascii"; format=flowed
3816 X-Virus-Scanned: by AMaViS new-20020517
3817 X-Topics: Perl programming course
3818 X-BeenThere: courses@linuxchix.org
3819 X-Mailman-Version: 2.1
3821 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
3822 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
3823 List-Post: <mailto:courses@linuxchix.org>
3824 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3825 <mailto:courses-request@linuxchix.org?subject=subscribe>
3826 List-Archive: <http://linuxchix.org/pipermail/courses>
3827 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3828 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
3829 X-List-Received-Date: Tue, 12 Aug 2003 12:40:08 -0000
3830 Content-Length: 1351
3835 >($protocol,$authority,$path)=($url=~m#(\w*)://(.*\.\w*)/(.*)#);
3837 Okay, three comments.
3839 First, for the benefit of other readers, I mention that the delimiter in
3840 this case is a hash. That's perfectly legal, and many Perl users prefer to
3841 do that, both because hashes are easy to see and because they're not used
3842 very often in patterns. Just make sure you don't put a space between the
3843 "m" and the hash. :-)
3845 Second, there's a minor problem in this pattern. I foolishly neglected to
3846 give diverse example cases. Try using this program on the URI
3847 <ftp://example.com/foo.bar/baz>. This is a problem caused by "greediness",
3848 meaning that each part of the regular expression will match as much as it
3849 can before moving on. We'll see more about this later.
3851 Third, I would have put a caret at the beginning and a dollar sign at the
3852 end to make sure the whole string matches. But of course that depends on
3853 exactly how the program is to be used; greediness assures that nothing will
3856 Finally, let me say that I am quite pleased to see your response, because
3857 up until now I have seen no response whatsoever, and I was beginning to
3858 wonder if anyone at all was benefiting from the course!
3861 Nothing is sadder than the murder of a beautiful theory
3862 by a gang of ugly facts.
3863 - Jeffrey Zeldman, in A List Apart
3865 From sneha@vit.ac.in Tue Aug 12 23:38:38 2003
3866 Return-Path: <sneha@vit.ac.in>
3867 Delivered-To: courses@linuxchix.org
3868 Received: from localhost (localhost [127.0.0.1])
3869 by www.linuxchix.org (Postfix) with ESMTP id A4118272D7F
3870 for <courses@linuxchix.org>; Tue, 12 Aug 2003 23:38:38 +1000 (EST)
3871 Received: from email.vit.ac.in (unknown [210.212.241.140])
3872 by www.linuxchix.org (Postfix) with ESMTP id 3C7EC272D65
3873 for <courses@linuxchix.org>; Tue, 12 Aug 2003 23:38:32 +1000 (EST)
3874 Received: from HEXBDC ([192.168.20.68]) by email.vit.ac.in with SMTP
3875 (Microsoft Exchange Internet Mail Service Version 5.5.2653.13)
3876 id QX6BPR37; Tue, 12 Aug 2003 19:18:20 +0530
3877 Message-ID: <005a01c36fc5$7c899e20$4414a8c0@vit.ac.in>
3878 From: "sneha" <sneha@vit.ac.in>
3879 To: <courses@linuxchix.org>
3880 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
3881 <5.1.0.14.1.20030812144201.02c9bd58@mail.wimba.com>
3882 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
3885 X-MSMail-Priority: Normal
3886 X-Mailer: Microsoft Outlook Express 5.00.2314.1300
3887 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
3888 X-Virus-Scanned: by AMaViS new-20020517
3889 Content-Type: text/plain;
3890 charset="iso-8859-1"
3891 Content-Transfer-Encoding: quoted-printable
3892 X-Content-Filtered-By: Mailman/MimeDel 2.1
3893 X-Topics: Perl programming course
3894 X-BeenThere: courses@linuxchix.org
3895 X-Mailman-Version: 2.1
3897 Reply-To: sneha <sneha@vit.ac.in>
3898 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
3899 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
3900 List-Post: <mailto:courses@linuxchix.org>
3901 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3902 <mailto:courses-request@linuxchix.org?subject=subscribe>
3903 List-Archive: <http://linuxchix.org/pipermail/courses>
3904 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
3905 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
3906 Date: Tue, 12 Aug 2003 13:38:39 -0000
3907 X-Original-Date: Sun, 31 Aug 2003 19:10:47 +0530
3908 X-List-Received-Date: Tue, 12 Aug 2003 13:38:39 -0000
3909 Content-Length: 2105
3913 I hope, there r lotza readers making use of this course, I am =
3915 I appreciate ur work on this. Wish to learn more :).
3916 The following is a small correction I made in my code...after I =
3919 ftp://example.com/foo.bar/baz
3920 ($protocol,$authority,$path)=3D($url=3D~m#^(\w*)://(.[^/]*)(.*)$#);=20
3925 ----- Original Message -----=20
3926 From: Dan Richter=20
3927 To: courses@linuxchix.org=20
3928 Sent: Tuesday, August 12, 2003 6:25 PM
3929 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
3934 >($protocol,$authority,$path)=3D($url=3D~m#(\w*)://(.*\.\w*)/(.*)#);
3936 Okay, three comments.
3938 First, for the benefit of other readers, I mention that the delimiter in =
3940 this case is a hash. That's perfectly legal, and many Perl users prefer =
3942 do that, both because hashes are easy to see and because they're not =
3944 very often in patterns. Just make sure you don't put a space between the =
3946 "m" and the hash. :-)
3948 Second, there's a minor problem in this pattern. I foolishly neglected =
3950 give diverse example cases. Try using this program on the URI=20
3951 <ftp://example.com/foo.bar/baz>. This is a problem caused by =
3953 meaning that each part of the regular expression will match as much as =
3955 can before moving on. We'll see more about this later.
3957 Third, I would have put a caret at the beginning and a dollar sign at =
3959 end to make sure the whole string matches. But of course that depends on =
3961 exactly how the program is to be used; greediness assures that nothing =
3965 Finally, let me say that I am quite pleased to see your response, =
3967 up until now I have seen no response whatsoever, and I was beginning to=20
3968 wonder if anyone at all was benefiting from the course!
3971 Nothing is sadder than the murder of a beautiful theory
3972 by a gang of ugly facts.
3973 - Jeffrey Zeldman, in A List Apart
3975 _______________________________________________
3976 Courses mailing list
3977 Courses@linuxchix.org
3978 http://mailman.linuxchix.org/mailman/listinfo/courses
3979 From stolar@numericable.fr Wed Aug 13 03:49:23 2003
3980 Return-Path: <stolar@numericable.fr>
3981 Delivered-To: courses@linuxchix.org
3982 Received: from localhost (localhost [127.0.0.1])
3983 by www.linuxchix.org (Postfix) with ESMTP id 4A5F7272D8C
3984 for <courses@linuxchix.org>; Wed, 13 Aug 2003 03:49:23 +1000 (EST)
3985 Received: from partridge.numericable.net (partridge.numericable.net
3987 by www.linuxchix.org (Postfix) with ESMTP id 6C7A4272D88
3988 for <courses@linuxchix.org>; Wed, 13 Aug 2003 03:49:18 +1000 (EST)
3989 Received: (qmail 26392 invoked from network); 12 Aug 2003 17:48:48 -0000
3990 Received: from unknown (HELO raspail) ([81.220.27.8])
3991 (envelope-sender <stolar@numericable.fr>)
3992 by 0 (qmail-ldap-1.03) with SMTP
3993 for <courses@linuxchix.org>; 12 Aug 2003 17:48:48 -0000
3994 Message-ID: <003a01c360f9$fa5da470$081bdc51@raspail>
3995 From: "Isabelle & Zohar Stolar" <stolar@numericable.fr>
3996 To: <courses@linuxchix.org>, "Dan Richter" <daniel.richter@wimba.com>
3997 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
3998 <5.1.0.14.1.20030812144201.02c9bd58@mail.wimba.com>
3999 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
4000 Date: Tue, 12 Aug 2003 19:48:45 +0200
4002 Content-Type: text/plain;
4003 charset="iso-8859-1"
4004 Content-Transfer-Encoding: 7bit
4006 X-MSMail-Priority: Normal
4007 X-Mailer: Microsoft Outlook Express 6.00.2800.1158
4008 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
4009 X-Virus-Scanned: by AMaViS new-20020517
4010 X-Mailman-Approved-At: Wed, 13 Aug 2003 10:09:02 +1000
4011 X-Topics: Perl programming course
4012 X-BeenThere: courses@linuxchix.org
4013 X-Mailman-Version: 2.1
4015 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4016 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4017 List-Post: <mailto:courses@linuxchix.org>
4018 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4019 <mailto:courses-request@linuxchix.org?subject=subscribe>
4020 List-Archive: <http://linuxchix.org/pipermail/courses>
4021 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4022 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4023 X-List-Received-Date: Tue, 12 Aug 2003 17:49:23 -0000
4028 > Finally, let me say that I am quite pleased to see your response, because
4029 > up until now I have seen no response whatsoever, and I was beginning to
4030 > wonder if anyone at all was benefiting from the course!
4033 Don't be sad Dan - You have you're faithful audience :-)
4036 --------------------------------------------------------------
4037 my ($prot, $auth, $path) ;
4038 while ( defined( my $input = <STDIN> )) {
4039 if ($input =~ /(\s*[a-z]*):\/\/([\w.]+)\/(.*)/) {
4040 ( $prot, $auth, $path ) = ( $input =~
4041 /(\s*\w*):\/\/([\w.]+)\/(.*)/ ) ; # it's not a double-u it's "\ / \ /"
4042 print "protocol = $prot\n" ;
4043 print "authority = $auth\n" ;
4044 print "path = $path\n" ;
4047 print "$input cannot be parsed" ;
4050 ---------------------------------------------------------------
4052 Thanks for your lessons,
4056 From daniel.richter@wimba.com Wed Aug 13 18:53:59 2003
4057 Return-Path: <daniel.richter@wimba.com>
4058 Delivered-To: courses@linuxchix.org
4059 Received: from localhost (localhost [127.0.0.1])
4060 by www.linuxchix.org (Postfix) with ESMTP id DDF3A272D7F
4061 for <courses@linuxchix.org>; Wed, 13 Aug 2003 18:53:58 +1000 (EST)
4062 Received: from sodium.wimba.fr (unknown [81.80.215.129])
4063 by www.linuxchix.org (Postfix) with ESMTP id 46CB8272D77
4064 for <courses@linuxchix.org>; Wed, 13 Aug 2003 18:53:56 +1000 (EST)
4065 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
4066 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h7D8ofk17022
4067 for <courses@linuxchix.org>; Wed, 13 Aug 2003 10:50:41 +0200
4068 Message-Id: <5.1.0.14.1.20030813105719.02bcb8f8@mail.wimba.com>
4069 X-Sender: daniel@mail.wimba.com
4070 X-Mailer: QUALCOMM Windows Eudora Version 5.1
4071 Date: Wed, 13 Aug 2003 11:09:13 +0200
4072 To: courses@linuxchix.org
4073 From: Dan Richter <daniel.richter@wimba.com>
4074 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
4075 In-Reply-To: <003a01c360f9$fa5da470$081bdc51@raspail>
4076 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
4077 <5.1.0.14.1.20030812144201.02c9bd58@mail.wimba.com>
4079 Content-Type: text/plain; charset="us-ascii"; format=flowed
4080 X-Virus-Scanned: by AMaViS new-20020517
4081 X-Topics: Perl programming course
4082 X-BeenThere: courses@linuxchix.org
4083 X-Mailman-Version: 2.1
4085 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4086 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4087 List-Post: <mailto:courses@linuxchix.org>
4088 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4089 <mailto:courses-request@linuxchix.org?subject=subscribe>
4090 List-Archive: <http://linuxchix.org/pipermail/courses>
4091 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4092 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4093 X-List-Received-Date: Wed, 13 Aug 2003 08:53:59 -0000
4098 >Don't be sad Dan - You have you're faithful audience :-)
4100 Well, I'm glad to hear it. :-)
4102 >Here's my solution:
4104 This is good. There is a minor bug though: domain names can include a few
4105 characters not included by \w, such as hyphens (dashes).
4107 ># it's not a double-u it's "\ / \ /"
4109 That's very good: the delimiter can be escaped just like any other special
4112 Some people call that LTS: Leaning Toothpick Syndrome. :-)
4115 They had a big meeting, drank some beer and had some pizza and
4116 decided 'A' would be 65.
4117 - Jim Greenly, professor at Georgia Institute of Technology
4119 From zohar@numericable.fr Wed Aug 13 05:14:02 2003
4120 Return-Path: <zohar@numericable.fr>
4121 Delivered-To: courses@linuxchix.org
4122 Received: from localhost (localhost [127.0.0.1])
4123 by www.linuxchix.org (Postfix) with ESMTP id 9419C272D88
4124 for <courses@linuxchix.org>; Wed, 13 Aug 2003 05:14:02 +1000 (EST)
4125 Received: from partridge.numericable.net (partridge.numericable.net
4127 by www.linuxchix.org (Postfix) with ESMTP id CEDF3272D12
4128 for <courses@linuxchix.org>; Wed, 13 Aug 2003 05:14:00 +1000 (EST)
4129 Received: (qmail 25430 invoked from network); 12 Aug 2003 19:13:59 -0000
4130 Received: from unknown (HELO raspail) ([81.220.27.8])
4131 (envelope-sender <zohar@numericable.fr>)
4132 by 0 (qmail-ldap-1.03) with SMTP
4133 for <courses@linuxchix.org>; 12 Aug 2003 19:13:59 -0000
4134 Message-ID: <008201c36105$e13985c0$081bdc51@raspail>
4135 From: "Zohar Stolar" <zohar@numericable.fr>
4136 To: <courses@linuxchix.org>
4137 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
4138 <5.1.0.14.1.20030812144201.02c9bd58@mail.wimba.com>
4139 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
4140 Date: Tue, 12 Aug 2003 21:13:57 +0200
4142 Content-Type: text/plain;
4143 charset="iso-8859-1"
4144 Content-Transfer-Encoding: 7bit
4146 X-MSMail-Priority: Normal
4147 X-Mailer: Microsoft Outlook Express 6.00.2800.1158
4148 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
4149 X-Virus-Scanned: by AMaViS new-20020517
4150 X-Topics: Perl programming course
4151 X-BeenThere: courses@linuxchix.org
4152 X-Mailman-Version: 2.1
4154 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4155 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4156 List-Post: <mailto:courses@linuxchix.org>
4157 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4158 <mailto:courses-request@linuxchix.org?subject=subscribe>
4159 List-Archive: <http://linuxchix.org/pipermail/courses>
4160 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4161 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4162 X-List-Received-Date: Tue, 12 Aug 2003 19:14:03 -0000
4167 > Finally, let me say that I am quite pleased to see your response, because
4168 > up until now I have seen no response whatsoever, and I was beginning to
4169 > wonder if anyone at all was benefiting from the course!
4172 Don't be sad Dan - You have you're faithful audience :-)
4175 --------------------------------------------------------------
4176 my ($prot, $auth, $path) ;
4177 while ( defined( my $input = <STDIN> )) {
4178 if ($input =~ /(\s*[a-z]*):\/\/([\w.]+)\/(.*)/) {
4179 ( $prot, $auth, $path ) = ( $input =~
4180 /(\s*\w*):\/\/([\w.]+)\/(.*)/ ) ; # it's not a double-u it's "\ / \ /"
4181 print "protocol = $prot\n" ;
4182 print "authority = $auth\n" ;
4183 print "path = $path\n" ;
4186 print "$input cannot be parsed" ;
4189 ---------------------------------------------------------------
4191 Thanks for your lessons,
4195 From katie@katie-and-rob.org Fri Aug 15 00:03:42 2003
4196 Return-Path: <katie@katie-and-rob.org>
4197 Delivered-To: courses@linuxchix.org
4198 Received: from localhost (localhost [127.0.0.1])
4199 by www.linuxchix.org (Postfix) with ESMTP id 28656272DB3
4200 for <courses@linuxchix.org>; Fri, 15 Aug 2003 00:03:42 +1000 (EST)
4201 Received: from localhost.localdomain (katie-and-rob.org [66.93.56.94])
4202 by www.linuxchix.org (Postfix) with ESMTP id 2F0BD272DB2
4203 for <courses@linuxchix.org>; Fri, 15 Aug 2003 00:01:54 +1000 (EST)
4204 Received: from localhost.localdomain (localhost [127.0.0.1] (may be forged))
4205 by localhost.localdomain (8.12.8/8.12.8) with ESMTP id h7EE0ewC003013
4206 for <courses@linuxchix.org>; Thu, 14 Aug 2003 10:00:40 -0400
4207 Received: (from katie@localhost)
4208 by localhost.localdomain (8.12.8/8.12.8/Submit) id h7EE0TJF003011
4209 for courses@linuxchix.org; Thu, 14 Aug 2003 10:00:29 -0400
4210 X-Authentication-Warning: localhost.localdomain: katie set sender to
4211 katie@katie-and-rob.org using -f
4212 Date: Thu, 14 Aug 2003 10:00:29 -0400
4213 From: Katie Bechtold <katie@katie-and-rob.org>
4214 To: courses@linuxchix.org
4215 Subject: Re: [Courses] [Perl] Part 6: The "m//" Operator
4216 Message-ID: <20030814140029.GO1376@blue>
4217 Mail-Followup-To: courses@linuxchix.org
4218 References: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
4220 Content-Type: text/plain; charset=us-ascii
4221 Content-Disposition: inline
4222 In-Reply-To: <5.1.0.14.1.20030808124741.02d63008@mail.wimba.com>
4223 X-Operating-System: Linux 2.4.20-19.9bigmem
4224 X-PGP-Key: http://katie-and-rob.org/katie/pgp_public_key
4225 User-Agent: Mutt/1.5.4i
4226 X-Virus-Scanned: by AMaViS new-20020517
4227 X-Topics: Perl programming course
4228 X-BeenThere: courses@linuxchix.org
4229 X-Mailman-Version: 2.1
4231 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4232 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4233 List-Post: <mailto:courses@linuxchix.org>
4234 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4235 <mailto:courses-request@linuxchix.org?subject=subscribe>
4236 List-Archive: <http://linuxchix.org/pipermail/courses>
4237 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4238 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4239 X-List-Received-Date: Thu, 14 Aug 2003 14:03:42 -0000
4243 On Fri, Aug 08, 2003 at 12:48:50PM +0200, Dan Richter wrote:
4244 > Write a Perl program that uses "m//" to split a URI into protocol, authority
4245 > and path, or outputs a message saying that you gave it a URI that it can't
4248 Here's my answer (written before I looked at any of the offered
4254 print "Input a URI: ";
4255 chomp(my $uri = <STDIN>);
4257 if ( $uri =~ /([^:]+):\/\/(([^\/]+))(\/.*)/ ) {
4263 print "Protocol: " . $protocol . "\n";
4264 print "Authority: " . $authority . "\n";
4265 print "Path: " . $path . "\n";
4268 print "Invalid URI.\n";
4272 Katie Bechtold http://katie-and-rob.org/
4274 Backward conditioning:
4275 Putting saliva in a dog's mouth in an attempt to make a bell ring.
4276 From daniel.richter@wimba.com Fri Aug 15 00:40:18 2003
4277 Return-Path: <daniel.richter@wimba.com>
4278 Delivered-To: courses@linuxchix.org
4279 Received: from localhost (localhost [127.0.0.1])
4280 by www.linuxchix.org (Postfix) with ESMTP id 7A98E272D34
4281 for <courses@linuxchix.org>; Fri, 15 Aug 2003 00:40:18 +1000 (EST)
4282 Received: from sodium.wimba.fr (unknown [81.80.215.129])
4283 by www.linuxchix.org (Postfix) with ESMTP id 5E63E272D3D
4284 for <courses@linuxchix.org>; Fri, 15 Aug 2003 00:40:02 +1000 (EST)
4285 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
4286 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h7EEaVk06905
4287 for <courses@linuxchix.org>; Thu, 14 Aug 2003 16:36:31 +0200
4288 Message-Id: <5.1.0.14.1.20030814145111.02cea6a0@mail.wimba.com>
4289 X-Sender: daniel@mail.wimba.com
4290 X-Mailer: QUALCOMM Windows Eudora Version 5.1
4291 Date: Thu, 14 Aug 2003 16:55:01 +0200
4292 To: courses@linuxchix.org
4293 From: Dan Richter <daniel.richter@wimba.com>
4294 Subject: [Courses] [Perl] Part 7: More About "m//"
4296 Content-Type: text/plain; charset="us-ascii"; format=flowed
4297 X-Virus-Scanned: by AMaViS new-20020517
4298 X-Topics: Perl programming course
4299 X-BeenThere: courses@linuxchix.org
4300 X-Mailman-Version: 2.1
4302 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4303 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4304 List-Post: <mailto:courses@linuxchix.org>
4305 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4306 <mailto:courses-request@linuxchix.org?subject=subscribe>
4307 List-Archive: <http://linuxchix.org/pipermail/courses>
4308 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4309 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4310 X-List-Received-Date: Thu, 14 Aug 2003 14:40:19 -0000
4311 Content-Length: 12426
4314 LinuxChix Perl Course Part 7: More About "m//"
4318 3) Anticipating Failed Matches
4319 4) Using Open Patterns
4321 6) Answer to Previous Exercise
4326 -----------------------------------
4330 I realise that Part 6 was quite a bit to handle. Regular expressions can be a
4331 lot to get your head around -- and there's no good way to split them up into
4332 short lessons -- but once you've mastered them they're very powerful.
4334 Fortunately, Part 7 has less actual content to memorise -- just a lot of
4335 warnings designed to help you avoid bugs.
4337 -----------------------------------
4339 2) Options for "m//"
4341 The "m//" operator takes several options, but we will only discuss four of them
4343 i Do case-insensitive pattern matching.
4344 m Treat string as multiple lines (let /^/ and /$/ match "\n").
4345 s Treat string as single line (let /./ match "\n").
4346 g Match globally, i.e., find all occurrences.
4347 To see the full list, consult "man perlop".
4349 The easiest option to understand is "i" (case insensitive):
4351 print "February\n" if $date =~ /feb/i; # Match "Feb", "FEB", etc.
4353 The "s" option (treat string as single line) is only slightly trickier.
4354 Normally /./ matches any character EXCEPT a newline. This is often what you
4355 want, but if you want /./ to match a newline as well, use "s":
4357 my($head,$body) = ( $html =~ m!<head>(.*)</head>.*<body>(.*)</body>!si );
4359 We use the "s" option here because line breaks shouldn't be taken into account.
4361 The "m" option (treat string a multiple lines) looks like it would be the
4362 opposite of the "s" option, but in fact the two can be used together because
4363 they affect the meanings of different characters. "m" causes /^/ and /$/ to
4364 match the beginning and end of a "line" in the middle of the string in addition
4365 to their normal meaning (the beginning or end of the string itself). The
4366 "lines" in a string are separated by the newline character "\n".
4368 my($subject) = ( $email =~ /^Subject: (.*)$/m );
4370 Note that even with the "m" option set, /^/ can only be used at the beginning
4371 of the pattern and /$/ can only be used at the end. The purpose of the "m"
4372 option is to allow you to identify a match without worrying about whether it's
4373 the beginning of the string or just the beginning of the line. If you're
4374 looking for a line break in the middle of a pattern, you know it's a newline,
4375 so use /\n/ instead:
4377 if ( $foo =~ /^one line^two lines$/m ) { ... } # Wrong
4378 if ( $foo =~ /^one line$two lines$/m ) { ... } # Wrong
4379 if ( $foo =~ /^one line$^two lines$/m ) { ... } # Wrong
4380 if ( $foo =~ /^one line\ntwo lines$/m ) { ... } # Right
4382 Finally, the "g" option causes a "global" match. This doesn't change WHETHER a
4383 string matches; rather, it returns all matches at once:
4385 # Find up to three words that contain the letter "q".
4386 my($a,$b,$c) = ( $list =~ /[a-z]*q[a-z]*/g );
4388 This isn't very useful right now, but it will be more helpful when we start
4391 -----------------------------------
4393 3) Anticipating Failed Matches
4395 The "m//" operator is a great way to extract information from a string, but you
4396 should always anticipate the possibility that the string won't match your
4399 For example, consider the following code:
4406 while ( 1 ) { # Loop forever.
4407 print "Enter patient name: ";
4408 chomp($name = <STDIN>);
4409 print "Enter blood type: ";
4410 chomp($blood = <STDIN>);
4411 $blood =~ m/(AB|A|B|O)/;
4412 print "Transfusing type $1 blood into $name\n\n";
4417 Enter patient name: Bob
4418 Enter blood type: type AB
4419 Transfusing type AB blood into Bob
4421 So far, so good, but now the nurse accidentally hits <Enter> before giving a
4424 Enter patient name: Jack
4426 Transfusing type AB blood into Jack
4428 Uh oh: since this match didn't work, "$1" maintained its value from the
4429 previous match. If Jack's blood type isn't AB, this could be fatal.
4431 So let's try writing that middle part differently:
4433 chomp($blood = <STDIN>);
4434 my($type) = ($blood =~ m/(AB|A|B|O)/);
4435 print "Transfusing type $type blood into $name\n\n";
4437 Okay, let's run it now:
4439 Enter patient name: Bob
4440 Enter blood type: type AB
4441 Transfusing type AB blood into Bob
4443 Enter patient name: Jack
4445 Use of uninitialized value in concatenation (.) or string at [...]
4446 Transfusing type blood into Jack
4450 The second-to-last line will only appear if you turned on warnings (which you
4451 should have). You see, "$type" only has a meaningful value if the user entered
4452 meaningful input; otherwise the match fails and "$type" has the special value
4453 "undef". So Jack is getting undefined blood. I don't know if that carries any
4454 risk but I wouldn't want to try it.
4459 if ( $blood =~ m/(AB|A|B|O)/ ) {
4462 print "Transfusing type $type blood into $name\n\n";
4464 This "fix" has the same result as the previous one: "$type" is only set if the
4465 blood type is valid, but the transfusion occurs anyway.
4467 So let's do this right:
4474 while ( 1 ) { # Loop forever.
4475 print "Enter patient name: ";
4476 chomp($name = <STDIN>);
4477 print "Enter blood type: ";
4478 chomp($blood = <STDIN>);
4479 if ( $blood =~ m/(AB|A|B|O)/ ) {
4480 print "Transfusing type $1 blood into $name\n\n";
4483 print "Wrong blood type; try again.\n\n";
4487 Of course, we don't have to use "$1": we could do this instead:
4489 if ( my($type) = ($blood =~ m/(AB|A|B|O)/) ) {
4490 print "Transfusing type $type blood into $name\n\n";
4493 print "Wrong blood type; try again.\n\n";
4496 The issue isn't whether or not you use "$1". What's important is that you
4497 always plan for a failed match.
4499 -----------------------------------
4501 4) Using Open Patterns
4503 It's great to check your input, but do it properly. For example, you often want
4504 a user to enter an e-mail address, so you test to make sure the e-mail address
4505 is valid. You might write a test like this:
4507 if ( $address =~ /^[A-Za-z0-9.]+\@[A-Za-z0-9.]+$/ ) { ... }
4509 But can an e-mail address include quotes? What about slashes?
4511 It turns out that an e-mail address can contain almost any character (at least
4512 on the left side of the "@"). So the following are valid e-mail addresses:
4517 And let's not forget that not every top-level domain has just two or three
4518 letters. If you doubt this, go to:
4519 http://index.museum/
4521 For more information on e-mail addresses, consult RFCs 822 and 2822:
4522 http://www.faqs.org/rfcs/rfc822.html
4523 http://www.faqs.org/rfcs/rfc2822.html
4525 Another example: suppose you want someone to enter his last name (sirname). If
4526 you just write the validation without thinking about it, you might forget that
4527 a last name can contain dashes, apostrophes (think "O'Conner") spaces (yes,
4528 some people have compound last names) and accented letters (non-English names).
4530 So remember: check your input, but do it properly. Don't assume you know more
4531 than you do, and give the user some leeway.
4533 Note: some of the examples in this tutorial are deliberately simplified. For
4534 example, in some places we operate on the "Subject" field of an e-mail as
4535 though it was a single line, but RFC 822 says that e-mail headers can be
4536 "folded" into multiple lines. A real program would have to be a little more
4539 -----------------------------------
4543 Computers make very bad poets. However, a computer program might be able to
4544 help a poet find a good rhyme.
4546 Write a program that accepts lines of poetry (one at a time) and looks for
4547 rhymes in a pre-programmed list of lines of poetry. The lines in the pre-
4548 programmed list should be separated by newlines.
4550 Of course, it's hard for a computer to determine whether two words rhyme. For
4551 this exercise we will use a very simple mechanism: we will assume that two
4552 words rhyme if their last three letters are the same. For extra credit, try to
4553 find a better mechanism.
4555 Your program should look like this:
4560 # The following is a multi-line string.
4561 # (It's the beginning of a poem by A. E. Housman.)
4563 my $possibilities = "Terence, this is stupid stuff:
4564 You eat your victuals fast enough;
4565 There can't be much amiss, 'tis clear,
4566 to see the rate you drink your beer.
4567 But oh, good Lord, the verse you make,
4568 It gives a chap the belly-ache.";
4570 while ( defined( my $line = <STDIN> ) ) {
4571 if ( my($last_letters) = ( $line =~ [** regular expression 1 **] ) ) {
4572 if ( my($match) = ( $possibilities =~ [** regular expression 2 **] ) ) {
4573 print "That rhymes with: $match\n";
4576 print "I can't think of anything that rhymes with that.\n";
4580 print "Sorry, I couldn't even begin to rhyme that one.\n";
4584 All you have to do is fill in the parts in [** brackets **].
4587 a) Remember that you only want to match letters; punctuation should be ignored.
4588 b) Use variable interpolation in the second regular expression. That is,
4589 regular expression 2 should contain '$last_letters'. Doing that can
4590 sometimes lead to unexpected results (e.g., if $last_letters contained a
4591 period it would be interpreted as a wild card), but since we're only
4592 matching letters we shouldn't have any problems.
4593 c) Make sure your match is case-insensitive: "poodle" rhymes with "NOODLE".
4594 d) Test for matches at the beginning, middle and end of $possibilities (that
4595 is: the first line, the last line, and some other line).
4597 In case you're wondering, I chose the poem "Terence, this is stupid stuff" for
4598 two reasons. First, the rhyming words are spelled very differently, so whether
4599 you want to rhyme with "fluff" or "rough", you will find a rhyme. Second, it's
4600 what people will say to YOU if you let your computer write your poetry!
4602 -----------------------------------
4604 6) Answer to Previous Exercise
4606 The previous exercise was to write a program that splits a URI into protocol,
4607 host and path. Here is mine:
4612 while ( defined(my $line = <STDIN>) ) {
4614 if ( my($protocol,$host,$path) =
4615 ($line =~ m<^([^:/]+)://([^/]+)(/?.*)$> ) ) {
4616 print "protocol=$protocol, host=$host, path=$path\n";
4619 print "Sorry: I can't parse that URI.\n";
4623 Yours will probably be different. For example, I was very loose on the
4624 hostname; you might have stipulated that it must not contain certain
4625 characters, such as spaces.
4627 -----------------------------------
4631 Part 1: Getting Started
4632 http://linuxchix.org/pipermail/courses/2003-March/001147.html
4635 http://linuxchix.org/pipermail/courses/2003-March/001153.html
4638 http://linuxchix.org/pipermail/courses/2003-April/001170.html
4640 Part 4: Control Structures
4641 http://linuxchix.org/pipermail/courses/2003-April/001184.html
4643 Part 4.5, a review with a little new information at the end:
4644 http://linuxchix.org/pipermail/courses/2003-July/001297.html
4646 Part 5: The "tr///" Operator:
4647 http://linuxchix.org/pipermail/courses/2003-July/001302.html
4649 Part 6: Part 6: The "m//" Operator
4650 http://linuxchix.org/pipermail/courses/2003-August/001305.html
4652 -----------------------------------
4658 b) Kirrily Robert, Paul Fenwick and Jacinta Richardson's "Intermedia Perl",
4659 which you can find (along with their "Introduction to Perl") at:
4660 http://www.perltraining.com.au/notes.html
4662 Thanks to Jacinta Richardson for fact checking.
4664 -----------------------------------
4668 This course (i.e., all parts of it) is copyright Alice Wood and Dan Richter,
4669 and is released under the same license as Perl itself (Artistic License or GPL,
4670 your choice). This is the license of choice to make it easy for other people to
4671 integrate your Perl code/documentation into their own projects. It is not
4672 generally used in projects not related to Perl.
4674 From daniel.richter@wimba.com Fri Aug 22 19:47:20 2003
4675 Return-Path: <daniel.richter@wimba.com>
4676 Delivered-To: courses@linuxchix.org
4677 Received: from localhost (localhost [127.0.0.1])
4678 by www.linuxchix.org (Postfix) with ESMTP id 04892272D2E
4679 for <courses@linuxchix.org>; Fri, 22 Aug 2003 19:47:19 +1000 (EST)
4680 Received: from sodium.wimba.fr (unknown [81.80.215.129])
4681 by www.linuxchix.org (Postfix) with ESMTP id EBFB9272CD1
4682 for <courses@linuxchix.org>; Fri, 22 Aug 2003 19:47:16 +1000 (EST)
4683 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
4684 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h7M9hZk15403
4685 for <courses@linuxchix.org>; Fri, 22 Aug 2003 11:43:38 +0200
4686 Message-Id: <5.1.0.14.1.20030821132207.02dfe738@mail.wimba.com>
4687 X-Sender: daniel@mail.wimba.com
4688 X-Mailer: QUALCOMM Windows Eudora Version 5.1
4689 Date: Fri, 22 Aug 2003 12:03:45 +0200
4690 To: courses@linuxchix.org
4691 From: Dan Richter <daniel.richter@wimba.com>
4692 Subject: [Courses] [Perl] Part 8: The "s///" Operator
4694 Content-Type: text/plain; charset="us-ascii"; format=flowed
4695 X-Virus-Scanned: by AMaViS new-20020517
4696 X-Topics: Perl programming course
4697 X-BeenThere: courses@linuxchix.org
4698 X-Mailman-Version: 2.1
4700 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
4701 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
4702 List-Post: <mailto:courses@linuxchix.org>
4703 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4704 <mailto:courses-request@linuxchix.org?subject=subscribe>
4705 List-Archive: <http://linuxchix.org/pipermail/courses>
4706 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
4707 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
4708 X-List-Received-Date: Fri, 22 Aug 2003 09:47:20 -0000
4709 Content-Length: 11162
4712 LinuxChix Perl Course Part 8: The "s///" Operator
4715 2) The "s///" Operator
4719 5) Answer to Previous Exercise
4724 -----------------------------------
4728 We're getting to the best part now. The "s///" operator is one of the most
4729 powerful operators in Perl. You'll find yourself using it again and again.
4731 Let me also mention that I'll be on vacation for the next two weeks, so you
4732 won't hear from me again until September 12. But I encourage you to send your
4733 exercises to the mailing list and to give suggestions on other people's
4736 -----------------------------------
4738 2) The "s///" Operator
4740 The "s///" operator performs a regular expression substitution. For example,
4741 try the following program:
4747 $text =~ s/foo/bear/;
4750 The command "s/X/Y/" replaces the first instance of the regular expression X
4751 with the text Y. (We'll see how to replace all instances in a moment.) If the
4752 regular expression X is not found anywhere in the string, the command is
4755 A common use for "s///" is to remove text entirely (by specifying that the
4756 text is to be replaced by nothing):
4758 $witness_data =~ s/Name: \w+//; # Anonymise data by removing name.
4760 As you have probably guessed, "s///" can use any delimiter, just like "m//"
4763 $text =~ s/foo/bar/;
4764 $text =~ s!foo!bar!;
4765 $text =~ s<foo><bar>;
4767 The "s///" operator can also use parentheses just like "m//":
4769 # Munge e-mail address. Note the "escaped at": @ is a special character.
4770 $address =~ s/(.+)\@([^@]+)/$1 at $2/;
4771 # Not a very logical way to do it; just for demonstration.
4773 Note: you may see "\1" used instead of "$1". Both work, but "$1" is the
4776 The "s///" operator accepts all of the options we discussed for "m//", and
4777 they have the same meaning:
4778 i Do case-insensitive pattern matching.
4779 m Treat string as multiple lines (let /^/ and /$/ match "\n").
4780 s Treat string as single line (let /./ match "\n").
4781 g Match globally, i.e., change all occurrences.
4782 We will also discuss an additional option:
4783 e Evaluate the replacement as Perl code.
4787 # Convert "foo", "Foo" or "FOO" to "bar".
4788 $text =~ s/foo/bar/i;
4790 # Reply to an e-mail. /^/ and /$/ mean beginning and end of line.
4791 $headers =~ s/^Subject: (.*)$/Subject: Re: $1/m;
4793 The "g" option still means "global", but with "s///" it means "replace all
4794 instances" rather than just "match all instances":
4796 # Convert all C++ comments to C comments.
4797 $C_code =~ s#//(.*)$#/*$1*/#mg;
4799 Note that we used the "$1" variable even though there are multiple matches; $1
4800 is re-evaluated for each match.
4802 You won't use the "e" option very often, but it comes in very handy when you
4803 do need it. For example:
4805 # Convert Japanese yen to euros.
4806 $price_list =~ s/([0-9.]+) yen/ ($1 * $conversion_factor) . ' euros' /ge;
4808 -----------------------------------
4812 Regular expressions in Perl are "greedy" by default, meaning that * and + will
4813 match as many characters as possible. For example, in the following regular
4816 my($answer) = ( $foo =~ /The answer is: (.+)/ );
4818 the /.+/ will match the whole answer, even though it would theoretically be
4819 correct to only match the first character of the answer, or the first few
4822 Usually you want to be greedy, but not always. If you don't want to be greedy,
4823 add a question mark after the * or +. This doesn't change WHETHER there's a
4824 match; it only changes the CONTENT of the match.
4826 For example, if you want to strip HTML tags:
4828 $html =~ s/<.*>//sg; # Oops: greediness got us here.
4830 That doesn't work because Perl sees "<html>...</html>" as one big /<.*>/ (it
4831 starts with "<" and ends with ">", right?). So it performs one replacement on
4832 the entire HTML document, replacing the whole document with nothing! We need
4833 to turn off the greediness:
4835 $html =~ s/<.*?>//sg; # Much better.
4837 This causes the /.*/ to be as short as possible while still making a match.
4839 Of course, we could do this instead:
4841 $html =~ s/<[^>]*>//sg; # Also works.
4843 But it's not always that easy. Consider this code to strip C-style comments
4846 # Remove /* ... */ comments from your C code.
4847 $C_code =~ s</\*(.*?)\*/><>sg;
4848 # (Asterisks are escaped. Parentheses are provided only for clarity.
4849 # The <> is the delimiter.)
4851 Once you get your head around that regular expression, you'll see that no
4852 greedy regular expression would do.
4854 -----------------------------------
4858 a) Harvard cardiologist Thomas Michel writes in his "Guide to Politically
4859 Correct Cardiology" about the importance of using inoffensive medical terms.
4860 For example, he suggests saying "metabolically different" instead of the
4861 highly offensive "dead"[*]. Write a Perl program that reads a medical
4862 diagnosis (or any other input) and uses "s///" to change the word "dead" to
4863 "metabolically different". Don't forget to use the "g" option to replace ALL
4864 matches, and to use /\b/ (word break) to avoid false matches such as "deaden"
4867 [*] Yes, the paper is real and the scientist is real. Of course, he wasn't
4868 taking himself too seriously when he wrote the paper.
4870 b) American English is slightly different from British English in several
4871 respects, one of which is spelling[*]. For example, words ending in "ise" in
4872 England generally end in "ize" in the United States, e.g., "exercise" becomes
4873 "exercize". Write a Perl program that "translates" such words from American to
4874 British English, i.e., changes words ending in "ize" to end in "ise". For
4875 extra credit, take into account variations like "exercizes" and "exercizing".
4877 [*] Blame it on Noah Webster. The writer of the first dictionary in America,
4878 Webster deliberately chose to spell words differently for reasons both
4879 practical (making the language easier to learn for immigrants) and patriotic
4880 (declaring linguistic independence).
4882 -----------------------------------
4884 5) Answer to Previous Exercise
4886 The previous exercise was to write a program to help you rhyme, where two
4887 words are defined to "rhyme" if they have the last three letters in common.
4888 Here is an example of such a program.
4893 my $possibilities = "Terence, this is stupid stuff:
4894 You eat your victuals fast enough;
4895 There can't be much amiss, 'tis clear,
4896 to see the rate you drink your beer.
4897 But oh, good Lord, the verse you make,
4898 It gives a chap the belly-ache.";
4900 # The beginning of a poem by A. E. Housman
4902 while ( defined( my $line = <STDIN> ) ) {
4903 if ( my($last_letters) = ( $line =~ /(\w?\w?\w)\W*$/m ) ) {
4904 if ( my($match) = ( $possibilities =~ /^(.*$last_letters\W*)$/mi ) ) {
4905 print "That rhymes with: $match\n";
4908 print "I can't think of anything that rhymes with that.\n";
4912 print "Sorry: I couldn't even begin to rhyme that one.\n";
4916 The first regular expression:
4917 if ( my($last_letters) = ( $line =~ /(\w?\w?\w)\W*$/m ) ) {
4918 means three "word characters", possibly followed by non-word characters,
4919 followed by the end of the line (or the end of the string). I used \w for
4920 brevity rather than [A-Za-z] even though \w includes digits and the underscore
4921 character: there's no way for the computer to guess that "3" rhymes with
4924 The question marks are there to anticipate the possibility that the last word
4925 might be less than three letters long. (Our definition of "rhyming" didn't
4926 actually take into account this case, but I considered it anyway.)
4928 The last-three-letters algorithm has few false positives but a lot of false
4929 negatives (e.g., it wouldn't recognise that any of the lines in the above poem
4930 by Housman rhyme). To try to improve accuracy, I wrote a regular expression
4931 that interprets the rhyming part of the word as follows:
4932 a) if the word ends in a "y", the rhyming part is the "y" and any vowels
4933 immediately before it: "flY", "plAY".
4934 b) if the word ends in an "e" not directly following a vowel, the rhyming part
4935 is defined like in (c), excecpt the silent "e" is treated as a consonant:
4936 "possIBLE", "remAKE".
4937 c) otherwise, the rhyming part is the part containing the last group of one or
4938 more vowels followed by any consonants: "shACK", "sEA", "sEE".
4940 The regular expression is as follows:
4942 my $vowel = '[aeiou]';
4943 my $consonant = '[b-df-hj-np-tv-z]';
4944 my $consonant_or_e_not_y = '[b-hj-np-tvwxz]';
4946 my $match_ending_letter = '[aiouy]';
4947 my $match_continuing_letter = $consonant;
4949 while ( defined( my $line = <STDIN> ) ) {
4950 if ( my($last_letters) = ( $line =~
4951 /($vowel*$consonant*$consonant_or_e_not_y|$vowel*$match_ending_letter)\W*$/im
4954 Though impressive, in practice it's not any more accurate than the last-three-
4955 letters algorithm, and it's probably less accurate. Oh, well.
4957 Finding rhymes is truly not for the fainthearted programmer. For example, the
4958 words "food" and "good" don't rhyme even though their spelling suggests that
4959 they would, and the word "read" can be pronounced either like "reed" or "red",
4960 depending on its function in the sentence.
4962 But if you ever do get really good at teaching poetry to computers, perhaps
4963 you can join Damian Conway, "the mad scientist of Perl", who wrote a Perl
4964 module to automatically generate a haiku before displaying an error message.
4968 what you need is a moment
4971 -----------------------------------
4975 Part 1: Getting Started
4976 http://linuxchix.org/pipermail/courses/2003-March/001147.html
4979 http://linuxchix.org/pipermail/courses/2003-March/001153.html
4982 http://linuxchix.org/pipermail/courses/2003-April/001170.html
4984 Part 4: Control Structures
4985 http://linuxchix.org/pipermail/courses/2003-April/001184.html
4987 Part 4.5, a review with a little new information at the end:
4988 http://linuxchix.org/pipermail/courses/2003-July/001297.html
4990 Part 5: The "tr///" Operator
4991 http://linuxchix.org/pipermail/courses/2003-July/001302.html
4993 Part 6: The "m//" Operator
4994 http://linuxchix.org/pipermail/courses/2003-August/001305.html
4996 Part 7: More About "m//"
4997 http://linuxchix.org/pipermail/courses/2003-August/001322.html
4999 -----------------------------------
5003 Works cited: "man perlop"
5005 Thanks to Jacinta Richardson for fact checking.
5007 -----------------------------------
5011 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and Dan
5012 Richter, and is released under the same license as Perl itself (Artistic
5013 License or GPL, your choice). This is the license of choice to make it easy
5014 for other people to integrate your Perl code/documentation into their own
5015 projects. It is not generally used in projects unrelated to Perl.
5018 From daniel.richter@wimba.com Fri Sep 12 22:26:49 2003
5019 Return-Path: <daniel.richter@wimba.com>
5020 Delivered-To: courses@linuxchix.org
5021 Received: from localhost (localhost [127.0.0.1])
5022 by www.linuxchix.org (Postfix) with ESMTP id 702BB272D65
5023 for <courses@linuxchix.org>; Fri, 12 Sep 2003 22:26:49 +1000 (EST)
5024 Received: from www.linuxchix.org ([127.0.0.1])
5025 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
5026 id 28680-05 for <courses@linuxchix.org>;
5027 Fri, 12 Sep 2003 22:26:49 +1000 (EST)
5028 Received: from sodium.wimba.fr (unknown [81.80.215.129])
5029 by www.linuxchix.org (Postfix) with ESMTP id 11673272D8F
5030 for <courses@linuxchix.org>; Fri, 12 Sep 2003 22:25:47 +1000 (EST)
5031 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
5032 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h8CCKXk24758
5033 for <courses@linuxchix.org>; Fri, 12 Sep 2003 14:20:34 +0200
5034 Message-Id: <5.1.0.14.1.20030912143601.02cd65e0@mail.wimba.com>
5035 X-Sender: daniel@mail.wimba.com
5036 X-Mailer: QUALCOMM Windows Eudora Version 5.1
5037 Date: Fri, 12 Sep 2003 14:43:00 +0200
5038 To: courses@linuxchix.org
5039 From: Dan Richter <daniel.richter@wimba.com>
5040 Subject: [Courses] [Perl] Sorry: I'm behind
5042 Content-Type: text/plain; charset="us-ascii"; format=flowed
5043 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
5044 X-Topics: Perl programming course
5045 X-BeenThere: courses@linuxchix.org
5046 X-Mailman-Version: 2.1
5048 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
5049 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
5050 List-Post: <mailto:courses@linuxchix.org>
5051 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5052 <mailto:courses-request@linuxchix.org?subject=subscribe>
5053 List-Archive: <http://linuxchix.org/pipermail/courses>
5054 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5055 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
5056 X-List-Received-Date: Fri, 12 Sep 2003 12:26:49 -0000
5060 Well, I came back from vacation a few days ago and I'm already behind. A
5061 new record. As one person put it, the sooner you get behind, the more time
5062 you have to catch up.
5064 So I'm sorry to say that I won't get Part 9 out today. With luck it will be
5068 [Larry] Wall [inventor of Perl] believes that people think about
5069 things in different ways, that natural languages accommodate many
5070 mindsets, and that programming languages should too.
5071 - Jon Udell, in his essay, "A Perl Hacker in the Land of Python"
5073 From daniel.richter@wimba.com Tue Sep 16 18:10:27 2003
5074 Return-Path: <daniel.richter@wimba.com>
5075 Delivered-To: courses@linuxchix.org
5076 Received: from localhost (localhost [127.0.0.1])
5077 by www.linuxchix.org (Postfix) with ESMTP id DCDD3272D4F
5078 for <courses@linuxchix.org>; Tue, 16 Sep 2003 18:10:26 +1000 (EST)
5079 Received: from www.linuxchix.org ([127.0.0.1])
5080 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
5081 id 11221-08 for <courses@linuxchix.org>;
5082 Tue, 16 Sep 2003 18:10:26 +1000 (EST)
5083 Received: from sodium.wimba.fr (unknown [81.80.215.129])
5084 by www.linuxchix.org (Postfix) with ESMTP id EC817272D20
5085 for <courses@linuxchix.org>; Tue, 16 Sep 2003 18:10:23 +1000 (EST)
5086 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
5087 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h8G85jk16733
5088 for <courses@linuxchix.org>; Tue, 16 Sep 2003 10:05:45 +0200
5089 Message-Id: <5.1.0.14.1.20030916102102.02dbed88@mail.wimba.com>
5090 X-Sender: daniel@mail.wimba.com
5091 X-Mailer: QUALCOMM Windows Eudora Version 5.1
5092 Date: Tue, 16 Sep 2003 10:28:05 +0200
5093 To: courses@linuxchix.org
5094 From: Dan Richter <daniel.richter@wimba.com>
5095 Subject: [Courses] [Perl] Part 9: Simple File Access
5097 Content-Type: text/plain; charset="us-ascii"; format=flowed
5098 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
5099 X-Topics: Perl programming course
5100 X-BeenThere: courses@linuxchix.org
5101 X-Mailman-Version: 2.1
5103 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
5104 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
5105 List-Post: <mailto:courses@linuxchix.org>
5106 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5107 <mailto:courses-request@linuxchix.org?subject=subscribe>
5108 List-Archive: <http://linuxchix.org/pipermail/courses>
5109 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5110 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
5111 X-List-Received-Date: Tue, 16 Sep 2003 08:10:27 -0000
5112 Content-Length: 10020
5115 LinuxChix Perl Course Part 9: Simple File Access
5118 2) The "die" Command
5119 3) Simple File Access
5121 5) Answers to Previous Exercises
5126 -----------------------------------
5130 As we will see shortly, Perl uses the "open" command to read from and write to
5131 files. The "open" command is very powerful and is not limited to files, but we
5132 won't see that until next week.
5134 -----------------------------------
5136 2) The "die" Command
5138 The "die" command does exactly that: it kills your program. Example:
5142 Of course, you'll usually want to make the command conditional and give a
5143 helpful error message:
5145 if ( $there_was_an_error ) {
5146 die "There was an error. Sorry. Aborting.";
5149 If you run this command (and you set $there_was_an_error to some non-zero
5150 value), you will see:
5152 There was an error. Sorry. Aborting. at test.pl line 7.
5154 Of course, if you try it yourself, "test.pl" will be replaced by the file in
5155 which you did the test. Perl is trying to be helpful by telling you where the
5156 program died. But if you don't want that additional information, put a newline
5157 on the end of the message string:
5159 die "There was an error. Sorry. Aborting.\n"; # No file+line information.
5161 There are two additional subtleties to the "die" command:
5162 a) it causes the message to be sent to standard error, not standard out, so
5163 the user will see it even if he is piping the output to a file.
5164 b) it sets the program's exit code to indicate that an error occurred. A
5165 program such as a shell script can detect this error code and react
5168 Sometimes it's helpful to include the last error message, which is stored in
5169 the special variable "$!". We'll see more about this in a moment.
5171 Obviously, "die" should only be used when something has prevented the program
5172 from doing whatever it was supposed to do. To end a program without an error,
5175 -----------------------------------
5177 3) Simple File Access
5179 Here's a simple program to copy one file to another:
5184 open MY_INPUT_FILE, "< in.txt" or die "Can't read in.txt: $!";
5185 open MY_OUTPUT_FILE, "> out.txt" or die "Can't write to out.txt: $!";
5187 while ( defined ( my $line = <MY_INPUT_FILE> ) ) {
5188 print MY_OUTPUT_FILE $line;
5191 close MY_INPUT_FILE;
5192 close MY_OUTPUT_FILE;
5194 Let's explain this from the top down.
5196 First, we can see that the "open" command in this example takes two parameters:
5197 a file handle and a file name. You'll notice that the file handle
5198 (MY_INPUT_FILE in this example) isn't a normal scalar because it doesn't begin
5199 with a dollar sign. (We didn't have to declare it with "my" either.) It also
5200 doesn't have quotes around it. Plain text like this that isn't surrounded by
5201 explanitory punctuation (like quotes) is called a "bareword". We'll be seeing
5202 barewords a little more in the future. We actually could have put quotes around
5203 it (in this "open" command at least), but by convention we don't.
5205 The "file handle" is simply a way of referring to a file once you've opened it.
5206 In C, file handles are integers; in Perl, they are text. By convention we write
5207 file handles in all CAPS.
5209 The file name in this case is "in.txt". The "<" in front of it means that the
5210 file is being opened in read mode (so we will be able to read from it but not
5211 write to it). The white space after the "<" won't cause any problems because
5212 "open" strips all white space from the beginning and end of the file name. As
5213 we will see later, you can use a command called "sysopen" if you want to open
5214 files that begin or end with whitespace characters.
5216 After opening the file, we check whether the "open" succeeds using "or die
5217 ...". Since Perl uses "short-circuit" boolean evaluation, the "die" command
5218 will only be executed if "open" returns false (failure). This means that if the
5219 file doesn't exist or can't be read, we'll know it right away. It's important
5220 to detect problems right away; otherwise we'll be scratching our heads
5221 wondering why the read command fails.
5223 The command to open the output file is just like the command to open the input
5224 file, except that it uses ">" instead of "<". If we wanted to append to the
5225 file instead of overwriting it, we would use ">>".
5227 In the next line, we can see that we can read from the file by surrounding its
5228 file handle with "<>". We've seen this before: remember "<STDIN>"? In that case
5229 the file handle was "STDIN", but the same syntax can be applied to any file
5232 The line containing "print" is a little different from any "print" statement
5233 we've seen before because it includes a file handle. This causes "print" to
5234 output the text to the file handle rather than standard output (STDOUT). Also
5235 note that there's no comma between the file handle and the text to be printed.
5237 Finally, we close both files. If we forget to close a file, Perl does a good
5238 job of cleaning up after our sloppiness, but it's still best to close the file
5241 Note that all built-in Perl functions/commands can be written with or without
5242 parentheses, so we could have written the above program like this:
5244 open(MY_INPUT_FILE, "< in.txt") or die "Can't read in.txt: $!";
5245 open(MY_OUTPUT_FILE, "> out.txt") or die "Can't write to out.txt: $!";
5247 while ( defined ( my $line = <MY_INPUT_FILE> ) ) {
5248 print(MY_OUTPUT_FILE $line);
5251 close(MY_INPUT_FILE);
5252 close(MY_OUTPUT_FILE);
5254 This has no effect whatsoever on the way the program functions. As usual, There
5255 Is More Than One Way To Do It. Just make sure you don't put a space between the
5256 function name and the parenthesis; otherwise Perl will interpret it as a list.
5258 As we've seen, a prefix (such as "<" or ">") in front of the file name
5259 indicates whether the file is for reading or writing. Here is a list of such
5264 >> write-only, appending to end of file
5265 +< read+write (if you're not sure which read+write to use, it's this)
5266 +> read+write, erasing the file when it's opened
5267 +>> read+write, appending data to the end of the file
5268 | execute command (also works as a suffix; more about this
5271 Additionally, a file that is just called "-" refers to standard input (if you
5272 read from it) or standard output (if you write to it). This is a convention
5273 used in many Unix commands.
5275 This begs the question: what do you do if you want to open a file that begins
5276 with one of these characters or with white space? Perl includes a more
5277 traditional command called "sysopen" which assumes that the file name you give
5278 it is exactly the file name you want. After opening the file, you can operate
5279 on the file handle exactly as if you had used "open" (i.e., there is no
5280 "sysclose"). We won't say anymore here because in practice "sysopen" is rarely
5281 used, but it is available if you want it. To learn more about it: "perldoc -f
5284 -----------------------------------
5288 One of the previous exercises (one solution to which is found below) was to
5289 "translate" the American "ize" into the British "ise". Modify this program to
5290 read its input from a file and write its output to another file.
5292 -----------------------------------
5294 5) Answers to Previous Exercises
5296 a) Here is a program that replaces the word "dead" with "metabolically
5302 while ( defined(my $line = <STDIN>) ) {
5303 $line =~ s/\bdead\b/metabolically different/g;
5307 b) Here is a very basic program that replaces "ize" with "ise":
5312 while ( defined(my $line = <STDIN>) ) {
5313 $line =~ s/ize\b/ise$1/g;
5317 Here is more advanced version that takes into account variations like
5318 "realised" and "desensitising". It also avoids changing the spelling of the
5319 words "size", "resize" and "downsize", which are spelled the same way in
5320 England and America:
5325 while ( defined(my $line = <STDIN>) ) {
5326 $line =~ s/iz(e|es|ing)\b/is$1/g;
5327 $line =~ s/\b(re|down)?sis(e|es|ing)\b/$1siz$2/g;
5331 -----------------------------------
5335 Part 1: Getting Started
5336 http://linuxchix.org/pipermail/courses/2003-March/001147.html
5339 http://linuxchix.org/pipermail/courses/2003-March/001153.html
5342 http://linuxchix.org/pipermail/courses/2003-April/001170.html
5344 Part 4: Control Structures
5345 http://linuxchix.org/pipermail/courses/2003-April/001184.html
5347 Part 4.5, a review with a little new information at the end:
5348 http://linuxchix.org/pipermail/courses/2003-July/001297.html
5350 Part 5: The "tr///" Operator
5351 http://linuxchix.org/pipermail/courses/2003-July/001302.html
5353 Part 6: The "m//" Operator
5354 http://linuxchix.org/pipermail/courses/2003-August/001305.html
5356 Part 7: More About "m//"
5357 http://linuxchix.org/pipermail/courses/2003-August/001322.html
5359 Part 8: The "s///" Operator
5360 http://linuxchix.org/pipermail/courses/2003-August/001330.html
5362 -----------------------------------
5368 b) "man perlopentut"
5369 c) Kirrily Robert, Paul Fenwick and Jacinta Richardson's "Intermedia Perl",
5370 which you can find (along with their "Introduction to Perl") at:
5371 http://www.perltraining.com.au/notes.html
5373 Thanks to Jacinta Richardson for fact checking.
5375 -----------------------------------
5379 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and Dan
5380 Richter, and is released under the same license as Perl itself (Artistic
5381 License or GPL, your choice). This is the license of choice to make it easy
5382 for other people to integrate your Perl code/documentation into their own
5383 projects. It is not generally used in projects unrelated to Perl.
5385 From daniel.richter@wimba.com Tue Sep 16 18:51:29 2003
5386 Return-Path: <daniel.richter@wimba.com>
5387 Delivered-To: courses@linuxchix.org
5388 Received: from localhost (localhost [127.0.0.1])
5389 by www.linuxchix.org (Postfix) with ESMTP id 064AA272D5C
5390 for <courses@linuxchix.org>; Tue, 16 Sep 2003 18:51:28 +1000 (EST)
5391 Received: from www.linuxchix.org ([127.0.0.1])
5392 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
5393 id 11515-10 for <courses@linuxchix.org>;
5394 Tue, 16 Sep 2003 18:51:28 +1000 (EST)
5395 Received: from sodium.wimba.fr (unknown [81.80.215.129])
5396 by www.linuxchix.org (Postfix) with ESMTP id DE62C272D41
5397 for <courses@linuxchix.org>; Tue, 16 Sep 2003 18:51:26 +1000 (EST)
5398 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
5399 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h8G8kqk17333
5400 for <courses@linuxchix.org>; Tue, 16 Sep 2003 10:46:52 +0200
5401 Message-Id: <5.1.0.14.1.20030916110553.02dc7eb0@mail.wimba.com>
5402 X-Sender: daniel@mail.wimba.com
5403 X-Mailer: QUALCOMM Windows Eudora Version 5.1
5404 Date: Tue, 16 Sep 2003 11:09:12 +0200
5405 To: courses@linuxchix.org
5406 From: Dan Richter <daniel.richter@wimba.com>
5407 Subject: Re: [Courses] [Perl] Part 9: Simple File Access
5408 In-Reply-To: <5.1.0.14.1.20030916102102.02dbed88@mail.wimba.com>
5410 Content-Type: text/plain; charset="us-ascii"; format=flowed
5411 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
5412 X-Topics: Perl programming course
5413 X-BeenThere: courses@linuxchix.org
5414 X-Mailman-Version: 2.1
5416 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
5417 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
5418 List-Post: <mailto:courses@linuxchix.org>
5419 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5420 <mailto:courses-request@linuxchix.org?subject=subscribe>
5421 List-Archive: <http://linuxchix.org/pipermail/courses>
5422 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5423 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
5424 X-List-Received-Date: Tue, 16 Sep 2003 08:51:29 -0000
5428 Oops: there's an error.
5430 >b) Here is a very basic program that replaces "ize" with "ise":
5432 > #!/usr/bin/perl -w
5435 > while ( defined(my $line = <STDIN>) ) {
5436 > $line =~ s/ize\b/ise$1/g;
5440 The "$1" in this program is an error; it should simply be removed.
5443 He doesn't have ulcers, but he's a carrier.
5446 From daniel.richter@wimba.com Wed Sep 24 19:20:21 2003
5447 Return-Path: <daniel.richter@wimba.com>
5448 Delivered-To: courses@linuxchix.org
5449 Received: from localhost (localhost [127.0.0.1])
5450 by www.linuxchix.org (Postfix) with ESMTP id 20E30272D5E
5451 for <courses@linuxchix.org>; Wed, 24 Sep 2003 19:20:21 +1000 (EST)
5452 Received: from www.linuxchix.org ([127.0.0.1])
5453 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
5454 id 19434-04 for <courses@linuxchix.org>;
5455 Wed, 24 Sep 2003 19:20:20 +1000 (EST)
5456 Received: from sodium.wimba.fr (unknown [81.80.215.129])
5457 by www.linuxchix.org (Postfix) with ESMTP id B9552272D5B
5458 for <courses@linuxchix.org>; Wed, 24 Sep 2003 19:20:18 +1000 (EST)
5459 Received: from Curium.wimba.com (curium.wimba.fr [10.0.0.42])
5460 by sodium.wimba.fr (8.11.6/8.8.7) with ESMTP id h8O9FNk03845
5461 for <courses@linuxchix.org>; Wed, 24 Sep 2003 11:15:24 +0200
5462 Message-Id: <5.1.0.14.1.20030924111938.0299d468@mail.wimba.com>
5463 X-Sender: daniel@mail.wimba.com
5464 X-Mailer: QUALCOMM Windows Eudora Version 5.1
5465 Date: Wed, 24 Sep 2003 11:38:39 +0200
5466 To: courses@linuxchix.org
5467 From: Dan Richter <daniel.richter@wimba.com>
5468 Subject: [Courses] [Perl] Part 10: Executing Commands with "open"
5470 Content-Type: text/plain; charset="us-ascii"; format=flowed
5471 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
5472 X-Topics: Perl programming course
5473 X-BeenThere: courses@linuxchix.org
5474 X-Mailman-Version: 2.1
5476 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
5477 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
5478 List-Post: <mailto:courses@linuxchix.org>
5479 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5480 <mailto:courses-request@linuxchix.org?subject=subscribe>
5481 List-Archive: <http://linuxchix.org/pipermail/courses>
5482 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5483 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
5484 X-List-Received-Date: Wed, 24 Sep 2003 09:20:21 -0000
5485 Content-Length: 9779
5488 LinuxChix Perl Course Part 10: Executing Commands with "open"
5491 2) Accessing Command-Line Arguments
5492 3) Executing Programs with "open"
5493 4) When Not to Use "open"
5494 5) A Simple Safety Rule
5496 7) Answer to Previous Exercise
5501 -----------------------------------
5505 Last week we saw how to read from and write to files using the "open" command.
5506 It turns out that "open" can also be used to execute programs and read their
5507 output, or even send output to them. Part 10 explains how.
5509 -----------------------------------
5511 2) Accessing Command-Line Arguments
5513 All of this working with "open" is making my head hurt, so let's start out with
5514 a minor detour into something much easier.
5516 Command-line arguments are passed to your program on the command line (hence
5517 the name "command-line arguments"). Suppose that you have a Perl program called
5518 "yourscript" and you execute it like this:
5520 yourscript arg1 arg2 arg3
5522 You could also execute it like this:
5524 perl yourscript arg1 arg2 arg3
5526 In either case, the "arg" parameters are command-line arguments.
5528 Your program can access its first command-line argument by calling the "shift"
5529 function. It can access the second command-line argument by calling "shift"
5530 again, and so on. When there are no more parameters to return, "shift" returns
5531 undefined. (The name "shift" comes from a shell script command with the same
5532 name and a similar purpose.)
5534 Try this program. Don't forget to pass command-line arguments, or it won't do
5540 while ( defined( my $person = shift ) ) {
5541 print "Hi, $person!\n";
5544 Note that we don't have to "chomp" command-line arguments because the operating
5545 system removes the whitespace around them (unless of course the user explicitly
5546 chooses to put whitespace around them).
5548 Some people are surprised to learn that you CANNOT access command-line
5549 arguments as $1, $2, etc. That's what you would do in a shell script, but in
5550 Perl these variables are reserved for regular expression matching.
5551 Interestingly enough, however, the variable $0 has a meaning in Perl similar to
5552 its meaning in shell scripts.
5554 By the way, command-line arguments can also be accessed via the array "@ARGV".
5555 But since we haven't discussed arrays yet, we won't get into that.
5557 -----------------------------------
5559 3) Executing Programs with "open"
5561 In addition to what we saw last week, the "open" command has one more very
5562 powerful application: it allows you to execute a command, send input and
5565 Try this program (it only works on Unix):
5570 open DATA, "who |" or die "Couldn't execute program: $!";
5571 while ( defined( my $line = <DATA> ) ) {
5577 Here's what happened: Perl saw that your "file" ended with a "pipe" (vertical
5578 bar) character. So it interpreted the "file" as a command to be executed, and
5579 interpreted the command's output as the "file"'s contents. The command is
5580 "who" (which prints information on currently logged-in users). If you execute
5581 that command, you will see that the output is exactly what the Perl program
5584 In this case, we "read" data from the command. To execute a command that we can
5585 "write" (send data) to, we should place a pipe character BEFORE the command.
5586 These options are mutually exclusive: we can read from a command or write to
5589 In the Unix world, a lot can be done by piping the output of one program into
5590 the input of another. Perl continues this spirit.
5592 Note that we can also send command-line parameters to the command, like this:
5594 open DATA, "who -H |" or die "Couldn't execute program: $!";
5596 In fact, Perl allows you to use "open" to do pretty much anything you would
5597 normally do on the command-line, as this example demonstrates:
5599 open OUTPUT, "| grep 'foo' > result.txt" or die "Failure: $!";
5601 We can then write whatever we want to the "OUTPUT" filehandle. The Unix "grep"
5602 command will filter out any text which doesn't contain the text "foo"; any text
5603 which DOES contain "foo" will be written to "result.txt".
5605 -----------------------------------
5607 4) When Not to Use "open"
5609 Don't overuse "open". This command should only be used when you need to pipe a
5610 lot of data to or from a program.
5612 For programs that give no output (or if you don't mind the output to going to
5613 the screen), use the "system" command:
5615 system "tar cf myArchive.tar *.c"; # Run "tar" to create an archive.
5617 Note: "system" breaks the convention of returning true if the program succeeds.
5618 Instead, it returns the exit code of the program plus some additional
5619 information, or -1 if the program couldn't be executed at all. You can count on
5620 "system" returning zero if the command succeeded, nonzero otherwise.
5622 And for programs that only return a few lines of output, use back-ticks:
5624 my $username = `whoami` or die "Couldn't execute command: $!";
5626 print "My name is $username\n";
5628 You can also use the "qx//" operator, which is exactly equivalent to back-ticks
5629 except that it allows you to choose your delimiter:
5631 # All of the following are exactly equivalent
5632 # to the above command that uses back-ticks.
5633 my $username = qx/whoami/ or die "Couldn't execute command: $!";
5634 my $username = qx(whoami) or die "Couldn't execute command: $!";
5635 my $username = qx#whoami# or die "Couldn't execute command: $!";
5637 -----------------------------------
5639 5) A Simple Safety Rule
5641 If you use the "open" command but don't specify whether you are opening for
5642 reading, writing or piping, Perl will assume that you are opening for reading.
5643 But DON'T DO THAT! Here's an example of why.
5645 One of the common uses of Perl is CGI (web) scripting. As the Perl master in
5646 your company, suppose you write a nice little dynamic web page that allows the
5647 user to choose a department, then generates the list of the people in that
5648 department. You write it like this:
5650 open DEPARTMENT_DIRECTORY, "/usr/departments/$department" or die "...";
5651 # Process and output the data.
5652 close DEPARTMENT_DIRECTORY;
5654 Well, one day someone will say, "hey, what happens if I enter
5655 '../../bin/rm -fr / |'?"
5657 Of course, what happens is that the command will do this:
5659 open DEPARTMENT_DIRECTORY, "/usr/departments/../../bin/rm -fr / |";
5661 The pipe (vertical bar) at the end of the file name means that it's a command
5662 to be executed. In fact, it's that famous command that wipes out everything on
5665 But this problem is really, really easy to avoid: just put a "<" on the front
5666 of the file you want to read. This will cause the "open" to fail if someone
5667 tries to trick your script into doing something other than reading.
5669 You have been warned.
5671 -----------------------------------
5675 Some Unix systems include a "pidof" command, which outputs the PID(s)
5676 associated with a currently running program:
5678 [me@mybox]$ pidof bash
5682 In the above example, there are two "bash" processes running, one with PID 1032
5683 and one with PID 1059.
5685 Write your own "pidof" program using Perl. Use the Unix command "ps -e" to read
5686 all of the processes that are currently running.
5688 -----------------------------------
5690 7) Answer to Previous Exercise
5692 Here is a program that reads a file (american.txt), "translates" it from
5693 American to British spelling (at least partly), and writes the result to
5694 another file (british.txt).
5699 open AMERICAN, '< american.txt' or die "Couldn't open file: $!";
5700 open BRITISH, '> british.txt' or die "Couldn't open file: $!";
5702 while ( defined(my $line = <AMERICAN>) ) {
5703 $line =~ s/iz(e|es|ing)\b/is$1/g;
5704 print BRITISH $line;
5710 -----------------------------------
5714 Part 1: Getting Started
5715 http://linuxchix.org/pipermail/courses/2003-March/001147.html
5718 http://linuxchix.org/pipermail/courses/2003-March/001153.html
5721 http://linuxchix.org/pipermail/courses/2003-April/001170.html
5723 Part 4: Control Structures
5724 http://linuxchix.org/pipermail/courses/2003-April/001184.html
5726 Part 4.5, a review with a little new information at the end:
5727 http://linuxchix.org/pipermail/courses/2003-July/001297.html
5729 Part 5: The "tr///" Operator
5730 http://linuxchix.org/pipermail/courses/2003-July/001302.html
5732 Part 6: The "m//" Operator
5733 http://linuxchix.org/pipermail/courses/2003-August/001305.html
5735 Part 7: More About "m//"
5736 http://linuxchix.org/pipermail/courses/2003-August/001322.html
5738 Part 8: The "s///" Operator
5739 http://linuxchix.org/pipermail/courses/2003-August/001330.html
5741 Part 9: Simple File Access
5742 http://linuxchix.org/pipermail/courses/2003-September/001340.html
5744 -----------------------------------
5750 b) "man perlopentut"
5751 c) Kirrily Robert, Paul Fenwick and Jacinta Richardson's "Intermediate Perl",
5752 which you can find (along with their "Introduction to Perl") at:
5753 http://www.perltraining.com.au/notes.html
5755 Thanks to Jacinta Richardson for fact checking.
5757 -----------------------------------
5761 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and Dan
5762 Richter, and is released under the same license as Perl itself (Artistic
5763 License or GPL, your choice). This is the license of choice to make it easy
5764 for other people to integrate your Perl code/documentation into their own
5765 projects. It is not generally used in projects unrelated to Perl.
5768 From daniel.richter@wimba.com Thu Oct 2 21:46:31 2003
5769 Return-Path: <daniel.richter@wimba.com>
5770 Delivered-To: courses@linuxchix.org
5771 Received: from localhost (localhost [127.0.0.1])
5772 by www.linuxchix.org (Postfix) with ESMTP id AAC22272D58
5773 for <courses@linuxchix.org>; Thu, 2 Oct 2003 21:46:31 +1000 (EST)
5774 Received: from www.linuxchix.org ([127.0.0.1])
5775 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
5776 id 02029-10 for <courses@linuxchix.org>;
5777 Thu, 2 Oct 2003 21:46:31 +1000 (EST)
5778 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
5779 by www.linuxchix.org (Postfix) with ESMTP id 68DDC272D1D
5780 for <courses@linuxchix.org>; Thu, 2 Oct 2003 21:46:24 +1000 (EST)
5781 Received: from wimba.com (hans.essi.fr [157.169.1.42])
5782 by email.essi.fr (Postfix) with ESMTP id 97A881C15C
5783 for <courses@linuxchix.org>; Thu, 2 Oct 2003 13:44:01 +0200 (CEST)
5784 Message-ID: <3F7C0FEC.2030204@wimba.com>
5785 Date: Thu, 02 Oct 2003 13:45:48 +0200
5786 From: Dan Richter <daniel.richter@wimba.com>
5787 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
5788 X-Accept-Language: en-us
5790 To: courses@linuxchix.org
5791 Subject: [Courses] [Perl] Part 11: Perl Variables
5792 Content-Type: text/plain; charset=us-ascii; format=flowed
5793 Content-Transfer-Encoding: 7bit
5794 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
5795 X-Topics: Perl programming course
5796 X-BeenThere: courses@linuxchix.org
5797 X-Mailman-Version: 2.1
5799 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
5800 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
5801 List-Post: <mailto:courses@linuxchix.org>
5802 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5803 <mailto:courses-request@linuxchix.org?subject=subscribe>
5804 List-Archive: <http://linuxchix.org/pipermail/courses>
5805 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
5806 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
5807 X-List-Received-Date: Thu, 02 Oct 2003 11:46:32 -0000
5808 Content-Length: 7126
5811 LinuxChix Perl Course Part 11: Perl Variables
5813 1) Weird Names for Variables
5815 3) The Special Variable "$_"
5817 5) Answer to Previous Exercise
5822 -----------------------------------
5824 1) Weird Names for Variables
5826 Perl has lots of built-in variables with odd names. We've already seen
5827 two of them: "$/" and "$!". But there are plenty more.
5829 For example, "$<" and "$>" are the current and effective user ID,
5833 print "Effective UID = $>\n";
5835 The variables "$(" and "$)" represent your real and effective group IDs,
5836 but they are more complicated.
5838 Assigning to these variables will change the user or group under which
5839 the process runs (within the limits of what your operating system allows
5840 of course: trying to set "$<" to zero won't necessarily make you root).
5841 But this could lead to problems with security and cross-platform
5842 compatibility that are too complicated to go into here, so do a little
5843 research before actually trying it.
5845 The variables "$?" (result of executing a program) and "$$" (PID of
5846 current process) have a similar meaning to their meaning in certain
5847 shells, such as bash (the Bourne-Again Shell).
5849 The variable "$," is easy to remember because its content is output
5850 every time you see a comma in a print statement:
5852 my($a,$b,$c) = ('foo', 'bar', 'baz');
5853 print $a, $b, $c, "\n"; # Output is: "foobarbaz"
5855 print $a, $b, $c, "\n"; # Output is: "foo##bar##baz##";
5857 For the full list of weird Perl variables, see "man perlvar". Don't
5858 worry about memorising all of them; you'll learn the most important ones
5861 All special Perl variables are global, which means that setting them
5862 introduces potential side effects. We will see more about that next week.
5864 -----------------------------------
5868 No, this isn't an anglo-centric statement; it's a Perl command.
5870 All special variables have names that are (arguably) more intuitive. For
5871 example, "$," can also be called "$OUTPUT_FIELD_SEPARATOR" or
5872 "$OFS". However, to use these names you must "use English;".
5878 $OUTPUT_FIELD_SEPARATOR = '|'; # Equivalent to: $, = '|';
5879 print 'field 1', 'field 2';
5882 However, due to an oversight in its implementation, "use English" makes
5883 regular expression matches slower. So you are welcome to "use English"
5884 right now if it helps you, but it's not recommended for writing Perl
5887 -----------------------------------
5889 3) The Special Variable "$_"
5891 Probably the most used variable in Perl is "$_", pronounced
5892 "dollar-underscore" and called "$ARG" if you "use English". This
5893 variable is passed as an understood parameter to many functions and
5894 operators if an expected parameter is not explicitly passed. Examples:
5896 chomp; # Equivalent to chomp($_);
5897 print; # Equivalent to print $_;
5899 "$_" is the understood subject of "tr///", "m//" and "s///":
5901 s/foo/bar/; # Equivalent to $_ =~ s/foo/bar/;
5902 if ( /baz/ ) { ... } # Equivalent to $_ ~= m/baz/
5904 In addition, for any file handle "HANDLE", the following are equivalent:
5906 while ( <HANDLE> ) { ... }
5907 # ... is equivalent to ...
5908 while ( defined( $_ = <HANDLE> ) ) { ... }
5910 Caution: this trick only works if "<HANDLE>" is used as the sole
5911 criterion of a while loop. So the following do NOT set "$_":
5913 <HANDLE>; # NOT equivalent to $_ = <HANDLE>;
5914 if ( <HANDLE> ) { ... } # NOT $_ = <HANDLE>
5916 The classic example use of "$_" is a program like the following:
5918 while ( <STDIN> ) { # Read line of input.
5919 s/puppy/dog/g; # Change "puppy" to "dog".
5920 s/kitten/cat/g; # Change "kitten" to "cat".
5921 print; # Output the line, with changes.
5924 Despite the "magic" nature of "$_", it can still be assigned to or from
5925 just like any other variable. (This is handy for testing.)
5930 We will see more uses of "$_" with time.
5932 -----------------------------------
5936 One previous exercise was to write a program that converted the word
5937 "dead" to "metabolically different". Here was my solution to this problem:
5942 while ( defined(my $line = <STDIN>) ) {
5943 $line =~ s/\bdead\b/metabolically different/g;
5947 Modify this program (or your own if you prefer) so that it uses no
5948 variables except "STDIN" and "$_", and try to avoid explicitly naming "$_".
5950 -----------------------------------
5953 5) Answer to Previous Exercise
5955 Here is my "pidof" program.
5960 my $program = shift; # Program to search for.
5962 unless ( defined($program) ) {
5963 die "Please give the name of a program on the command-line.\n";
5966 open PS, "ps -e |" or die "Couldn't execute 'ps': $!";
5967 while ( my $line = <PS> ) {
5969 # PID TTY Time Program
5970 if ( $line =~ /^\s*(\d+)\s+\S+\s+\S+\s+(\S+)\s*$/ ) {
5971 print "$1\n" if $2 eq $program;
5976 -----------------------------------
5980 Part 1: Getting Started
5981 http://linuxchix.org/pipermail/courses/2003-March/001147.html
5984 http://linuxchix.org/pipermail/courses/2003-March/001153.html
5987 http://linuxchix.org/pipermail/courses/2003-April/001170.html
5989 Part 4: Control Structures
5990 http://linuxchix.org/pipermail/courses/2003-April/001184.html
5992 Part 4.5, a review with a little new information at the end:
5993 http://linuxchix.org/pipermail/courses/2003-July/001297.html
5995 Part 5: The "tr///" Operator
5996 http://linuxchix.org/pipermail/courses/2003-July/001302.html
5998 Part 6: The "m//" Operator
5999 http://linuxchix.org/pipermail/courses/2003-August/001305.html
6001 Part 7: More About "m//"
6002 http://linuxchix.org/pipermail/courses/2003-August/001322.html
6004 Part 8: The "s///" Operator
6005 http://linuxchix.org/pipermail/courses/2003-August/001330.html
6007 Part 9: Simple File Access
6008 http://linuxchix.org/pipermail/courses/2003-September/001340.html
6011 http://linuxchix.org/pipermail/courses/2003-September/001344.html
6013 -----------------------------------
6019 b) Kirrily Robert, Paul Fenwick and Jacinta Richardson's "Intermediate
6021 which you can find (along with their "Introduction to Perl") at:
6022 http://www.perltraining.com.au/notes.html
6024 Thanks to Jacinta Richardson for fact checking.
6026 -----------------------------------
6030 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
6031 Dan Richter, and is released under the same license as Perl itself
6032 (Artistic License or GPL, your choice). This is the license of choice to
6033 make it easy for other people to integrate your Perl code/documentation
6034 into their own projects. It is not generally used in projects unrelated
6038 From daniel.richter@wimba.com Thu Oct 9 21:38:26 2003
6039 Return-Path: <daniel.richter@wimba.com>
6040 Delivered-To: courses@linuxchix.org
6041 Received: from localhost (localhost [127.0.0.1])
6042 by www.linuxchix.org (Postfix) with ESMTP id B2AE0272DBF
6043 for <courses@linuxchix.org>; Thu, 9 Oct 2003 21:38:26 +1000 (EST)
6044 Received: from www.linuxchix.org ([127.0.0.1])
6045 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
6046 id 20786-05 for <courses@linuxchix.org>;
6047 Thu, 9 Oct 2003 21:38:26 +1000 (EST)
6048 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
6049 by www.linuxchix.org (Postfix) with ESMTP id ED1B3272D9F
6050 for <courses@linuxchix.org>; Thu, 9 Oct 2003 21:38:22 +1000 (EST)
6051 Received: from wimba.com (vers.essi.fr [157.169.1.70])
6052 by email.essi.fr (Postfix) with ESMTP id 9DB4D1C2F5
6053 for <courses@linuxchix.org>; Thu, 9 Oct 2003 13:35:35 +0200 (CEST)
6054 Message-ID: <3F85489E.1070108@wimba.com>
6055 Date: Thu, 09 Oct 2003 13:38:06 +0200
6056 From: Dan Richter <daniel.richter@wimba.com>
6057 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
6058 X-Accept-Language: en-us
6060 To: courses@linuxchix.org
6061 Subject: [Courses] [Perl] Part 12: Side Effects with Perl Variables
6062 Content-Type: text/plain; charset=us-ascii; format=flowed
6063 Content-Transfer-Encoding: 7bit
6064 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
6065 X-Topics: Perl programming course
6066 X-BeenThere: courses@linuxchix.org
6067 X-Mailman-Version: 2.1
6069 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
6070 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
6071 List-Post: <mailto:courses@linuxchix.org>
6072 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6073 <mailto:courses-request@linuxchix.org?subject=subscribe>
6074 List-Archive: <http://linuxchix.org/pipermail/courses>
6075 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6076 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
6077 X-List-Received-Date: Thu, 09 Oct 2003 11:38:27 -0000
6078 Content-Length: 8984
6081 LinuxChix Perl Course Part 12: Side Effects with Perl Variables
6087 5) Answer to Previous Exercise
6092 -----------------------------------
6096 Last week we saw how to use Perl's special variables. This week we are
6097 going to see an important caveat concerning these special variables.
6098 Developing good habits now will save us lots of debugging when we start
6099 writing larger programs.
6101 -----------------------------------
6105 Like most languages, Perl includes the concept of "scope". The scope of
6106 a variable is the part of the code in which it can be accessed.
6110 print "$foo\n"; # OK
6112 print "$foo\n"; # D'oh! Went out of scope.
6114 Of course, Perl will only warn us of this mistake if we "use strict".
6115 (Otherwise, it will just create a new variable named "$foo".)
6117 In Perl, the scope of a variable is determined (partly) by the depth of
6118 the "code block" in which it was declared. A "code block" is delimited
6119 by opening and closing braces. Note that you can create a code block
6120 without using keywords such as "if":
6123 { # Start inner code block.
6124 print "Before redefinition, foo = $foo\n";
6125 my $foo = 'xyz'; # New declaration in this code block.
6126 print "Inside code block, foo = $foo\n";
6128 print "Outside code block, foo = $foo\n";
6130 Try the above code. As you can see, the inner code block "inherits" the
6131 variables defined outside it. By contrast, variables declared inside the
6132 code block "vanish" when the code block is finished, even if they have
6133 the same names as variables declared outside the code block.
6135 -----------------------------------
6139 All Perl variables are global unless explicitly declared otherwise
6140 (e.g., with "my"). If a global variable "$foo" appears in several places
6141 in your program, it's the same "$foo", even if it's mentioned in a
6142 separate library. This can wreak all kinds of havoc because you might
6143 have used the name "$foo" without knowing that there was a "$foo" in
6144 some library that you happened to use. This is called a "side effect",
6145 and it's usually not wanted. This is why you should always declare your
6146 variables with "my", which makes them private rather than global.
6148 What about the special variables we talked about last week? As we have
6149 seen, they change the workings of Perl functions and operators. (For
6150 example, "$/" changes the way "<STDIN>" breaks "lines".) This is an
6151 expected and desired side effect.
6153 But when we start using functions (other than those built into the
6154 language), we may run into situations where one part of the code doesn't
6155 anticipate the fact that another part of the code changed a global
6156 variable. Here is an example:
6158 # Declare function "home_dir". Takes a
6159 # username and returns its home directory.
6161 open PASSWD, '< /etc/passwd' or die "Uh oh: $!";
6162 $/ = ':'; # Records separated by colons.
6169 # Read usernames (one per line).
6170 open FILE, '< somefile.txt' or die "NO!!!";
6171 while ( my $username = <FILE> ) {
6173 my $home = home_dir($username);
6174 print "$username: $home\n";
6178 Because "$/" is global, the "$/" in the function is the same as the "$/"
6179 in the main body. So setting "$/" in the function changes the way
6180 "<FILE>" operates in the main body. This type of bug is difficult to
6183 We cannot solve the problem by declairing "$/" with "my" because we want
6184 it to have some side effects (besides, Perl will yell at you if you
6185 try). So we declare it as "local". Variables declared with "local" can
6186 be accessed by functions called within the code block, but after the
6187 code block is closed (with a "}") the change ceases to take effect. You
6188 should usually use "local" when you change a special Perl variable:
6190 # Declare function "home_dir".
6192 open PASSWD, '< /etc/passwd' or die "Uh oh: $!";
6193 local $/ = ':'; # Declare variable and set value.
6198 But there's an even more subtle trap here. If we had changed the
6199 variable in the main body and not the function, the change would have
6200 been reflected in the function even if it was declared as local in the
6203 # Stupid function; just for demonstration.
6211 print read_line() . "\n";
6213 In this example, setting "$/" has an effect on "read_line" even though
6214 "$/" was declared as "local". It's important that you understand the
6215 difference between this example and the previous one. One way to guard
6216 against this later problem is to enclose the changed variable in a code
6220 { # Introduce code block; scope is reduced to this block.
6223 } # End of block. Local $/ goes out of scope.
6224 print read_line() . "\n";
6226 But this implies knowing that "read_line" would be affected by your
6227 changed variable. Are you supposed to check the implementation of every
6228 function you call to see if it uses any built-in Perl variables? Of
6229 course not! Instead, the rule is: as much as possible, you should not
6230 call ANY function when you have changed a special variable, unless you
6231 intended the change to affect that function. The correlary to that rule
6232 is: make the code block containing the changed variable as small as
6235 It's especially important to declare "$_" as local in functions, to
6238 # Change employee list to keep up with people quitting.
6239 while ( <IN_FILE> ) {
6240 my $quit_person = GetPersonWhoQuit(); # Did this change "$_"?
6241 s/$quit_person//g; # Uh oh: where did the line of input go?
6242 print OUT_FILE; # I wonder what we just output!
6245 Of course, you should use your common sense here. For example, when you
6246 change "$<" (user ID), you probably want the change to be permanent and
6247 global. Likewise, functions should definitely declare "$_" as local, but
6248 declaring "$!" (last error) as local is probably less important because
6249 that variable is expected to change - indeed, the caller might use "$!"
6250 to understand why the function failed. Think carefully about the
6251 expected behavior, but when in doubt, avoid side effects.
6253 You have been warned!
6255 -----------------------------------
6259 There's no exercise this week.
6261 -----------------------------------
6263 5) Answer to Previous Exercise
6265 Here is a previous program which does not explicitly name any variable.
6271 s/\bdead\b/metabolically different/g;
6275 -----------------------------------
6279 Part 1: Getting Started
6280 http://linuxchix.org/pipermail/courses/2003-March/001147.html
6283 http://linuxchix.org/pipermail/courses/2003-March/001153.html
6286 http://linuxchix.org/pipermail/courses/2003-April/001170.html
6288 Part 4: Control Structures
6289 http://linuxchix.org/pipermail/courses/2003-April/001184.html
6291 Part 4.5, a review with a little new information at the end:
6292 http://linuxchix.org/pipermail/courses/2003-July/001297.html
6294 Part 5: The "tr///" Operator
6295 http://linuxchix.org/pipermail/courses/2003-July/001302.html
6297 Part 6: The "m//" Operator
6298 http://linuxchix.org/pipermail/courses/2003-August/001305.html
6300 Part 7: More About "m//"
6301 http://linuxchix.org/pipermail/courses/2003-August/001322.html
6303 Part 8: The "s///" Operator
6304 http://linuxchix.org/pipermail/courses/2003-August/001330.html
6306 Part 9: Simple File Access
6307 http://linuxchix.org/pipermail/courses/2003-September/001340.html
6309 Part 10: Executing Commands with "open"
6310 http://linuxchix.org/pipermail/courses/2003-September/001344.html
6312 Part 11: Perl Variables
6313 http://linuxchix.org/pipermail/courses/2003-October/001345.html
6315 -----------------------------------
6321 b) Kirrily Robert, Paul Fenwick and Jacinta Richardson's
6322 "Intermediate Perl", which you can find (along with their
6323 "Introduction to Perl") at:
6324 http://www.perltraining.com.au/notes.html
6326 Thanks to Jacinta Richardson for fact checking.
6328 -----------------------------------
6332 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
6333 Dan Richter, and is released under the same license as Perl itself
6334 (Artistic License or GPL, your choice). This is the license of choice to
6335 make it easy for other people to integrate your Perl code/documentation
6336 into their own projects. It is not generally used in projects unrelated
6340 From daniel.richter@wimba.com Fri Oct 17 16:47:55 2003
6341 Return-Path: <daniel.richter@wimba.com>
6342 Delivered-To: courses@linuxchix.org
6343 Received: from localhost (localhost [127.0.0.1])
6344 by www.linuxchix.org (Postfix) with ESMTP id 89C61272D47
6345 for <courses@linuxchix.org>; Fri, 17 Oct 2003 16:47:55 +1000 (EST)
6346 Received: from www.linuxchix.org ([127.0.0.1])
6347 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
6348 id 19594-06 for <courses@linuxchix.org>;
6349 Fri, 17 Oct 2003 16:47:55 +1000 (EST)
6350 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
6351 by www.linuxchix.org (Postfix) with ESMTP id CF7A8272CD1
6352 for <courses@linuxchix.org>; Fri, 17 Oct 2003 16:47:53 +1000 (EST)
6353 Received: from wimba.com (gidy.essi.fr [157.169.2.31])
6354 by email.essi.fr (Postfix) with ESMTP id 4E6F11C3D3
6355 for <courses@linuxchix.org>; Fri, 17 Oct 2003 08:43:58 +0200 (CEST)
6356 Message-ID: <3F8F9073.3050509@wimba.com>
6357 Date: Fri, 17 Oct 2003 08:47:15 +0200
6358 From: Dan Richter <daniel.richter@wimba.com>
6359 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
6360 X-Accept-Language: en-us
6362 To: courses@linuxchix.org
6363 Subject: [Courses] [Perl] Part 13: Perl Style
6364 Content-Type: text/plain; charset=us-ascii; format=flowed
6365 Content-Transfer-Encoding: 7bit
6366 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
6367 X-Topics: Perl programming course
6368 X-BeenThere: courses@linuxchix.org
6369 X-Mailman-Version: 2.1
6371 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
6372 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
6373 List-Post: <mailto:courses@linuxchix.org>
6374 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6375 <mailto:courses-request@linuxchix.org?subject=subscribe>
6376 List-Archive: <http://linuxchix.org/pipermail/courses>
6377 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6378 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
6379 X-List-Received-Date: Fri, 17 Oct 2003 06:47:55 -0000
6380 Content-Length: 8141
6383 LinuxChix Perl Course Part 13: Perl Style
6386 2) The Many Faces of Perl
6387 3) The Special File Handle "ARGV"
6389 5) [Non-]Answer to Previous Exercise
6394 -----------------------------------
6398 An important concept in Perl is that "There Is More Than One Way To Do
6399 It" (TIMTOWTDY, pronounced "Tim Towtdy"). We have seen this to some
6400 degree already. A simple example is "tr///" and "y///", which mean
6401 exactly the same thing. As another example, "unless" is the same as "if
6402 not". Likewise, "s///" can usually do the job of "tr///".
6404 This is one of the ways in which Perl is similar to human languages,
6405 which have many different words to express similar concepts (as a quick
6406 look through a thesaurus will demonstrate). Perl borrows other ideas
6407 from human languages as well. One simple example is the way "if" can be
6408 placed before or after the command to be conditionally executed. In
6409 addition, the implied use of "$_" is foreign to English speakers, but
6410 implied subjects are common in other languages such as Latin and Chinese.
6412 Larry Wall, who invented Perl, is fascinated with human languages. He
6413 even studied them in graduate school - and for an interesting reason:
6414 "At the time, [my wife and I] were actually planning to be missionaries
6415 (more specifically, Bible translators), but we had to drop that idea for
6416 health reasons." But Wall doesn't regret the change of plans: he figures
6417 that Perl is actually more useful to the missionaries than he personally
6420 Perl's similarity to a human language goes hand-in-hand with its ability
6421 to do many different types of jobs. As Jon Udell puts it, "[Larry] Wall
6422 [inventor of Perl] believes that people think about things in different
6423 ways, that natural languages accommodate many mindsets, and that
6424 programming languages should too."
6426 We're now going to see a bit of Perl's human-language-like versatility.
6428 -----------------------------------
6430 2) The Many Faces of Perl
6432 As we have seen, Perl commands can be written as functions or statements.
6434 die("Error!") unless defined($foo); # Look like functions.
6435 die "Error!" unless defined $foo; # Look like statements.
6437 This changes the appearance only: the functionality is exactly the same.
6439 More interestingly, the following code looks like it comes from a shell
6440 script, but it is actually valid Perl code:
6442 (-d $dir) || mkdir $dir; # Create directory if it doesn't exist.
6443 $size=`wc -c $dir/*`; # Get sizes of files.
6449 Yes, Perl borrows "-d", "mkdir" back-ticks and "here-documents" (the
6450 "<<EOF" part) from your favourite shell.
6452 Of course, the similarity is limited. Functions like "mkdir" are Perl
6453 built-ins; you cannot execute an arbitrary shell command directly from
6454 Perl. (That's why we had to use back-ticks to execute "wc".) In
6455 addition, you can't use "<", ">" and "|" to pipe I/O the way you would
6456 in a shell script (except when using "open", as we saw last week).
6458 But I still think that it's impressive that a Perl program can be
6459 written like C or like a shell script.
6461 -----------------------------------
6463 3) The Special File Handle "ARGV"
6465 Okay, I admit that this section isn't too related to Perl style, but I
6466 had to put it somewhere!
6468 "ARGV" is a special file handle which sequentially opens (for reading)
6469 all the files specified on the command-line (i.e., every command-line
6470 argument is interpreted as a file name and is opened). The files are
6471 read in the order in which they were specified on the command-line and
6472 as though the contents formed one big file (i.e., when you reach the end
6473 of a file, Perl automatically closes it and opens the next). The
6474 variable "$ARGV" always contains the name of the file that is currently
6475 being read by "<ARGV>".
6477 In practice, you never see "<ARGV>". Instead, you see it written as
6478 "<>", because "ARGV" is the default file handle for reading if none is
6481 For example, consider the following code:
6483 while ( <> ) { # while ( defined ( $_ = <ARGV> ) ) {
6487 If you store this code in a file named "test.pl" and run:
6489 perl test.pl file1 file2 file3
6491 the program will output the contents of "file1", then the contents of
6492 "file2", then the contents of "file3".
6494 A dash ("-") given as a command-line argument refers to standard input.
6495 Also, if no command-line arguments are given at all, standard input is
6498 -----------------------------------
6502 The Unix program "cat" outputs data to standard out according to its
6503 command-line arguments, as follows:
6504 1) If no command-line arguments are given, simply outputs standard input.
6505 2) A command-line argument of "-" also means to output standard input.
6506 3) Other command-line arguments refer to files, which are read and output.
6508 The program is so called because it conCATenates data to standard output.
6510 You will notice that this behaviour is very, very similar to the
6511 behaviour of "<ARGV>". (In fact, it's exactly the same, except that
6512 "cat" interprets arguemnts beginning with a dash as switches, whereas
6513 "<ARGV>" interprets them as files.) That's why "<ARGV>" is helpful: it's
6514 a shortcut to typical Unix behaviour.
6516 Write a "polite cat" program that behaves like "cat", but makes harsh
6517 terms more polite. For example, you might replace "died" with "passed
6518 away". It's up to you exactly which terms you want to change. (You don't
6519 have to implement "cat"'s switches, of course.)
6521 Try to use as few characters as possible without sacrificing good style.
6523 -----------------------------------
6525 5) [Non-]Answer to Previous Exercise
6527 There was no exercise last week.
6529 -----------------------------------
6533 Part 1: Getting Started
6534 http://linuxchix.org/pipermail/courses/2003-March/001147.html
6537 http://linuxchix.org/pipermail/courses/2003-March/001153.html
6540 http://linuxchix.org/pipermail/courses/2003-April/001170.html
6542 Part 4: Control Structures
6543 http://linuxchix.org/pipermail/courses/2003-April/001184.html
6545 Part 4.5, a review with a little new information at the end:
6546 http://linuxchix.org/pipermail/courses/2003-July/001297.html
6548 Part 5: The "tr///" Operator
6549 http://linuxchix.org/pipermail/courses/2003-July/001302.html
6551 Part 6: The "m//" Operator
6552 http://linuxchix.org/pipermail/courses/2003-August/001305.html
6554 Part 7: More About "m//"
6555 http://linuxchix.org/pipermail/courses/2003-August/001322.html
6557 Part 8: The "s///" Operator
6558 http://linuxchix.org/pipermail/courses/2003-August/001330.html
6560 Part 9: Simple File Access
6561 http://linuxchix.org/pipermail/courses/2003-September/001340.html
6563 Part 10: Executing Commands with "open"
6564 http://linuxchix.org/pipermail/courses/2003-September/001344.html
6566 Part 11: Perl Variables
6567 http://linuxchix.org/pipermail/courses/2003-October/001345.html
6569 Part 12: Side Effects with Perl Variables
6570 http://linuxchix.org/pipermail/courses/2003-October/001347.html
6572 -----------------------------------
6577 a) Jon Udell, "A Perl Hacker in the Land of Python"
6578 which you can view here:
6579 http://www.byte.com/documents/BYT20000201S0001/
6580 or if you don't feel like registering with BYTE.com:
6581 http://www.my-opensource.org/lists/myoss/2000-02/msg00090.html
6582 b) Marjorie Richardson's interview of Larry Wall, found at:
6583 http://www.linuxjournal.com/article.php?sid=3394
6585 Thanks to Jacinta Richardson for fact checking.
6587 -----------------------------------
6591 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
6592 Dan Richter, and is released under the same license as Perl itself
6593 (Artistic License or GPL, your choice). This is the license of choice to
6594 make it easy for other people to integrate your Perl code/documentation
6595 into their own projects. It is not generally used in projects unrelated
6599 From daniel.richter@wimba.com Fri Oct 31 19:08:30 2003
6600 Return-Path: <daniel.richter@wimba.com>
6601 Delivered-To: courses@linuxchix.org
6602 Received: from localhost (localhost [127.0.0.1])
6603 by www.linuxchix.org (Postfix) with ESMTP id 30B34272DDA
6604 for <courses@linuxchix.org>; Fri, 31 Oct 2003 19:08:30 +1100 (EST)
6605 Received: from www.linuxchix.org ([127.0.0.1])
6606 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
6607 id 09387-01 for <courses@linuxchix.org>;
6608 Fri, 31 Oct 2003 19:08:29 +1100 (EST)
6609 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
6610 by www.linuxchix.org (Postfix) with ESMTP id 8539A272D3D
6611 for <courses@linuxchix.org>; Fri, 31 Oct 2003 19:08:28 +1100 (EST)
6612 Received: from wimba.com (very.essi.fr [157.169.1.72])
6613 by email.essi.fr (Postfix) with ESMTP id D62E81C515
6614 for <courses@linuxchix.org>; Fri, 31 Oct 2003 09:07:56 +0100 (CET)
6615 Message-ID: <3FA21872.2090409@wimba.com>
6616 Date: Fri, 31 Oct 2003 09:08:18 +0100
6617 From: Dan Richter <daniel.richter@wimba.com>
6618 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
6619 X-Accept-Language: en-us
6621 To: courses@linuxchix.org
6622 Content-Type: text/plain; charset=us-ascii; format=flowed
6623 Content-Transfer-Encoding: 7bit
6624 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
6625 X-Topics: Perl programming course
6626 Subject: [Courses] Courses] [Perl] Part 14: Arrays
6627 X-BeenThere: courses@linuxchix.org
6628 X-Mailman-Version: 2.1
6630 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
6631 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
6632 List-Post: <mailto:courses@linuxchix.org>
6633 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6634 <mailto:courses-request@linuxchix.org?subject=subscribe>
6635 List-Archive: <http://linuxchix.org/pipermail/courses>
6636 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6637 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
6638 X-List-Received-Date: Fri, 31 Oct 2003 08:08:30 -0000
6639 Content-Length: 8405
6642 LinuxChix Perl Course Part 14: Arrays
6645 2) Declaring an Array
6646 3) Accessing the Elements of an Array
6647 4) Assigning to an Array
6650 7) Answer to Previous Exercise
6655 -----------------------------------
6659 First of all, I want to apologise for not sending this to the list last
6660 week. I had to attend a funeral. :-(
6662 But anyway, let's talk about Perl...
6664 Up until now we have only looked at scalar variables. It's high time we
6665 had a look at arrays.
6667 Arrays in Perl are very powerful. This week we're only going to look at
6668 the basics, but don't get in the habit of "micro-managing" your arrays.
6669 Perl includes powerful macro-commands to do most of what you would
6670 logically want to do. We will see some of these commands in the
6673 -----------------------------------
6675 2) Declaring an Array
6677 Array variables are declared just like scalar variables, except that
6678 they are preceded by an at-sign rather than a dollar sign:
6682 Note that "@foo" and "$foo" are totally different variables. You can use
6683 them both in the same code block; they won't interfere with each other
6686 That wasn't so hard, was it?
6688 -----------------------------------
6690 3) Accessing the Elements of an Array
6692 Array elements are accessed using the standard "[]" operator - but with
6693 a twist. When a single element is accessed, the array name must be
6694 preceded by a dollar sign rather than an at-sign:
6696 my @list = ('some', 'words');
6697 print $list[0] . "\n";
6699 As "man perldata" puts it: "$foo[1] is a part of @foo, not a part of
6700 $foo. This may seem a bit weird, but that's okay, because it is weird."
6702 Don't let the humility of those words fool you: the system is actually
6703 quite clever when you fully understand it. The "[]" indicates that the
6704 variable is an array; the dollar sign indicates that what you want from
6705 it is a scalar. It may seem obvious that you would want a scalar, but
6706 Perl includes the ability to "slice" an array by specifying multiple
6709 my @chars = ('A', 'B', 'C', 'D', 'E', 'F');
6710 my @a = @chars[1,5]; # Gets B and F.
6711 my @b = @chars[1 .. 4]; # Gets B, C, D and E.
6712 my @c = @chars[1,3..5]; # Gets B, D, E and F.
6714 When you slice, you get back an array, so the variable name is preceded
6717 Note that array subscripts always start at zero. (You can change this by
6718 setting the special Perl variable "$[" - but that's a REALLY bad idea.)
6720 A negative subscript is interpreted as though it had been added to the
6721 length of the array, so negative numbers can thought of as "counting
6722 backwards from the end":
6724 my @chars = ('a', 'b', 'c', 'd', 'e', 'f');
6725 print "$chars[-2]\n"; # Prints "e".
6727 It's not an error to access a non-existant subscript; you just get back
6728 "undef" (the undefined value).
6730 -----------------------------------
6732 4) Assigning to an Array
6734 Perl provides many ways to assign to an array. One is a way we have
6737 my @x = ('foo', 'bar', 'baz');
6739 Sometimes it's easier to use the "qw//" (quote words) operator:
6741 my @x = qw/foo bar baz/; # Equivalent to the above.
6743 Note that the words in "qw//" should NOT be separated by commas.
6745 We can also assign one array to another:
6747 my @foo = qw{ The "qw//" operator can use any delimiter. };
6750 Note that the assignment is done by value, not by reference; subsequent
6751 changes to "@foo" will not affect "@bar".
6753 Another way to assign to an array is using subscripts:
6756 @y[1,4,9] = ('one squared', 'two squared', 'three squared');
6758 It's not an error to assign beyond the end of the array: the extra
6759 indices will be automatically created and assigned the value "undef":
6761 my @arr = qw/just three elements/;
6762 $arr[50] = 'xyz'; # No problem.
6764 Note that arrays in Perl can contain any scalar:
6766 my @foo = ('some text', 3.14, FILE_HANDLE);
6768 But an array CANNOT contain another array:
6770 $bar[3] = @foo; # Won't work as expected.
6771 @bar[3] = @foo; # Equivalent to: $bar[3] = $foo[0]
6773 However, an array can contain a REFERENCE to an array, because a
6774 reference is a scalar. We'll see more about this when we learn about
6777 -----------------------------------
6781 As you may recall, the following reads a single line from "FOO":
6785 By assigning to an array instead of a scalar, we can read ALL the lines
6788 @lines = <FOO>; # Read the whole file!
6790 Each element of "@lines" is one line of the file (usually with newline
6791 characters at the end; we have to "chomp" to remove them).
6793 Using "print" to output an array is exactly the same as calling "print"
6794 for each element of the array:
6798 Array subscripts are interpolated inside quotes:
6800 print "Line 2 is: $lines[2]"; # Works as expected.
6801 print "Line $i us: $lines[$i]"; # Works as expected.
6803 A whole array can also be interpolated inside quotes. In that case, the
6804 elements of the array are separated by whatever '$"' is set to (it's a
6807 print "The list is: @list\n"; # Output whole list.
6808 print "Parts of the list are: @list[2,$i..$j]\n"; # Works!
6810 -----------------------------------
6814 The Unix program "tail" outputs the last few lines of a file, or
6815 standard input if no file is given. Write your own "tail" program in Perl.
6817 Because we're just starting out with arrays here, we'll make three
6818 simplifying assumptions:
6819 1) We assume that the input will not have fewer lines than the number of
6820 lines you want to display.
6821 2) We do not allow the user to specify the number of lines to display on
6822 the command line (as real "tail" does).
6823 3) We indiscriminantly read the entire file into memory regardless of
6826 You will notice that we haven't discussed yet how to get the length of
6827 an array. That's okay: we don't need the length here. Use a slice with
6828 negative subscripts.
6830 -----------------------------------
6832 7) Answer to Previous Exercise
6834 Here is my "polite cat" program:
6840 s/\bzero\b/not too much/g;
6841 s/\bunemployed\b/at a carreer crossroads/g;
6845 -----------------------------------
6849 Part 1: Getting Started
6850 http://linuxchix.org/pipermail/courses/2003-March/001147.html
6853 http://linuxchix.org/pipermail/courses/2003-March/001153.html
6856 http://linuxchix.org/pipermail/courses/2003-April/001170.html
6858 Part 4: Control Structures
6859 http://linuxchix.org/pipermail/courses/2003-April/001184.html
6861 Part 4.5, a review with a little new information at the end:
6862 http://linuxchix.org/pipermail/courses/2003-July/001297.html
6864 Part 5: The "tr///" Operator
6865 http://linuxchix.org/pipermail/courses/2003-July/001302.html
6867 Part 6: The "m//" Operator
6868 http://linuxchix.org/pipermail/courses/2003-August/001305.html
6870 Part 7: More About "m//"
6871 http://linuxchix.org/pipermail/courses/2003-August/001322.html
6873 Part 8: The "s///" Operator
6874 http://linuxchix.org/pipermail/courses/2003-August/001330.html
6876 Part 9: Simple File Access
6877 http://linuxchix.org/pipermail/courses/2003-September/001340.html
6879 Part 10: Executing Commands with "open"
6880 http://linuxchix.org/pipermail/courses/2003-September/001344.html
6882 Part 11: Perl Variables
6883 http://linuxchix.org/pipermail/courses/2003-October/001345.html
6885 Part 12: Side Effects with Perl Variables
6886 http://linuxchix.org/pipermail/courses/2003-October/001347.html
6889 http://linuxchix.org/pipermail/courses/2003-October/001349.html
6891 -----------------------------------
6895 Works cited: man perldata
6897 Thanks to Jacinta Richardson for fact checking.
6899 -----------------------------------
6903 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
6904 Dan Richter, and is released under the same license as Perl itself
6905 (Artistic License or GPL, your choice). This is the license of choice to
6906 make it easy for other people to integrate your Perl code/documentation
6907 into their own projects. It is not generally used in projects unrelated
6911 From daniel.richter@wimba.com Thu Nov 6 22:59:05 2003
6912 Return-Path: <daniel.richter@wimba.com>
6913 Delivered-To: courses@linuxchix.org
6914 Received: from localhost (localhost [127.0.0.1])
6915 by www.linuxchix.org (Postfix) with ESMTP id E2441272E3F
6916 for <courses@linuxchix.org>; Thu, 6 Nov 2003 22:59:04 +1100 (EST)
6917 Received: from www.linuxchix.org ([127.0.0.1])
6918 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
6919 id 23277-09 for <courses@linuxchix.org>;
6920 Thu, 6 Nov 2003 22:59:04 +1100 (EST)
6921 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
6922 by www.linuxchix.org (Postfix) with ESMTP id 7F658272E26
6923 for <courses@linuxchix.org>; Thu, 6 Nov 2003 22:59:03 +1100 (EST)
6924 Received: from wimba.com (mont.essi.fr [157.169.2.24])
6925 by email.essi.fr (Postfix) with ESMTP id A11E01C360
6926 for <courses@linuxchix.org>; Thu, 6 Nov 2003 12:57:44 +0100 (CET)
6927 Message-ID: <3FAA3772.4080103@wimba.com>
6928 Date: Thu, 06 Nov 2003 12:58:42 +0100
6929 From: Dan Richter <daniel.richter@wimba.com>
6930 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
6931 X-Accept-Language: en-us
6933 To: courses@linuxchix.org
6934 Subject: [Courses] [Perl] Part 15: More About Lists
6935 Content-Type: text/plain; charset=us-ascii; format=flowed
6936 Content-Transfer-Encoding: 7bit
6937 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
6938 X-Topics: Perl programming course
6939 X-BeenThere: courses@linuxchix.org
6940 X-Mailman-Version: 2.1
6942 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
6943 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
6944 List-Post: <mailto:courses@linuxchix.org>
6945 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6946 <mailto:courses-request@linuxchix.org?subject=subscribe>
6947 List-Archive: <http://linuxchix.org/pipermail/courses>
6948 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
6949 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
6950 X-List-Received-Date: Thu, 06 Nov 2003 11:59:05 -0000
6951 Content-Length: 6976
6954 LinuxChix Perl Course Part 15: More About Lists
6958 3) The Length of an Array
6959 4) The "foreach" Loop
6961 6) Answer to Previous Exercise
6966 -----------------------------------
6970 Last week we saw the very basics of array variables in Perl. This week
6971 we're still looking at the basics, but slightly higher-level basics.
6973 By the way, last week I used the term "array", mainly because I thought
6974 it would be more familiar. The general term in Perl is "list". An array
6975 is actually a type of list: a named list. Of course, we can informally
6976 use the terms interchangeably.
6978 -----------------------------------
6982 As we saw last week, lists in Perl are "flat": that is, they must
6983 contain only scalars. This obviously makes certain complex data
6984 structures more difficult to represent, but it has the advantage of
6985 making lists easy to join and split. For example, the following will
6986 join three lists into one big list:
6988 my @everyone = (@men, @women, @children);
6990 Remember that arrays are always assigned by value, so subsequent changes
6991 to, say, "@women" will have no effect on "@everyone".
6993 Likewise, the following will insert "$new_guy" at position 5 of the
6994 array (pushing all the other elements back one, of course):
6996 @everyone = (@everyone[0..4], $new_guy, @everyone[5..-1]);
6998 And we can even use a list of variables as an lvalue (something to
6999 assign to). We've already seen that in statements like this:
7001 my($hour,$minute,$second) = ($time =~ m/(\d+):(\d+):(\d+)/);
7003 The "m//" operator returns a list, "$hour" is set to the first value of
7004 that list, "$minute" is set to the second value of the list, etc.
7006 Note that if the number of elements on the left and right sides of the
7007 assignment don't match, no error message is produced:
7009 ($a, $b) = ('one', 'two', 'three'); # Same as: $a='one'; $b='two';
7010 ($a, $b, $c) = ('one', 'two'); # Ditto, but sets $c to undef!
7012 If one of the lvalues is an array, the array sucks up all remaining values:
7014 ($first,$second,@rest) = @everyone;
7016 This means that it's a mistake (not an error, but surely a mistake) to
7017 follow an array with another variable in an assignment:
7019 (@rest,$last) = @everyone; # NO! Same as: @rest=@everyone; $last=undef
7020 (@a, @b) = qw/a list of words/; # Likewise not good.
7021 (@a, @b) = (@c, @d); # NOT the same as @a=@c; @b=@d;
7023 -----------------------------------
7025 3) The Length of an Array
7027 To find the length of an array, simply assign the array to a scalar
7030 my @arr = ('foo', 'bar', 'baz');
7032 print "Length is: $length\n";
7034 We'll see more about this next week.
7036 By the way, don't try this with a literal list:
7038 my $length_but_doesnt_work = ('foo', 'bar'); # Sets to "bar".
7040 I'm sorry; I should have mentioned this last week.
7042 -----------------------------------
7044 4) The "foreach" Loop
7046 To loop through the values of an array, you could use a "for" loop:
7048 my @foo = qw/one two three four/;
7049 for (my $i=0; $i<@foo; $i++) {
7053 However, it's usually easier to use a "foreach" loop:
7055 my @foo = qw/one two three four/;
7056 foreach my $value (@foo) {
7060 If no scalar variable is provided in the "foreach" statement, "$_" is used:
7062 my @foo = qw/one two three four/;
7067 In this case (and ONLY in this case), changes to "$_" affect the array:
7069 my @foo = qw/one two three four/;
7071 s/[aeiou]/X/g; # Replace vowels with "X".
7073 print "@foo\n"; # The array now has no vowels.
7075 We'll see a similar way to change the values of an array when we look at
7078 -----------------------------------
7082 a) Fix last week's "tail" program to take into account the possiblity
7083 that the user inputs a small number of lines. (See below.)
7085 b) The Perl function "reverse" reverses the order of an array. Write
7086 your own implementation of "reverse", i.e.:
7088 my @array = qw/A B C D E F G/;
7092 print "Result is: @result\n";
7094 Try to use "foreach" instead of "for".
7096 -----------------------------------
7098 6) Answer to Previous Exercise
7100 Here is an implementation of "tail" in Perl:
7105 my $lines_to_output = 10;
7108 print @lines[ -$lines_to_output .. -1 ];
7110 Again, this program is faulty because it assumes that there are at least
7111 ten lines of input. If there are less than ten lines, the Perl
7112 interpreter will complain because we're printing the undefined value
7115 It really was stupid of me to suggest that you write a buggy program.
7118 -----------------------------------
7122 Part 1: Getting Started
7123 http://linuxchix.org/pipermail/courses/2003-March/001147.html
7126 http://linuxchix.org/pipermail/courses/2003-March/001153.html
7129 http://linuxchix.org/pipermail/courses/2003-April/001170.html
7131 Part 4: Control Structures
7132 http://linuxchix.org/pipermail/courses/2003-April/001184.html
7134 Part 4.5, a review with a little new information at the end:
7135 http://linuxchix.org/pipermail/courses/2003-July/001297.html
7137 Part 5: The "tr///" Operator
7138 http://linuxchix.org/pipermail/courses/2003-July/001302.html
7140 Part 6: The "m//" Operator
7141 http://linuxchix.org/pipermail/courses/2003-August/001305.html
7143 Part 7: More About "m//"
7144 http://linuxchix.org/pipermail/courses/2003-August/001322.html
7146 Part 8: The "s///" Operator
7147 http://linuxchix.org/pipermail/courses/2003-August/001330.html
7149 Part 9: Simple File Access
7150 http://linuxchix.org/pipermail/courses/2003-September/001340.html
7152 Part 10: Executing Commands with "open"
7153 http://linuxchix.org/pipermail/courses/2003-September/001344.html
7155 Part 11: Perl Variables
7156 http://linuxchix.org/pipermail/courses/2003-October/001345.html
7158 Part 12: Side Effects with Perl Variables
7159 http://linuxchix.org/pipermail/courses/2003-October/001347.html
7162 http://linuxchix.org/pipermail/courses/2003-October/001349.html
7165 http://linuxchix.org/pipermail/courses/2003-October/001350.html
7167 -----------------------------------
7175 Thanks to Jacinta Richardson for fact checking.
7177 -----------------------------------
7181 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
7182 Dan Richter, and is released under the same license as Perl itself
7183 (Artistic License or GPL, your choice). This is the license of choice to
7184 make it easy for other people to integrate your Perl code/documentation
7185 into their own projects. It is not generally used in projects unrelated
7189 From ruifernandes@tvtel.pt Fri Nov 7 07:15:02 2003
7190 Return-Path: <ruifernandes@tvtel.pt>
7191 Delivered-To: courses@linuxchix.org
7192 Received: from localhost (localhost [127.0.0.1])
7193 by www.linuxchix.org (Postfix) with ESMTP id 5C61E272E64
7194 for <courses@linuxchix.org>; Fri, 7 Nov 2003 07:15:02 +1100 (EST)
7195 Received: from www.linuxchix.org ([127.0.0.1])
7196 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7197 id 28439-08 for <courses@linuxchix.org>;
7198 Fri, 7 Nov 2003 07:15:02 +1100 (EST)
7199 Received: from mail2.expernet.pt (unknown [195.23.74.2])
7200 by www.linuxchix.org (Postfix) with ESMTP id B5B5D272E23
7201 for <courses@linuxchix.org>; Fri, 7 Nov 2003 07:15:00 +1100 (EST)
7202 Received: from rui (rev-195-23-77-102.tvtel.pt [195.23.77.102] (may be
7204 by mail2.expernet.pt (8.12.8/8.11.4) with SMTP id hA6K8iXM011154
7205 for <courses@linuxchix.org>; Thu, 6 Nov 2003 20:08:45 GMT
7206 Message-ID: <007901c3a4a2$a1924200$664d17c3@rui>
7207 From: "Rui Fernandes" <ruifernandes@tvtel.pt>
7208 To: <courses@linuxchix.org>
7209 Subject: [Courses] [Perl] Part 8: The "s///" Operator
7210 Date: Thu, 6 Nov 2003 20:14:49 -0000
7213 X-MSMail-Priority: High
7214 X-Mailer: Microsoft Outlook Express 6.00.2800.1106
7215 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
7216 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
7217 X-Mailman-Approved-At: Sat, 08 Nov 2003 08:34:10 +1100
7218 Content-Type: text/plain;
7219 charset="iso-8859-1"
7220 Content-Transfer-Encoding: quoted-printable
7221 X-Content-Filtered-By: Mailman/MimeDel 2.1
7222 X-Topics: Perl programming course
7223 X-BeenThere: courses@linuxchix.org
7224 X-Mailman-Version: 2.1
7226 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7227 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7228 List-Post: <mailto:courses@linuxchix.org>
7229 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7230 <mailto:courses-request@linuxchix.org?subject=subscribe>
7231 List-Archive: <http://linuxchix.org/pipermail/courses>
7232 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7233 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7234 X-List-Received-Date: Thu, 06 Nov 2003 20:15:02 -0000
7240 I've got a problem with a substitution in Perl. I have a string ( let's =
7241 supose $word) that can have values like the followings - it's =
7246 word=BA =E7htn mnfbgd=20
7250 I want to extract only the character A to Z , a to z and 0 to 1 from the =
7251 strings so I could have:
7260 How can I do this if I don't know the number of letters in the string?
7261 Please answer as soon as possible - It's very urgent.
7266 From tbowen@CapitalThinking.com Sat Nov 8 09:00:30 2003
7267 Return-Path: <tbowen@CapitalThinking.com>
7268 Delivered-To: courses@linuxchix.org
7269 Received: from localhost (localhost [127.0.0.1])
7270 by www.linuxchix.org (Postfix) with ESMTP id A4A88272E03
7271 for <courses@linuxchix.org>; Sat, 8 Nov 2003 09:00:30 +1100 (EST)
7272 Received: from www.linuxchix.org ([127.0.0.1])
7273 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7274 id 10367-10 for <courses@linuxchix.org>;
7275 Sat, 8 Nov 2003 09:00:30 +1100 (EST)
7276 Received: from mail1.CapitalThinking.com (mail1.capitalthinking.com
7278 by www.linuxchix.org (Postfix) with ESMTP id 906C3272D20
7279 for <courses@linuxchix.org>; Sat, 8 Nov 2003 09:00:29 +1100 (EST)
7280 Received: from imail02.ct.capitalthinking.com ([216.52.89.16])
7282 for <courses@linuxchix.org>; Fri, 7 Nov 2003 17:00:21 -0500 (EST)
7283 content-class: urn:content-classes:message
7285 Content-Type: text/plain;
7286 charset="iso-8859-1"
7287 Content-Transfer-Encoding: quoted-printable
7288 Subject: RE: [Courses] [Perl] Part 8: The "s///" Operator
7289 X-MimeOLE: Produced By Microsoft Exchange V6.0.6375.0
7290 Date: Fri, 7 Nov 2003 17:00:14 -0500
7291 Message-ID: <7386B7F8D4854B41975B4DEA6286DA311A052E@imail02.ct.capitalthinking.com>
7293 X-MS-TNEF-Correlator:
7294 Thread-Topic: [Courses] [Perl] Part 8: The "s///" Operator
7295 Thread-Index: AcOld3ZXzoreKEF5Tv6j0zREbOZymAAAQS2w
7296 From: "Bowen, Tricia" <tbowen@CapitalThinking.com>
7297 To: <courses@linuxchix.org>
7298 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
7299 X-Topics: Perl programming course
7300 X-BeenThere: courses@linuxchix.org
7301 X-Mailman-Version: 2.1
7303 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7304 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7305 List-Post: <mailto:courses@linuxchix.org>
7306 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7307 <mailto:courses-request@linuxchix.org?subject=subscribe>
7308 List-Archive: <http://linuxchix.org/pipermail/courses>
7309 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7310 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7311 X-List-Received-Date: Fri, 07 Nov 2003 22:00:30 -0000
7312 Content-Length: 1125
7315 try running this on the command-line:
7317 perl -pi.bak -e 's/\W//g' test.txt
7319 \W is equivalent to anything other than a word character =
7322 test.txt is a sample file that contains your crazy characters.
7326 -----Original Message-----
7327 From: Rui Fernandes [mailto:ruifernandes@tvtel.pt]
7328 Sent: Thursday, November 06, 2003 3:15 PM
7329 To: courses@linuxchix.org
7330 Subject: [Courses] [Perl] Part 8: The "s///" Operator
7336 I've got a problem with a substitution in Perl. I have a string ( let's =
7337 supose $word) that can have values like the followings - it's =
7342 word=BA =E7htn mnfbgd=20
7346 I want to extract only the character A to Z , a to z and 0 to 1 from the =
7347 strings so I could have:
7356 How can I do this if I don't know the number of letters in the string?
7357 Please answer as soon as possible - It's very urgent.
7362 _______________________________________________
7363 Courses mailing list
7364 Courses@linuxchix.org
7365 http://mailman.linuxchix.org/mailman/listinfo/courses
7366 From daniel.richter@wimba.com Thu Nov 13 23:36:38 2003
7367 Return-Path: <daniel.richter@wimba.com>
7368 Delivered-To: courses@linuxchix.org
7369 Received: from localhost (localhost [127.0.0.1])
7370 by www.linuxchix.org (Postfix) with ESMTP id D9AC1272E11
7371 for <courses@linuxchix.org>; Thu, 13 Nov 2003 23:36:37 +1100 (EST)
7372 Received: from www.linuxchix.org ([127.0.0.1])
7373 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7374 id 04827-03 for <courses@linuxchix.org>;
7375 Thu, 13 Nov 2003 23:36:37 +1100 (EST)
7376 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
7377 by www.linuxchix.org (Postfix) with ESMTP id 89582272E07
7378 for <courses@linuxchix.org>; Thu, 13 Nov 2003 23:35:47 +1100 (EST)
7379 Received: from wimba.com (port.essi.fr [157.169.1.60])
7380 by email.essi.fr (Postfix) with ESMTP id 096F71C175
7381 for <courses@linuxchix.org>; Thu, 13 Nov 2003 13:32:51 +0100 (CET)
7382 Message-ID: <3FB37A54.30800@wimba.com>
7383 Date: Thu, 13 Nov 2003 13:34:28 +0100
7384 From: Dan Richter <daniel.richter@wimba.com>
7385 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
7386 X-Accept-Language: en-us
7388 To: courses@linuxchix.org
7389 Subject: [Courses] [Perl] Part 16: Array Functions
7390 Content-Type: text/plain; charset=us-ascii; format=flowed
7391 Content-Transfer-Encoding: 7bit
7392 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
7393 X-Topics: Perl programming course
7394 X-BeenThere: courses@linuxchix.org
7395 X-Mailman-Version: 2.1
7397 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7398 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7399 List-Post: <mailto:courses@linuxchix.org>
7400 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7401 <mailto:courses-request@linuxchix.org?subject=subscribe>
7402 List-Archive: <http://linuxchix.org/pipermail/courses>
7403 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7404 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7405 X-List-Received-Date: Thu, 13 Nov 2003 12:36:38 -0000
7406 Content-Length: 7528
7409 LinuxChix Perl Course Part 16: Array Functions
7412 2) Re-Visiting Old Perl Functions
7413 3) Six Simple Functions
7414 4) Consider the Context
7416 6) Answers to Previous Exercises
7421 -----------------------------------
7425 Perl includes many functions that make handling arrays a breeze.
7427 Some of these functions are quite versatile, i.e., you can change the
7428 way they work if you need a slightly different behaviour. We could never
7429 get through all the different customisations that can be done, but you
7430 can find out more about any one of these functions by typing
7431 "perldoc -f <function name>" at the command-line.
7433 -----------------------------------
7435 2) Re-Visiting Old Perl Functions
7437 Some Perl functions that we've already seen will accept either scalars
7438 or lists. For example, we've seen the "chomp" function used with
7439 scalars, but it can also be used with a list (in which case it chomps
7440 every element of the list):
7444 chomp($a, $list, $of, $scalars);
7446 But not all Perl functions can do this. For example, the "uc" function
7447 capitalises all the letters in a string, but it won't accept a list.
7448 Always RTFM to make sure.
7450 -----------------------------------
7452 3) Six Simple Functions
7454 We're going to start with
7456 a) split - splits a string into an array
7457 b) join - combines an array to form a string
7458 c) reverse - reverses the order of an array (first element becomes last)
7459 d) sort - puts the array in alphabetical order (NOT numerical order)
7460 e) push - adds one or more values to the end of an array
7461 f) pop - removes and returns the last element of an array
7465 my @arr = split( /[#!]+/, 'foo###bar!!!baz' );
7466 # Now @arr = ('foo', 'bar', 'baz')
7467 print join('**', @arr); # Output is: "foo**bar**baz"
7468 @arr = reverse(@arr); # Now @arr = ('baz', 'bar', 'foo')
7469 @arr = sort(@arr); # Now @arr = ('bar', 'baz', 'foo')
7470 my $x = pop(@arr); # Now $x='baz' and @arr=('foo', 'bar')
7471 push(@arr, 'Bob', 'Jane'); # Now: ('foo', 'bar', 'Bob', 'Jane')
7473 Note that only the functions "push" and "pop" change the array variable
7474 passed in. This means that the other functions can take any list, not
7477 my @arr = ('foo', 'bar', 'baz');
7478 print join('**', @arr); # Output is: "foo**bar**baz"
7479 print join('**', 'foo', 'bar', 'baz'); # Same as previous line
7480 print join('**', 'A', @arr, 'B'); # Output: "A**foo**bar**baz**B"
7482 -----------------------------------
7484 4) Consider the Context
7486 Many Perl functions and operators behave differently depending on the
7487 "context". Perl has three possible contexts: scalar context, list
7488 context and void context:
7490 my @arr = ('foo', 'bar', 'baz');
7491 my @a = reverse(@arr); # Array context
7492 my $s = reverse(@arr); # Scalar context
7493 reverse(@arr); # Void context
7495 print "@a\n"; # Output is: baz bar foo
7496 print "$s\n"; # Output is: zabraboof (foobarbaz backwards)
7498 Note that the context is determined by how the return value is used, NOT
7499 the parameters passed to the function/operator. This is quite Perl-like;
7500 most languages have functions that always return the same value, no
7501 matter how it's used.
7503 The use of an array (variable) in scalar context returns the length of
7506 my @array = ('foo', 'bar', 'baz');
7507 my $scalar = @array;
7508 print "$scalar\n"; # Output is 3.
7510 But don't try this with a literal list:
7512 my $scalar = ('foo', 'bar', 'baz'); # Warnings generated here.
7513 print "$scalar\n"; # Output is baz.
7515 In the above cases the context was obvious because we assigned to an
7516 array or scalar variable. But the context can also be indicated by an
7517 operator. For example, arithmetic operators are all scalar, as is string
7520 print "The length of the array is: " . @array . "\n";
7522 We can also force scalar context by using the "scalar" operator:
7524 ($length_a, $length_b) = (scalar @a, scalar @b);
7526 "man perlfunc" tells us: "There is no equivalent operator to force an
7527 expression to be interpolated in list context because in practice, this
7530 -----------------------------------
7534 Write a Perl program that reads /etc/passwd and outputs just a list of
7535 usernames and UIDs, ordered alphabetically by username. Remember: There
7536 Is More Than One Way To Do It.
7538 -----------------------------------
7540 6) Answers to Previous Exercises
7542 a) Here is a tail program that works on even a small number of lines.
7549 my $lines_to_output = @lines; # Get number of lines.
7550 $lines_to_output = 10 if $lines_to_output > 10; # Limit number of lines.
7552 print @lines[ -$lines_to_output .. -1 ];
7554 b) Here is a reversal algorithm.
7559 my @array = qw/A B C D E F G/;
7562 foreach my $element (@array) {
7563 @result = ($element, @result);
7566 print "Result is: @result\n";
7568 As you might guess, it would be a little more efficient to use "push"
7569 and "pop", but we hadn't seen these functions last week. (Calling the
7570 "reverse" function would be more efficient still! :-)
7572 -----------------------------------
7576 Part 1: Getting Started
7577 http://linuxchix.org/pipermail/courses/2003-March/001147.html
7580 http://linuxchix.org/pipermail/courses/2003-March/001153.html
7583 http://linuxchix.org/pipermail/courses/2003-April/001170.html
7585 Part 4: Control Structures
7586 http://linuxchix.org/pipermail/courses/2003-April/001184.html
7588 Part 4.5, a review with a little new information at the end:
7589 http://linuxchix.org/pipermail/courses/2003-July/001297.html
7591 Part 5: The "tr///" Operator
7592 http://linuxchix.org/pipermail/courses/2003-July/001302.html
7594 Part 6: The "m//" Operator
7595 http://linuxchix.org/pipermail/courses/2003-August/001305.html
7597 Part 7: More About "m//"
7598 http://linuxchix.org/pipermail/courses/2003-August/001322.html
7600 Part 8: The "s///" Operator
7601 http://linuxchix.org/pipermail/courses/2003-August/001330.html
7603 Part 9: Simple File Access
7604 http://linuxchix.org/pipermail/courses/2003-September/001340.html
7606 Part 10: Executing Commands with "open"
7607 http://linuxchix.org/pipermail/courses/2003-September/001344.html
7609 Part 11: Perl Variables
7610 http://linuxchix.org/pipermail/courses/2003-October/001345.html
7612 Part 12: Side Effects with Perl Variables
7613 http://linuxchix.org/pipermail/courses/2003-October/001347.html
7616 http://linuxchix.org/pipermail/courses/2003-October/001349.html
7619 http://linuxchix.org/pipermail/courses/2003-October/001350.html
7621 Part 15: More About Lists
7622 http://linuxchix.org/pipermail/courses/2003-November/001351.html
7624 -----------------------------------
7628 Works cited: man perlfunc
7630 Thanks to Jacinta Richardson for fact checking.
7632 -----------------------------------
7636 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
7637 Dan Richter, and is released under the same license as Perl itself
7638 (Artistic License or GPL, your choice). This is the license of choice to
7639 make it easy for other people to integrate your Perl code/documentation
7640 into their own projects. It is not generally used in projects unrelated
7644 From daniel.richter@wimba.com Fri Nov 21 22:58:12 2003
7645 Return-Path: <daniel.richter@wimba.com>
7646 Delivered-To: courses@linuxchix.org
7647 Received: from localhost (localhost [127.0.0.1])
7648 by www.linuxchix.org (Postfix) with ESMTP id C20CB272E08
7649 for <courses@linuxchix.org>; Fri, 21 Nov 2003 22:58:12 +1100 (EST)
7650 Received: from www.linuxchix.org ([127.0.0.1])
7651 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7652 id 29499-05 for <courses@linuxchix.org>;
7653 Fri, 21 Nov 2003 22:58:12 +1100 (EST)
7654 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
7655 by www.linuxchix.org (Postfix) with ESMTP id 8FD15272E04
7656 for <courses@linuxchix.org>; Fri, 21 Nov 2003 22:57:07 +1100 (EST)
7657 Received: from wimba.com (henu.essi.fr [157.169.1.46])
7658 by email.essi.fr (Postfix) with ESMTP id 0ACE11C1B7
7659 for <courses@linuxchix.org>; Fri, 21 Nov 2003 12:55:03 +0100 (CET)
7660 Message-ID: <3FBDFD2F.5010508@wimba.com>
7661 Date: Fri, 21 Nov 2003 12:55:27 +0100
7662 From: Dan Richter <daniel.richter@wimba.com>
7663 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
7664 X-Accept-Language: en-us
7666 To: courses@linuxchix.org
7667 Subject: [Courses] [Perl] Nothing this week
7668 Content-Type: text/plain; charset=us-ascii; format=flowed
7669 Content-Transfer-Encoding: 7bit
7670 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
7671 X-Topics: Perl programming course
7672 X-BeenThere: courses@linuxchix.org
7673 X-Mailman-Version: 2.1
7675 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7676 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7677 List-Post: <mailto:courses@linuxchix.org>
7678 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7679 <mailto:courses-request@linuxchix.org?subject=subscribe>
7680 List-Archive: <http://linuxchix.org/pipermail/courses>
7681 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7682 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7683 X-List-Received-Date: Fri, 21 Nov 2003 11:58:13 -0000
7687 It's Friday morning and Part 17 of the Perl course still isn't written.
7688 Oh, well: it will be ready next week. Sorry for the delay, everyone.
7691 When a program dies / what you need is a moment / of serenity.
7692 - Damian Conway, whose Perl module generates haikus
7694 From daniel.richter@wimba.com Fri Nov 28 22:01:58 2003
7695 Return-Path: <daniel.richter@wimba.com>
7696 Delivered-To: courses@linuxchix.org
7697 Received: from localhost (localhost [127.0.0.1])
7698 by www.linuxchix.org (Postfix) with ESMTP id 5675A272E0A
7699 for <courses@linuxchix.org>; Fri, 28 Nov 2003 22:01:58 +1100 (EST)
7700 Received: from www.linuxchix.org ([127.0.0.1])
7701 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7702 id 17332-08 for <courses@linuxchix.org>;
7703 Fri, 28 Nov 2003 22:01:58 +1100 (EST)
7704 Received: from email.essi.fr (email.essi.fr [157.169.25.6])
7705 by www.linuxchix.org (Postfix) with ESMTP id 1AEC2272E09
7706 for <courses@linuxchix.org>; Fri, 28 Nov 2003 22:01:57 +1100 (EST)
7707 Received: from wimba.com (male.essi.fr [157.169.2.13])
7708 by email.essi.fr (Postfix) with ESMTP id E82E61C202
7709 for <courses@linuxchix.org>; Fri, 28 Nov 2003 12:00:17 +0100 (CET)
7710 Message-ID: <3FC72B1E.5070809@wimba.com>
7711 Date: Fri, 28 Nov 2003 12:01:50 +0100
7712 From: Dan Richter <daniel.richter@wimba.com>
7713 User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.2.1) Gecko/20030225
7714 X-Accept-Language: en-us
7716 To: courses@linuxchix.org
7717 Subject: [Courses] [Perl] Part 17: grep and map
7718 Content-Type: text/plain; charset=us-ascii; format=flowed
7719 Content-Transfer-Encoding: 7bit
7720 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at linuxchix.org
7721 X-Topics: Perl programming course
7722 X-BeenThere: courses@linuxchix.org
7723 X-Mailman-Version: 2.1
7725 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7726 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7727 List-Post: <mailto:courses@linuxchix.org>
7728 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7729 <mailto:courses-request@linuxchix.org?subject=subscribe>
7730 List-Archive: <http://linuxchix.org/pipermail/courses>
7731 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7732 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7733 X-List-Received-Date: Fri, 28 Nov 2003 11:01:58 -0000
7734 Content-Length: 6890
7737 LinuxChix Perl Course Part 17: grep and map
7740 2) grep - filters a list
7741 3) map - transforms the values of a list
7742 4) What "grep" and "map" have in common
7744 6) Answer to Previous Exercise
7749 -----------------------------------
7753 Before we finish looking at arrays in Perl, I thought we should take a
7754 quick look at two handy Perl functions: "grep" and "map". Both functions
7755 are technically operators because they allow you to do magical things
7756 that a function can't do, but syntactically they look like functions, so
7757 we refer to them as functions here.
7759 Let me add that this will be the last e-mail before January. It's that
7760 busy-busy-busy time of year and I'm afraid that I have no more time to
7761 write about Perl than you have to read about it.
7763 -----------------------------------
7765 2) grep - filters a list
7767 The "grep" function returns only the elements of a list that meet a
7770 @positive_numbers = grep($_ > 0, @numbers);
7772 As you can see, each element is refered to as "$_". This (plus the fact
7773 that parentheses are optional) allows you write commands that look
7774 similar to invocations of the Unix "grep" program:
7776 @non_blank_lines = grep /\S/, @lines;
7778 In addition, you can specify a code block rather than a single condition:
7780 @non_blank_lines = grep { /\S/ } @lines; # Equivalent to the above.
7782 Obviously it doesn't matter in this case, but code blocks are helpful
7783 when you want a complex filter with multiple lines of code. The result
7784 of the code block is the result of the last statement executed:
7786 # All positive numbers can be used as exponents,
7787 # but negative exponents must be integers.
7788 @can_be_used_as_exponent = grep {
7790 ! /\./; # No decimal point -> integer.
7797 -----------------------------------
7799 3) map - transforms the values of a list
7801 The "map" function applies a transformation to each element of a list
7802 and returns the result, leaving the original list unchanged (unless you
7803 mess it up; more on that in a moment).
7805 @lines_with_newlines = map( $_ . "\n", @lines_without_newlines);
7807 As with "grep", each value in the list is refered to as "$_".
7809 "map" can also take a block of code:
7811 # Replace "x@y.z" with "x at y dot z" to confuse spammers.
7812 @disguised_addresses = map {
7814 $email =~ s/\@/ at /;
7815 $email =~ s/\./ dot /g;
7819 Note that it's important not to change "$_" because that would change
7820 the original "@email_addresses" (and you wouldn't get what you wanted in
7821 "@disguised_addresses").
7823 "map" needs not be a one-to-one mapping. For example, in the following code:
7825 @words = map m/\b(\w+)\b/g, @lines; # Spaces are for clarity.
7827 the regular expression splits a string into a list of words. The "map"
7828 function returns the result of joining all the small lists. If a line
7829 contains no words, the regular expression will return an empty list, and
7832 -----------------------------------
7834 4) What "grep" and "map" have in common
7836 "grep" and "map" have a lot in common. They both "magically" take a
7837 piece of code (either an expression or a code block) as a parameter. You
7838 need to put a comma after an expression but shouldn't put a comma after
7841 Changing "$_" in "grep" or "map" will change the original list. This
7842 isn't generally a good idea because it makes the code hard to read.
7843 Remember that "map" builds a list of results by evaluating an
7844 expression, NOT by setting "$_".
7846 A side effect of this fact is that you should not use "s///" with "map".
7847 The "s///" operator changes "$_" rather than returning a result, so you
7848 won't get what you would expect if you use it with "map" (and you
7849 CERTAINLY shouldn't use it with "grep").
7851 -----------------------------------
7855 a) Write some Perl code that, given a list of numbers, generates a list
7856 of square roots of those numbers. (The square root function in Perl is
7859 b) Modify the code to filter out any negative numbers. The result should
7860 be as though the negative numbers were never in the original list.
7862 c) Write a Perl program that reads two files and outputs only the lines
7863 that are common to both of them.
7865 -----------------------------------
7867 6) Answer to Previous Exercise
7869 The following program reads the password file and outputs a list of
7870 usernames and UIDs, ordered by username:
7875 open FILE, '< /etc/passwd' or die "Couldn't open file: $!";
7876 my @data = sort(<FILE>);
7881 my @fields = split(/:/); # Equivalent to split(/:/, $_)
7882 push @result, $fields[0] . ' -> ' . $fields[2];
7885 print join("\n",@result) . "\n";
7887 The above program is a nice review of Perl functions. But of course,
7888 There Is More Than One Way To Do It, and we could replace the bottom
7892 s/^(.*?):.*?:(\d*):.*$/$1 -> $2/;
7894 print join("\n",@result) . "\n";
7896 Or to make the program really short:
7898 $_ = join '', @data;
7899 s/^(.*?):.*?:(\d*):.*$/$1 -> $2/gm;
7900 print; # Prints "$_"
7902 -----------------------------------
7906 Part 16: Array Functions
7907 http://linuxchix.org/pipermail/courses/2003-November/001359.html
7909 Part 15: More About Lists
7910 http://linuxchix.org/pipermail/courses/2003-November/001351.html
7913 http://linuxchix.org/pipermail/courses/2003-October/001350.html
7916 http://linuxchix.org/pipermail/courses/2003-October/001349.html
7918 Part 12: Side Effects with Perl Variables
7919 http://linuxchix.org/pipermail/courses/2003-October/001347.html
7921 Part 11: Perl Variables
7922 http://linuxchix.org/pipermail/courses/2003-October/001345.html
7924 Parts 1-10: see the end of:
7925 http://linuxchix.org/pipermail/courses/2003-October/001345.html
7927 -----------------------------------
7933 b) Kirrily Robert, Paul Fenwick and Jacinta Richardson's
7934 "Intermediate Perl", which you can find (along with their
7935 "Introduction to Perl") at:
7936 http://www.perltraining.com.au/notes.html
7938 Thanks to Jacinta Richardson for fact checking.
7940 -----------------------------------
7944 This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
7945 Dan Richter, and is released under the same license as Perl itself
7946 (Artistic License or GPL, your choice). This is the license of choice to
7947 make it easy for other people to integrate your Perl code/documentation
7948 into their own projects. It is not generally used in projects unrelated
7952 From dan@cellectivity.com Fri Apr 1 20:31:50 2005
7953 Return-Path: <dan@cellectivity.com>
7954 X-Original-To: courses@linuxchix.org
7955 Delivered-To: courses@linuxchix.org
7956 Received: from localhost (localhost [127.0.0.1])
7957 by www.linuxchix.org (Postfix) with ESMTP id 89E7927307A
7958 for <courses@linuxchix.org>; Fri, 1 Apr 2005 20:31:50 +1000 (EST)
7959 Received: from www.linuxchix.org ([127.0.0.1])
7960 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
7961 id 19282-06 for <courses@linuxchix.org>;
7962 Fri, 1 Apr 2005 20:31:50 +1000 (EST)
7963 X-Greylist: delayed 735 seconds by postgrey-1.17 at nest;
7964 Fri, 01 Apr 2005 20:31:49 EST
7965 Received: from cellectivity.com (unknown [212.18.242.163])
7966 by www.linuxchix.org (Postfix) with ESMTP id 71E0727301A
7967 for <courses@linuxchix.org>; Fri, 1 Apr 2005 20:31:49 +1000 (EST)
7968 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
7969 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
7970 Fri, 1 Apr 2005 11:20:16 +0100
7971 Message-ID: <424D1F42.3070809@cellectivity.com>
7972 Date: Fri, 01 Apr 2005 11:15:30 +0100
7973 From: Dan <dan@cellectivity.com>
7974 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
7975 rv:1.7.5) Gecko/20050105 Debian/1.7.5-1
7976 X-Accept-Language: en
7978 To: courses@linuxchix.org
7979 Content-Type: text/plain; charset=us-ascii; format=flowed
7980 Content-Transfer-Encoding: 7bit
7981 X-OriginalArrivalTime: 01 Apr 2005 10:20:16.0406 (UTC)
7982 FILETIME=[6604E360:01C536A4]
7983 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
7985 Subject: [Courses] [Perl] Welcome to the Perl course
7986 X-BeenThere: courses@linuxchix.org
7987 X-Mailman-Version: 2.1.5
7989 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
7990 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7991 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
7992 List-Archive: <http://linuxchix.org/pipermail/courses>
7993 List-Post: <mailto:courses@linuxchix.org>
7994 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
7995 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
7996 <mailto:courses-request@linuxchix.org?subject=subscribe>
7997 X-List-Received-Date: Fri, 01 Apr 2005 10:31:50 -0000
7998 Content-Length: 2896
8001 This course is an introduction to Perl. It assumes that you have no
8002 knowledge of Perl whatsoever, but that you have at least written
8003 small programs in some other language. (More on that in a moment.)
8005 The purpose of the course is not to make you a master of Perl, but
8006 to make you proficient enough to write basic Perl programs and to
8007 read other people's Perl code.
8009 The lessons will go on a weekly cycle: one every Friday (including
8012 It's recommended that you do at least the first exercise from each
8013 week, unless you apply what you read by writing something similar
8014 on your own. Exercises beyond the first one are more exploratory and
8015 so are optional: you do them if you have time. The optional exercises
8016 will bring you a greater knowledge of Perl, but if you don't have
8017 time for them, the first exercise is enough to get the basics.
8019 A solution to each week's exercises will be provided the following
8022 When you complete an exercise, you can do one of three things with
8024 1) keep it to yourself
8025 2) send it to me and I'll critique it
8026 3) send it to the list for everyone to discuss (but give everyone
8027 else a few days to work on it before sending your solution)
8029 Any of these are okay, but I encourage you to send non-trivial
8030 answers to the list so that everyone else can consider them. There
8031 Is More Than One Way To Do It (TIMTOWTDI, pronounced Tim-Towtdy).
8033 When discussing someone else's solution, of course please be polite
8034 about it. Point out obscure cases that weren't taken into account
8035 (unless they're obvious), remembering that the author may have
8036 intentionally ignored them: it *is* just an exercise after all!
8038 Any other discussion or questions are also welcome. I welcome private
8039 questions/comments, but I encourage you to write to the list - even if
8040 it's to complain that something wasn't explained well enough. ;-)
8042 A few people wrote to me in surprise that I discourage learning Perl
8043 as a first language. This is because I believe that some other
8044 language (ANY other language) would be a better tool to learn
8045 programming. Perl has no constants, virtually no data
8046 types, and its handling of structs and exceptions is very poor
8047 compared to almost any other language. In addition, Perl's wide
8048 variety of choices require the experience to choose well. I recommend
8049 starting with some other language, such as Python, Ruby, Java or
8050 (for the stalwart) C++. However, the choice to learn Perl is
8051 ultimately yours; everyone is welcome in this course.
8054 [M]y wife and I attended grad school in linguistics at Berkeley
8055 and UCLA. At the time, we were actually planning to be
8056 missionaries (more specifically, Bible translators), but we had
8057 to drop that idea for health reasons. Funny thing is, now the
8058 missionaries probably get more good out of Perl than they'd have
8059 gotten out of me as a missionary. Go figure.
8060 - Larry Wall (inventor of Perl)
8062 From dan@cellectivity.com Sat Apr 2 02:14:19 2005
8063 Return-Path: <dan@cellectivity.com>
8064 X-Original-To: courses@linuxchix.org
8065 Delivered-To: courses@linuxchix.org
8066 Received: from localhost (localhost [127.0.0.1])
8067 by www.linuxchix.org (Postfix) with ESMTP id A58D327307B
8068 for <courses@linuxchix.org>; Sat, 2 Apr 2005 02:14:19 +1000 (EST)
8069 Received: from www.linuxchix.org ([127.0.0.1])
8070 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8071 id 21247-08 for <courses@linuxchix.org>;
8072 Sat, 2 Apr 2005 02:14:19 +1000 (EST)
8073 Received: from cellectivity.com (unknown [212.18.242.163])
8074 by www.linuxchix.org (Postfix) with ESMTP id 4E674273074
8075 for <courses@linuxchix.org>; Sat, 2 Apr 2005 02:14:17 +1000 (EST)
8076 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
8077 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
8078 Fri, 1 Apr 2005 17:15:00 +0100
8079 Message-ID: <424D7264.4060904@cellectivity.com>
8080 Date: Fri, 01 Apr 2005 17:10:12 +0100
8081 From: Dan <dan@cellectivity.com>
8082 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
8083 rv:1.7.5) Gecko/20050105 Debian/1.7.5-1
8084 X-Accept-Language: en
8086 To: courses@linuxchix.org
8087 Subject: [Courses] [Perl] Part 1: Getting Started
8088 Content-Type: text/plain; charset=us-ascii; format=flowed
8089 Content-Transfer-Encoding: 7bit
8090 X-OriginalArrivalTime: 01 Apr 2005 16:15:00.0609 (UTC)
8091 FILETIME=[F4648710:01C536D5]
8092 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8094 X-BeenThere: courses@linuxchix.org
8095 X-Mailman-Version: 2.1.5
8097 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8098 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8099 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8100 List-Archive: <http://linuxchix.org/pipermail/courses>
8101 List-Post: <mailto:courses@linuxchix.org>
8102 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8103 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8104 <mailto:courses-request@linuxchix.org?subject=subscribe>
8105 X-List-Received-Date: Fri, 01 Apr 2005 16:14:19 -0000
8106 Content-Length: 4615
8109 LinuxChix Perl Course Part 1: Getting Started
8113 3) Warnings and Strict Compilation
8119 ----------------------------------------
8123 The first step to using Perl is installing it. If you're using a
8124 Linux or BSD system you're ahead of the game, because it's rare these
8125 days to find an open-source installation without Perl. If you're
8126 running Windows, you may need to download Perl, and www.perl.org is a
8127 good place to start.
8129 ----------------------------------------
8133 Since we're not big on originality, we're going to start with a Hello
8136 print "Hello, world!\n";
8138 To execute the program, write it in a file (say, hello.pl) and
8139 execute the following on the command line:
8143 Now let's make the program a little more interesting:
8146 print "Hello, $who!\n";
8148 Finally, let's add some comments. Comments can be provided using the
8149 "#" character, which is often called a hash or pound sign. (I believe
8150 the correct term is "octothorp".) The comment begins at the "#" and
8151 ends at the end of the line.
8153 # This is a Hello World program in Perl.
8154 my $who; # Declare variable.
8155 $who = "world"; # Assign variable.
8156 print "Hello, $who!\n"; # Print result.
8158 ----------------------------------------
8160 3) Warnings and Strict Compilation
8162 Although the above program is a valid, working progam, I want to
8163 introduce two tools that will save you hours of debugging later on.
8165 The first tool is strict compilation. This requires you, among other
8166 things, to declare each variable before you use it. This will alert
8167 you of misspelled variable names. To use strict compilation, begin
8168 the program with "use strict;". (We'll see an example below.)
8170 The second tool is warnings. This will cause the interpreter to
8171 mention probable logic errors as the program executes. For example,
8172 the statement "$a = $b + c;" is syntactically correct in Perl, but
8173 you probably wanted a dollar sign in front of the "c". If you have
8174 warnings turned on, Perl will flag this mistake.
8176 To turn on warnings, you can invoke the "-w" flag on the command
8181 However, this requires the user to include the flag every time she
8182 executes the program. A better solution is to use a "shebang" line at
8183 the beginning of the program:
8187 The perl interpreter will honor the "shebang" line even if the "-w"
8188 flag is not invoked on the command line.
8190 Notice that the shebang line is special in two ways:
8191 a) It starts with a hash but is not a comment.
8192 b) It doesn't end with a semicolon.
8194 If you're not familiar with shebang lines, feel free to post a
8195 question to the list (this one or the techtalk list).
8197 An additional benefit of the "shebang" line is that it allows you to
8198 execute the script without typing "perl" on the command line, just as
8199 if it were a shell script or a compiled program. (Don't forget to set
8200 execution permissions.) However, it also means that we have to know
8201 where on the filesystem the Perl interpreter is located. We can do
8202 this by typing "which perl", or a similar command, at the command
8205 So our Hello World program becomes:
8209 my $who; # Declare variable.
8210 $who = "world"; # Assign variable.
8211 print "Hello, $who!\n"; # Print result.
8213 Experienced Perl programmers may sometimes turn off these safeguards,
8214 but it's not recommended for beginners.
8216 ----------------------------------------
8220 a) In the Hello World program, what does the "\n" mean? If you don't
8221 know, try moving it around inside the string and see what result it
8224 b) Use the man pages to find out what "Perl" stands for.
8226 ----------------------------------------
8230 A big thank you to Jacinta Richardson for suggestions and
8231 proofreading. More advanced Perl users might want to check out the
8232 free material from Perl Training Australia
8233 <http://www.perltraining.com.au/>, which she is a part of.
8235 Other contributors include Meryll Larkin.
8237 ----------------------------------------
8241 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
8242 Richter and Alice Wood, and is released under the same license as
8243 Perl itself (Artistic License or GPL, your choice). This is the
8244 license of choice to make it easy for other people to integrate your
8245 Perl code/documentation into their own projects. It is not generally
8246 used in projects unrelated to Perl.
8248 From devdas@dvb.homelinux.org Sat Apr 2 03:39:08 2005
8249 Return-Path: <devdas@dvb.homelinux.org>
8250 X-Original-To: courses@linuxchix.org
8251 Delivered-To: courses@linuxchix.org
8252 Received: from localhost (localhost [127.0.0.1])
8253 by www.linuxchix.org (Postfix) with ESMTP id 8DFF227307B
8254 for <courses@linuxchix.org>; Sat, 2 Apr 2005 03:39:08 +1000 (EST)
8255 Received: from www.linuxchix.org ([127.0.0.1])
8256 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8257 id 21749-06 for <courses@linuxchix.org>;
8258 Sat, 2 Apr 2005 03:39:08 +1000 (EST)
8259 X-Greylist: delayed 1457 seconds by postgrey-1.17 at nest;
8260 Sat, 02 Apr 2005 03:39:08 EST
8261 Received: from tin.nixcartel.org (ns1.nixcartel.com [66.98.212.93])
8262 by www.linuxchix.org (Postfix) with ESMTP id 1FB1327301A
8263 for <courses@linuxchix.org>; Sat, 2 Apr 2005 03:39:08 +1000 (EST)
8264 Received: from dvb.homelinux.org (unknown [202.88.172.195])
8265 by tin.nixcartel.org (Postfix) with ESMTP id 00A101AC08C
8266 for <courses@linuxchix.org>; Fri, 1 Apr 2005 11:35:56 -0600 (CST)
8267 Received: by dvb.homelinux.org (Postfix, from userid 500)
8268 id D996A340D2; Fri, 1 Apr 2005 22:44:18 +0530 (IST)
8269 Date: Fri, 1 Apr 2005 22:44:18 +0530
8270 From: Devdas Bhagat <devdas@dvb.homelinux.org>
8271 To: courses@linuxchix.org
8272 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8273 Message-ID: <20050401224418.A1455@evita.devdas.geek>
8274 References: <424D7264.4060904@cellectivity.com>
8276 Content-Type: text/plain; charset=us-ascii
8277 Content-Disposition: inline
8278 User-Agent: Mutt/1.2.5.1i
8279 In-Reply-To: <424D7264.4060904@cellectivity.com>;
8280 from dan@cellectivity.com on Fri, Apr 01, 2005 at 05:10:12PM +0100
8281 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8283 X-BeenThere: courses@linuxchix.org
8284 X-Mailman-Version: 2.1.5
8286 Reply-To: Devdas Bhagat <devdas@dvb.homelinux.org>
8287 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8288 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8289 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8290 List-Archive: <http://linuxchix.org/pipermail/courses>
8291 List-Post: <mailto:courses@linuxchix.org>
8292 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8293 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8294 <mailto:courses-request@linuxchix.org?subject=subscribe>
8295 X-List-Received-Date: Fri, 01 Apr 2005 17:39:08 -0000
8299 On 01/04/05 17:10 +0100, Dan wrote:
8300 > LinuxChix Perl Course Part 1: Getting Started
8304 > 3) Warnings and Strict Compilation
8306 > 5) Acknowledgements
8310 > ----------------------------------------
8314 > The first step to using Perl is installing it. If you're using a
8315 > Linux or BSD system you're ahead of the game, because it's rare these
8316 > days to find an open-source installation without Perl. If you're
8317 > running Windows, you may need to download Perl, and www.perl.org is a
8318 > good place to start.
8320 http://www.activestate.com/ is probably a better place to point to for
8321 Windows binary downloads.
8325 From fazia@faziarizvi.net Tue Apr 5 08:52:30 2005
8326 Return-Path: <fazia@faziarizvi.net>
8327 X-Original-To: courses@linuxchix.org
8328 Delivered-To: courses@linuxchix.org
8329 Received: from localhost (localhost [127.0.0.1])
8330 by www.linuxchix.org (Postfix) with ESMTP id D07CE27309B
8331 for <courses@linuxchix.org>; Tue, 5 Apr 2005 08:52:30 +1000 (EST)
8332 Received: from www.linuxchix.org ([127.0.0.1])
8333 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8334 id 16310-09 for <courses@linuxchix.org>;
8335 Tue, 5 Apr 2005 08:52:30 +1000 (EST)
8336 X-Greylist: delayed 2034 seconds by postgrey-1.17 at nest;
8337 Tue, 05 Apr 2005 08:52:30 EST
8338 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
8339 by www.linuxchix.org (Postfix) with ESMTP id 52A37272FE9
8340 for <courses@linuxchix.org>; Tue, 5 Apr 2005 08:52:30 +1000 (EST)
8341 Received: from tauri.jsnider.net ([66.194.162.10])
8342 by tauri.jsnider.net with esmtp (Exim 4.44) id 1DIZtv-0004qi-3A
8343 for courses@linuxchix.org; Mon, 04 Apr 2005 17:18:31 -0500
8344 Date: Mon, 4 Apr 2005 17:18:31 -0500 (CDT)
8345 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
8346 X-X-Sender: fazia@tauri.jsnider.net
8347 To: courses@linuxchix.org
8348 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8349 Message-ID: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8351 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
8352 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8354 X-BeenThere: courses@linuxchix.org
8355 X-Mailman-Version: 2.1.5
8357 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8358 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8359 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8360 List-Archive: <http://linuxchix.org/pipermail/courses>
8361 List-Post: <mailto:courses@linuxchix.org>
8362 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8363 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8364 <mailto:courses-request@linuxchix.org?subject=subscribe>
8365 X-List-Received-Date: Mon, 04 Apr 2005 22:52:31 -0000
8366 Content-Length: 1043
8372 I've just joined the Linuxchix Courses list, specifically for your class
8373 on Perl. I'll be following it every week.
8375 Since I joined a bit late, I went to the archive and read the first two
8376 messages. I've already done my first program. :-)
8378 I also have my first question. Though I'll be doing later programs on a
8379 linux server, I put together this first one on my Mac (OSX). Everything
8380 worked except I could not get the program to run by simply typing
8381 "hello.pl" after insterting the shebang line. (It runs just fine if I type
8384 I know that some flavors of perl can be different. Is this the case here?
8385 Or did I perhaps write something incorrectly?
8389 # My very first perl program April 4, 2005 - Fazia
8390 my $who; # Declare variable
8391 $who = "world"; # Assign variable
8392 print "Hello, world!\n"; # Print result
8400 Bits and Bytes: http://uweb.txstate.edu/~fr06/weblog/
8401 Weblog: http://www.faziarizvi.net/
8403 From devdas@dvb.homelinux.org Tue Apr 5 09:14:58 2005
8404 Return-Path: <devdas@dvb.homelinux.org>
8405 X-Original-To: courses@linuxchix.org
8406 Delivered-To: courses@linuxchix.org
8407 Received: from localhost (localhost [127.0.0.1])
8408 by www.linuxchix.org (Postfix) with ESMTP id 992EC27309B
8409 for <courses@linuxchix.org>; Tue, 5 Apr 2005 09:14:58 +1000 (EST)
8410 Received: from www.linuxchix.org ([127.0.0.1])
8411 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8412 id 16480-08 for <courses@linuxchix.org>;
8413 Tue, 5 Apr 2005 09:14:58 +1000 (EST)
8414 Received: from tin.nixcartel.org (ns1.nixcartel.com [66.98.212.93])
8415 by www.linuxchix.org (Postfix) with ESMTP id AB830273086
8416 for <courses@linuxchix.org>; Tue, 5 Apr 2005 09:14:53 +1000 (EST)
8417 Received: from dvb.homelinux.org (unknown [202.88.172.195])
8418 by tin.nixcartel.org (Postfix) with ESMTP id AC3A11AC07F
8419 for <courses@linuxchix.org>; Mon, 4 Apr 2005 18:36:07 -0500 (CDT)
8420 Received: by dvb.homelinux.org (Postfix, from userid 500)
8421 id 1136C340D0; Tue, 5 Apr 2005 04:43:30 +0530 (IST)
8422 Date: Tue, 5 Apr 2005 04:43:29 +0530
8423 From: Devdas Bhagat <devdas@dvb.homelinux.org>
8424 To: courses@linuxchix.org
8425 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8426 Message-ID: <20050405044329.A12674@evita.devdas.geek>
8427 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8429 Content-Type: text/plain; charset=us-ascii
8430 Content-Disposition: inline
8431 User-Agent: Mutt/1.2.5.1i
8432 In-Reply-To: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>;
8433 from fazia@faziarizvi.net on Mon, Apr 04, 2005 at 05:18:31PM -0500
8434 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8436 X-BeenThere: courses@linuxchix.org
8437 X-Mailman-Version: 2.1.5
8439 Reply-To: Devdas Bhagat <devdas@dvb.homelinux.org>
8440 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8441 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8442 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8443 List-Archive: <http://linuxchix.org/pipermail/courses>
8444 List-Post: <mailto:courses@linuxchix.org>
8445 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8446 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8447 <mailto:courses-request@linuxchix.org?subject=subscribe>
8448 X-List-Received-Date: Mon, 04 Apr 2005 23:14:58 -0000
8449 Content-Length: 1917
8452 On 04/04/05 17:18 -0500, Fazia Begum Rizvi wrote:
8456 > I've just joined the Linuxchix Courses list, specifically for your class
8457 > on Perl. I'll be following it every week.
8459 > Since I joined a bit late, I went to the archive and read the first two
8460 > messages. I've already done my first program. :-)
8462 > I also have my first question. Though I'll be doing later programs on a
8463 > linux server, I put together this first one on my Mac (OSX). Everything
8464 > worked except I could not get the program to run by simply typing
8465 > "hello.pl" after insterting the shebang line. (It runs just fine if I type
8468 On Unix systems, the OS needs to be told that the file is an executable.
8469 You can do this by running chmod +x file. Refer to the chmod manual for
8470 more detail on the options it takes. man 1 chmod.
8472 Also, on most Unix systems, the current directory is not in your PATH
8473 environment variable. The PATH is the list of directories where an
8474 executable is looked for by the shell. To see your current PATH,
8475 echo $PATH in your shell. If you had marked that file as executable,
8476 then you should try running ./hello.pl to see if it works.
8478 > I know that some flavors of perl can be different. Is this the case here?
8479 > Or did I perhaps write something incorrectly?
8481 When you call the script as perl <scriptname>, the shell executes the
8482 perl binary, which proceeds to run through the commands in <scriptname>.
8484 When you use the shebang line, the shell invokes the interpreter
8485 referred to by the command on the shebang line, and then that handles
8486 the rest of the script.
8488 > #!/usr/bin/perl -w
8490 > # My very first perl program April 4, 2005 - Fazia
8491 > my $who; # Declare variable
8492 > $who = "world"; # Assign variable
8493 > print "Hello, world!\n"; # Print result
8495 Did you mean to use $who up there?
8496 print "Hello, $who!\n";
8500 From fazia@faziarizvi.net Tue Apr 5 14:15:42 2005
8501 Return-Path: <fazia@faziarizvi.net>
8502 X-Original-To: courses@linuxchix.org
8503 Delivered-To: courses@linuxchix.org
8504 Received: from localhost (localhost [127.0.0.1])
8505 by www.linuxchix.org (Postfix) with ESMTP id DB09427309B
8506 for <courses@linuxchix.org>; Tue, 5 Apr 2005 14:15:41 +1000 (EST)
8507 Received: from www.linuxchix.org ([127.0.0.1])
8508 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8509 id 18453-06 for <courses@linuxchix.org>;
8510 Tue, 5 Apr 2005 14:15:41 +1000 (EST)
8511 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
8512 by www.linuxchix.org (Postfix) with ESMTP id 652E3273062
8513 for <courses@linuxchix.org>; Tue, 5 Apr 2005 14:15:41 +1000 (EST)
8514 Received: from tauri.jsnider.net ([66.194.162.10])
8515 by tauri.jsnider.net with esmtp (Exim 4.44)
8516 id 1DIfTP-0005CE-Gs; Mon, 04 Apr 2005 23:15:31 -0500
8517 Date: Mon, 4 Apr 2005 23:15:31 -0500 (CDT)
8518 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
8519 X-X-Sender: fazia@tauri.jsnider.net
8520 To: Devdas Bhagat <devdas@dvb.homelinux.org>
8521 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8522 In-Reply-To: <20050405044329.A12674@evita.devdas.geek>
8523 Message-ID: <Pine.LNX.4.60.0504042311470.19968@tauri.jsnider.net>
8524 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8525 <20050405044329.A12674@evita.devdas.geek>
8527 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
8528 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8530 Cc: courses@linuxchix.org
8531 X-BeenThere: courses@linuxchix.org
8532 X-Mailman-Version: 2.1.5
8534 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8535 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8536 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8537 List-Archive: <http://linuxchix.org/pipermail/courses>
8538 List-Post: <mailto:courses@linuxchix.org>
8539 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8540 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8541 <mailto:courses-request@linuxchix.org?subject=subscribe>
8542 X-List-Received-Date: Tue, 05 Apr 2005 04:15:42 -0000
8543 Content-Length: 1268
8547 On Tue, 5 Apr 2005, Devdas Bhagat wrote:
8549 > On Unix systems, the OS needs to be told that the file is an executable.
8550 > You can do this by running chmod +x file. Refer to the chmod manual for
8551 > more detail on the options it takes. man 1 chmod.
8553 Yeah - that much I knew, and the chmod command works fine on OSX. (I was
8554 able to make it executable.) Still didn't work though, so I was stumped.
8555 (Not that it matters too much since I'll be doing everything else on an
8556 Linux server, but I was curious.)
8559 > Also, on most Unix systems, the current directory is not in your PATH
8560 > environment variable. The PATH is the list of directories where an
8561 > executable is looked for by the shell. To see your current PATH,
8562 > echo $PATH in your shell. If you had marked that file as executable,
8563 > then you should try running ./hello.pl to see if it works.
8566 Ah! Okay, lemme see if that works...
8569 >> #!/usr/bin/perl -w
8571 >> # My very first perl program April 4, 2005 - Fazia
8572 >> my $who; # Declare variable
8573 >> $who = "world"; # Assign variable
8574 >> print "Hello, world!\n"; # Print result
8576 > Did you mean to use $who up there?
8577 > print "Hello, $who!\n";
8579 Yup, but I copied and pasted before I fixed that. :-)
8586 From fazia@faziarizvi.net Tue Apr 5 14:18:41 2005
8587 Return-Path: <fazia@faziarizvi.net>
8588 X-Original-To: courses@linuxchix.org
8589 Delivered-To: courses@linuxchix.org
8590 Received: from localhost (localhost [127.0.0.1])
8591 by www.linuxchix.org (Postfix) with ESMTP id 8C13927309B
8592 for <courses@linuxchix.org>; Tue, 5 Apr 2005 14:18:41 +1000 (EST)
8593 Received: from www.linuxchix.org ([127.0.0.1])
8594 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8595 id 18564-03 for <courses@linuxchix.org>;
8596 Tue, 5 Apr 2005 14:18:41 +1000 (EST)
8597 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
8598 by www.linuxchix.org (Postfix) with ESMTP id 16435273062
8599 for <courses@linuxchix.org>; Tue, 5 Apr 2005 14:18:41 +1000 (EST)
8600 Received: from tauri.jsnider.net ([66.194.162.10])
8601 by tauri.jsnider.net with esmtp (Exim 4.44)
8602 id 1DIfWN-0005CS-M3; Mon, 04 Apr 2005 23:18:35 -0500
8603 Date: Mon, 4 Apr 2005 23:18:35 -0500 (CDT)
8604 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
8605 X-X-Sender: fazia@tauri.jsnider.net
8606 To: Devdas Bhagat <devdas@dvb.homelinux.org>
8607 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8608 In-Reply-To: <20050405044329.A12674@evita.devdas.geek>
8609 Message-ID: <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8610 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8611 <20050405044329.A12674@evita.devdas.geek>
8613 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
8614 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8616 Cc: courses@linuxchix.org
8617 X-BeenThere: courses@linuxchix.org
8618 X-Mailman-Version: 2.1.5
8620 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8621 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8622 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8623 List-Archive: <http://linuxchix.org/pipermail/courses>
8624 List-Post: <mailto:courses@linuxchix.org>
8625 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8626 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8627 <mailto:courses-request@linuxchix.org?subject=subscribe>
8628 X-List-Received-Date: Tue, 05 Apr 2005 04:18:41 -0000
8633 On Tue, 5 Apr 2005, Devdas Bhagat wrote:
8635 > then you should try running ./hello.pl to see if it works.
8642 From suzo@spin.net.au Tue Apr 5 15:04:18 2005
8643 Return-Path: <suzo@spin.net.au>
8644 X-Original-To: courses@linuxchix.org
8645 Delivered-To: courses@linuxchix.org
8646 Received: from localhost (localhost [127.0.0.1])
8647 by www.linuxchix.org (Postfix) with ESMTP id 44F9227309B
8648 for <courses@linuxchix.org>; Tue, 5 Apr 2005 15:04:18 +1000 (EST)
8649 Received: from www.linuxchix.org ([127.0.0.1])
8650 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8651 id 18661-09 for <courses@linuxchix.org>;
8652 Tue, 5 Apr 2005 15:04:18 +1000 (EST)
8653 X-Greylist: delayed 1411 seconds by postgrey-1.17 at nest;
8654 Tue, 05 Apr 2005 15:04:17 EST
8655 Received: from angel.comcen.com.au (angel.comcen.com.au [203.23.236.69])
8656 by www.linuxchix.org (Postfix) with ESMTP id EE17D273059
8657 for <courses@linuxchix.org>; Tue, 5 Apr 2005 15:04:17 +1000 (EST)
8658 Received: from [127.0.0.1] (202-172-117-64.cpe.nsw-1.comcen.com.au
8660 by angel.comcen.com.au (8.12.10/8.12.9) with ESMTP id j354eUGa090551
8661 for <courses@linuxchix.org>; Tue, 5 Apr 2005 14:40:35 +1000 (EST)
8662 Message-ID: <425216BA.1000601@spin.net.au>
8663 Date: Tue, 05 Apr 2005 14:40:26 +1000
8664 From: Sue Stones <suzo@spin.net.au>
8665 User-Agent: Mozilla Thunderbird 0.9 (Windows/20041103)
8666 X-Accept-Language: en-us, en
8668 To: courses@linuxchix.org
8669 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8670 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net> <20050405044329.A12674@evita.devdas.geek>
8671 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8672 In-Reply-To: <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8673 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
8674 Content-Transfer-Encoding: 7bit
8675 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8677 X-BeenThere: courses@linuxchix.org
8678 X-Mailman-Version: 2.1.5
8680 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8681 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8682 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8683 List-Archive: <http://linuxchix.org/pipermail/courses>
8684 List-Post: <mailto:courses@linuxchix.org>
8685 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8686 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8687 <mailto:courses-request@linuxchix.org?subject=subscribe>
8688 X-List-Received-Date: Tue, 05 Apr 2005 05:04:18 -0000
8692 Fazia Begum Rizvi wrote:
8694 > On Tue, 5 Apr 2005, Devdas Bhagat wrote:
8696 >> then you should try running ./hello.pl to see if it works.
8701 I like to put "." in my path, as the fist directory to look in, to solve
8702 the problem of having to type "./" before everything. Usually the path
8703 is set in your profile, but exactly where that is in OSX I don't know.
8705 I usually also add a bin directory in my home directory if there isn't
8706 one there. I can then put in scripts that I have written.
8708 If you find the profile there will usually be a line something like
8709 $PATH=/path/to/somewhere:/path/to/somewhere/else
8711 The colon in this case is a separator, and could be different. I would
8713 $PATH=./:$HOME/sue/bin:/path/to/somewhere:/path/to/somewhere/else
8718 From evilpig@gmail.com Tue Apr 5 15:39:02 2005
8719 Return-Path: <evilpig@gmail.com>
8720 X-Original-To: courses@linuxchix.org
8721 Delivered-To: courses@linuxchix.org
8722 Received: from localhost (localhost [127.0.0.1])
8723 by www.linuxchix.org (Postfix) with ESMTP id 636C827309B
8724 for <courses@linuxchix.org>; Tue, 5 Apr 2005 15:39:02 +1000 (EST)
8725 Received: from www.linuxchix.org ([127.0.0.1])
8726 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8727 id 18949-05 for <courses@linuxchix.org>;
8728 Tue, 5 Apr 2005 15:39:02 +1000 (EST)
8729 X-Greylist: delayed 398 seconds by postgrey-1.17 at nest;
8730 Tue, 05 Apr 2005 15:39:01 EST
8731 Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.193])
8732 by www.linuxchix.org (Postfix) with ESMTP id 91B38272FE9
8733 for <courses@linuxchix.org>; Tue, 5 Apr 2005 15:39:01 +1000 (EST)
8734 Received: by rproxy.gmail.com with SMTP id c51so1417959rne
8735 for <courses@linuxchix.org>; Mon, 04 Apr 2005 22:38:52 -0700 (PDT)
8736 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com;
8737 h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:references;
8738 b=i/i9ld5aU2v6NJkLMaD3Y4taJ2RZUPPkaJhZfxQQcYvr8v9FUoBu5g6Bcxl4TnuRkMIxSJMtlgyPZvHdmtX2PkQNBseU4NuykY4iUa2mGKpB5mmY5V7j81L+119Rf4Y1WLGroL48ZOZNz5OqfiXazVbrZFw6ubop2mFYzW4jCgY=
8739 Received: by 10.38.208.10 with SMTP id f10mr5954637rng;
8740 Mon, 04 Apr 2005 22:32:12 -0700 (PDT)
8741 Received: by 10.38.89.10 with HTTP; Mon, 4 Apr 2005 22:32:12 -0700 (PDT)
8742 Message-ID: <133ac4eb05040422325c54b7e0@mail.gmail.com>
8743 Date: Tue, 5 Apr 2005 00:32:12 -0500
8744 From: Colleen Hatfield <evilpig@gmail.com>
8745 To: courses@linuxchix.org
8746 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8747 In-Reply-To: <425216BA.1000601@spin.net.au>
8749 Content-Type: text/plain; charset=ISO-8859-1
8750 Content-Transfer-Encoding: 7bit
8751 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8752 <20050405044329.A12674@evita.devdas.geek>
8753 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8754 <425216BA.1000601@spin.net.au>
8755 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8757 X-BeenThere: courses@linuxchix.org
8758 X-Mailman-Version: 2.1.5
8760 Reply-To: Colleen Hatfield <evilpig@gmail.com>
8761 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8762 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8763 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8764 List-Archive: <http://linuxchix.org/pipermail/courses>
8765 List-Post: <mailto:courses@linuxchix.org>
8766 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8767 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8768 <mailto:courses-request@linuxchix.org?subject=subscribe>
8769 X-List-Received-Date: Tue, 05 Apr 2005 05:39:03 -0000
8770 Content-Length: 1279
8773 On Apr 4, 2005 11:40 PM, Sue Stones <suzo@spin.net.au> wrote:
8775 > I like to put "." in my path, as the fist directory to look in, to solve
8776 > the problem of having to type "./" before everything.
8778 Be very careful with this. Having . in your PATH at all is insecure;
8779 having it first is even worse.
8781 The reason this is a problem is that it makes it very easy to execute
8782 malicious code unintentionally. For example, you might be browsing
8783 around your system and ls a directory. If a malicious user has put a
8784 malicious program with the name "ls" in the directory that you execute
8785 the ls command from, you've just executed it. If you're the only
8786 person that ever touches your machine, it's probably not too
8787 dangerous; otherwise it should definitely be avoided. ;-)
8789 Your other suggestion of creating a ~/bin/ directory is much safer
8790 (though I'd still probably put it at the end of the path rather than
8791 the beginning, unless you write programs with the same names as your
8792 system executable files).
8795 > is set in your profile, but exactly where that is in OSX I don't know.
8797 OS X is happily familiar in this area: /etc/profile is the system
8798 profile, which has the default PATH info; you can override/add on to
8799 this in your ~/.bash_profile or ~/.bashrc.
8803 From suzo@spin.net.au Tue Apr 5 16:46:28 2005
8804 Return-Path: <suzo@spin.net.au>
8805 X-Original-To: courses@linuxchix.org
8806 Delivered-To: courses@linuxchix.org
8807 Received: from localhost (localhost [127.0.0.1])
8808 by www.linuxchix.org (Postfix) with ESMTP id BB21427309B
8809 for <courses@linuxchix.org>; Tue, 5 Apr 2005 16:46:28 +1000 (EST)
8810 Received: from www.linuxchix.org ([127.0.0.1])
8811 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8812 id 19328-06 for <courses@linuxchix.org>;
8813 Tue, 5 Apr 2005 16:46:28 +1000 (EST)
8814 Received: from angel.comcen.com.au (angel.comcen.com.au [203.23.236.69])
8815 by www.linuxchix.org (Postfix) with ESMTP id 7617E27307A
8816 for <courses@linuxchix.org>; Tue, 5 Apr 2005 16:46:28 +1000 (EST)
8817 Received: from [127.0.0.1] (202-172-117-64.cpe.nsw-1.comcen.com.au
8819 by angel.comcen.com.au (8.12.10/8.12.9) with ESMTP id j356kCGa095652
8820 for <courses@linuxchix.org>; Tue, 5 Apr 2005 16:46:22 +1000 (EST)
8821 Message-ID: <42523430.2020705@spin.net.au>
8822 Date: Tue, 05 Apr 2005 16:46:08 +1000
8823 From: Sue Stones <suzo@spin.net.au>
8824 User-Agent: Mozilla Thunderbird 0.9 (Windows/20041103)
8825 X-Accept-Language: en-us, en
8827 To: courses@linuxchix.org
8828 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8829 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net> <20050405044329.A12674@evita.devdas.geek> <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net> <425216BA.1000601@spin.net.au>
8830 <133ac4eb05040422325c54b7e0@mail.gmail.com>
8831 In-Reply-To: <133ac4eb05040422325c54b7e0@mail.gmail.com>
8832 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
8833 Content-Transfer-Encoding: 7bit
8834 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8836 X-BeenThere: courses@linuxchix.org
8837 X-Mailman-Version: 2.1.5
8839 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8840 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8841 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8842 List-Archive: <http://linuxchix.org/pipermail/courses>
8843 List-Post: <mailto:courses@linuxchix.org>
8844 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8845 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8846 <mailto:courses-request@linuxchix.org?subject=subscribe>
8847 X-List-Received-Date: Tue, 05 Apr 2005 06:46:29 -0000
8851 Colleen Hatfield wrote:
8852 > If you're the only
8853 > person that ever touches your machine, it's probably not too
8854 > dangerous; otherwise it should definitely be avoided. ;-)
8856 Personally, yes I am the only one that has ever used my machine. (My
8857 only housemate for the last decade has been a dog that had absolutely no
8858 interest in using the computer) But if I was in a position where
8859 someone else was going to use my computer even for a few hours, I would
8860 create an account for them.
8865 From magnio@pvv.ntnu.no Tue Apr 5 18:02:55 2005
8866 Return-Path: <magnio@pvv.ntnu.no>
8867 X-Original-To: courses@linuxchix.org
8868 Delivered-To: courses@linuxchix.org
8869 Received: from localhost (localhost [127.0.0.1])
8870 by www.linuxchix.org (Postfix) with ESMTP id 2095C27309B
8871 for <courses@linuxchix.org>; Tue, 5 Apr 2005 18:02:55 +1000 (EST)
8872 Received: from www.linuxchix.org ([127.0.0.1])
8873 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8874 id 19889-08 for <courses@linuxchix.org>;
8875 Tue, 5 Apr 2005 18:02:55 +1000 (EST)
8876 X-Greylist: delayed 402 seconds by postgrey-1.17 at nest;
8877 Tue, 05 Apr 2005 18:02:53 EST
8878 Received: from bacchus.pvv.ntnu.no (bacchus.pvv.ntnu.no [129.241.210.178])
8879 by www.linuxchix.org (Postfix) with SMTP id BE1AD273060
8880 for <courses@linuxchix.org>; Tue, 5 Apr 2005 18:02:53 +1000 (EST)
8881 Received: (qmail 47491 invoked by uid 4000); 5 Apr 2005 07:56:04 -0000
8882 Date: Tue, 5 Apr 2005 09:56:04 +0200
8883 From: Magni Onsoien <magnio+courses@pvv.org>
8884 To: courses@linuxchix.org
8885 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8886 Message-ID: <20050405075604.GH2091@pvv.org>
8887 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8888 <20050405044329.A12674@evita.devdas.geek>
8889 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8890 <425216BA.1000601@spin.net.au>
8891 <133ac4eb05040422325c54b7e0@mail.gmail.com>
8892 <42523430.2020705@spin.net.au>
8894 Content-Type: text/plain; charset=us-ascii
8895 Content-Disposition: inline
8896 In-Reply-To: <42523430.2020705@spin.net.au>
8897 User-Agent: Mutt/1.4i
8899 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
8901 X-BeenThere: courses@linuxchix.org
8902 X-Mailman-Version: 2.1.5
8904 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
8905 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8906 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
8907 List-Archive: <http://linuxchix.org/pipermail/courses>
8908 List-Post: <mailto:courses@linuxchix.org>
8909 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
8910 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
8911 <mailto:courses-request@linuxchix.org?subject=subscribe>
8912 X-List-Received-Date: Tue, 05 Apr 2005 08:02:55 -0000
8913 Content-Length: 2328
8916 On 2005-04-05 16:46:08 +1000, Sue Stones said:
8917 > Colleen Hatfield wrote:
8918 > >If you're the only
8919 > >person that ever touches your machine, it's probably not too
8920 > >dangerous; otherwise it should definitely be avoided. ;-)
8922 > Personally, yes I am the only one that has ever used my machine. (My
8923 > only housemate for the last decade has been a dog that had absolutely no
8924 > interest in using the computer) But if I was in a position where
8925 > someone else was going to use my computer even for a few hours, I would
8926 > create an account for them.
8928 I do that before they are getting close to anythng but a locked screen
8931 But imagine that you have created her an account and you are using the
8932 computer together (either simultanously, like a server, or sequentally).
8933 The other person make a script that do something pretty evil, like 'rm
8934 -rf $HOME 2>&1>/dev/null' (which will delete your homedir and redirect
8935 all errors to /dev/null so you won't see them). This script is called ls
8936 and is put into /tmp (a directory writeable for anyone) and made
8939 I guess you can figure out what happens when you go to /tmp and type 'ls'
8940 to see what's there, if you have . first in your $PATH? :-)
8942 So making a separate account really doesn't matter if it's a multi user
8943 system, since in reality you'll be sharing directories and stuff anyway.
8944 And DON'T remove the world-write permissions for /tmp, it will break
8945 your system :-) (Not breaking in the same sense as rm -rf $HOME, though...)
8947 And for the record, the same will of course happen if the user puts her
8948 malicious script in her $HOME-directory and asks you to "have a look at
8949 my homedir, something seems to be wrong there".
8951 The best thing to do is to stick with ./script and avoid . in the $PATH.
8952 If you make scripts, it's safer to add their directory at the end of the
8953 $PATH (with e.g. 'export PATH=$PATH:$HOME/myscriptdir:$HOME/perlcourse').
8954 If you WANT to have . in your $PATH, add it to the end of the $PATH
8955 ('export PATH=$PATH:.') so you won't execute scripts and binaries with
8956 the same name as system programs when you didn't want to. If you insist
8957 on having your own 'ls', you could either use ./ls or add an alias for
8958 it in .bash_profile ('alias ls=$HOME/myscripts/ls').
8960 [Ok, this isn't on perl anymore. Sorry.]
8965 sash is very good for you.
8967 From anna@cs.wits.ac.za Tue Apr 5 22:57:48 2005
8968 Return-Path: <anna@cs.wits.ac.za>
8969 X-Original-To: courses@linuxchix.org
8970 Delivered-To: courses@linuxchix.org
8971 Received: from localhost (localhost [127.0.0.1])
8972 by www.linuxchix.org (Postfix) with ESMTP id 6C33127309B
8973 for <courses@linuxchix.org>; Tue, 5 Apr 2005 22:57:48 +1000 (EST)
8974 Received: from www.linuxchix.org ([127.0.0.1])
8975 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
8976 id 21502-10 for <courses@linuxchix.org>;
8977 Tue, 5 Apr 2005 22:57:48 +1000 (EST)
8978 X-Greylist: delayed 320 seconds by postgrey-1.17 at nest;
8979 Tue, 05 Apr 2005 22:57:43 EST
8980 Received: from titan.cs.wits.ac.za (titan.cs.wits.ac.za [146.141.27.10])
8981 by www.linuxchix.org (Postfix) with ESMTP id F3DC6272FE9
8982 for <courses@linuxchix.org>; Tue, 5 Apr 2005 22:57:43 +1000 (EST)
8983 Received: from cs.wits.ac.za (mail [127.0.0.1])
8984 by titan.cs.wits.ac.za (8.12.8/8.11.0) with ESMTP id j35CpY51023920;
8985 Tue, 5 Apr 2005 14:51:35 +0200
8986 From: "Anna Badimo" <anna@cs.wits.ac.za>
8987 To: Magni Onsoien <magnio+courses@pvv.org>, courses@linuxchix.org
8988 Subject: Re: [Courses] [Perl] Part 1: Getting Started
8989 Date: Tue, 5 Apr 2005 14:51:34 +0200
8990 Message-Id: <20050405125025.M64677@cs.wits.ac.za>
8991 In-Reply-To: <20050405075604.GH2091@pvv.org>
8992 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
8993 <20050405044329.A12674@evita.devdas.geek>
8994 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
8995 <425216BA.1000601@spin.net.au>
8996 <133ac4eb05040422325c54b7e0@mail.gmail.com>
8997 <42523430.2020705@spin.net.au> <20050405075604.GH2091@pvv.org>
8998 X-Mailer: Open WebMail 2.50 20050106
8999 X-OriginatingIP: 146.141.27.33 (anna)
9001 Content-Type: text/plain;
9003 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-1.4
9004 (titan.cs.wits.ac.za [127.0.0.1]);
9005 Tue, 05 Apr 2005 14:51:40 +0200 (SAST)
9006 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9008 X-BeenThere: courses@linuxchix.org
9009 X-Mailman-Version: 2.1.5
9011 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9012 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9013 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9014 List-Archive: <http://linuxchix.org/pipermail/courses>
9015 List-Post: <mailto:courses@linuxchix.org>
9016 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9017 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9018 <mailto:courses-request@linuxchix.org?subject=subscribe>
9019 X-List-Received-Date: Tue, 05 Apr 2005 12:57:48 -0000
9020 Content-Length: 3005
9023 Thanks all for the course. I am so excited to get this opportunity to learn
9024 Perl and share with experienced Linux people.
9033 ---------- Original Message -----------
9034 From: Magni Onsoien <magnio+courses@pvv.org>
9035 To: courses@linuxchix.org
9036 Sent: Tue, 5 Apr 2005 09:56:04 +0200
9037 Subject: Re: [Courses] [Perl] Part 1: Getting Started
9039 > On 2005-04-05 16:46:08 +1000, Sue Stones said:
9040 > > Colleen Hatfield wrote:
9041 > > >If you're the only
9042 > > >person that ever touches your machine, it's probably not too
9043 > > >dangerous; otherwise it should definitely be avoided. ;-)
9045 > > Personally, yes I am the only one that has ever used my machine. (My
9046 > > only housemate for the last decade has been a dog that had absolutely no
9047 > > interest in using the computer) But if I was in a position where
9048 > > someone else was going to use my computer even for a few hours, I would
9049 > > create an account for them.
9051 > I do that before they are getting close to anythng but a locked
9054 > But imagine that you have created her an account and you are using
9055 > the computer together (either simultanously, like a server, or
9056 > sequentally). The other person make a script that do something
9057 > pretty evil, like 'rm -rf $HOME 2>&1>/dev/null' (which will delete
9058 > your homedir and redirect all errors to /dev/null so you won't see
9059 > them). This script is called ls and is put into /tmp (a directory
9060 > writeable for anyone) and made executable for all.
9062 > I guess you can figure out what happens when you go to /tmp and type
9063 > 'ls' to see what's there, if you have . first in your $PATH? :-)
9065 > So making a separate account really doesn't matter if it's a multi user
9066 > system, since in reality you'll be sharing directories and stuff anyway.
9067 > And DON'T remove the world-write permissions for /tmp, it will break
9068 > your system :-) (Not breaking in the same sense as rm -rf $HOME,
9071 > And for the record, the same will of course happen if the user puts
9072 > her malicious script in her $HOME-directory and asks you to "have a
9073 > look at my homedir, something seems to be wrong there".
9075 > The best thing to do is to stick with ./script and avoid . in the $PATH.
9076 > If you make scripts, it's safer to add their directory at the end of
9077 > the $PATH (with e.g. 'export
9078 > PATH=$PATH:$HOME/myscriptdir:$HOME/perlcourse'). If you WANT to have
9079 > . in your $PATH, add it to the end of the $PATH
9080 > ('export PATH=$PATH:.') so you won't execute scripts and binaries
9081 > with the same name as system programs when you didn't want to. If
9082 > you insist on having your own 'ls', you could either use ./ls or add
9083 > an alias for it in .bash_profile ('alias ls=$HOME/myscripts/ls').
9085 > [Ok, this isn't on perl anymore. Sorry.]
9089 > sash is very good for you.
9090 > _______________________________________________
9091 > Courses mailing list
9092 > Courses@linuxchix.org
9093 > http://mailman.linuxchix.org/mailman/listinfo/courses
9094 ------- End of Original Message -------
9097 From avery@u.washington.edu Wed Apr 6 02:57:01 2005
9098 Return-Path: <avery@u.washington.edu>
9099 X-Original-To: courses@linuxchix.org
9100 Delivered-To: courses@linuxchix.org
9101 Received: from localhost (localhost [127.0.0.1])
9102 by www.linuxchix.org (Postfix) with ESMTP id E3D47273086
9103 for <courses@linuxchix.org>; Wed, 6 Apr 2005 02:57:00 +1000 (EST)
9104 Received: from www.linuxchix.org ([127.0.0.1])
9105 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9106 id 23245-09 for <courses@linuxchix.org>;
9107 Wed, 6 Apr 2005 02:57:00 +1000 (EST)
9108 Received: from ms-smtp-04-eri0.southeast.rr.com
9109 (ms-smtp-04-lbl.southeast.rr.com [24.25.9.103])
9110 by www.linuxchix.org (Postfix) with ESMTP id 15470272FE9
9111 for <courses@linuxchix.org>; Wed, 6 Apr 2005 02:56:59 +1000 (EST)
9112 Received: from gesh.nc.rr.com (cpe-066-057-131-165.nc.res.rr.com
9114 by ms-smtp-04-eri0.southeast.rr.com (8.12.10/8.12.7) with ESMTP id
9116 for <courses@linuxchix.org>; Tue, 5 Apr 2005 12:56:47 -0400 (EDT)
9117 Received: from avery by gesh.nc.rr.com with local (Exim 3.36 #1 (Debian))
9119 for <courses@linuxchix.org>; Tue, 05 Apr 2005 12:59:22 -0400
9120 Date: Tue, 5 Apr 2005 12:59:22 -0400
9121 To: courses@linuxchix.org
9122 Subject: Re: [Courses] [Perl] Part 1: Getting Started
9123 Message-ID: <20050405165922.GF18335@gesh.kejia>
9124 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
9125 <20050405044329.A12674@evita.devdas.geek>
9126 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
9127 <425216BA.1000601@spin.net.au>
9128 <133ac4eb05040422325c54b7e0@mail.gmail.com>
9129 <42523430.2020705@spin.net.au> <20050405075604.GH2091@pvv.org>
9131 Content-Type: text/plain; charset=us-ascii; format=flowed
9132 Content-Disposition: inline
9133 In-Reply-To: <20050405075604.GH2091@pvv.org>
9134 User-Agent: Mutt/1.5.8i
9135 From: Avery Ke <avery@u.washington.edu>
9136 X-Virus-Scanned: Symantec AntiVirus Scan Engine
9137 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9139 X-BeenThere: courses@linuxchix.org
9140 X-Mailman-Version: 2.1.5
9142 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9143 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9144 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9145 List-Archive: <http://linuxchix.org/pipermail/courses>
9146 List-Post: <mailto:courses@linuxchix.org>
9147 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9148 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9149 <mailto:courses-request@linuxchix.org?subject=subscribe>
9150 X-List-Received-Date: Tue, 05 Apr 2005 16:57:01 -0000
9151 Content-Length: 2614
9154 Is anyone else wanting to try out the perl code in the lessons from
9155 emacs? I wanted to try out script in perl as I was reviwing it in the
9156 lesson, without having to save to separate file. After googling around
9157 for a bit (keywords: compile perl emacs), I found what I wanted at:
9158 http://www.phyast.pitt.edu/~tanc/scripts/.emacs.html
9160 If you put the following in your .emacs file, you can highlight a
9161 portion of perl script and send it to perl. I added the last lines
9162 (add-hook 'cperl-mode hook) so it would work in both perl-mode and
9165 ;; ------------------------------------------------------------------
9167 ;; from http://www.phyast.pitt.edu/~tanc/scripts/.emacs.html
9168 ;; ------------------------------------------------------------------
9170 ;; sburke@netadventure.net
9172 "Run selected region as Perl code"
9174 (shell-command-on-region (mark) (point) "perl ")
9177 (add-hook 'perl-mode-hook
9179 (local-set-key "\M-p" 'perl-eval)
9183 (add-hook 'cperl-mode-hook
9185 (local-set-key "\M-p" 'perl-eval)
9188 ;; ------------------------------------------------------------------
9191 To send the whole buffer to perl, I found the following at:
9192 http://lists.gnu.org/archive/html/help-gnu-emacs/2003-08/msg00114.html
9194 ;; -------------------------------------------------------------------
9195 ;;Compilation mode for perl files
9196 ;;http://lists.gnu.org/archive/html/help-gnu-emacs/2003-08/msg00114.html
9197 ;; -------------------------------------------------------------------
9199 (add-to-list 'compilation-error-regexp-alist '(".* \\([-a-zA-Z._/]+\\) line
9200 \\([0-9]+\\)." 1 2))
9201 (defun perl-compile (&optional prefix)
9204 (let* ((include-path (if (eq window-system 'w32) "-I%SP%" "-I$SP"))
9205 (compile-command (concat "perl "include-path " -cw "
9206 (buffer-file-name)))
9207 (compilation-read-command prefix))
9208 (call-interactively 'compile)))
9209 (require 'perl-mode)
9210 (require 'cperl-mode)
9211 (define-key perl-mode-map "\C-c\C-c" 'perl-compile)
9212 (define-key cperl-mode-map "\C-c\C-c" 'perl-compile)
9213 ;; ------------------------------------------------------------------
9215 There is also mode-compile-el at:
9216 http://www.tls.cena.fr/~boubaker/Emacs/
9217 I downloaded mode-compile.el, but got some error messages when I tried to
9218 byte-compile-file in Emacs. So I'm using the work-arounds above, which
9219 work fine for my needs right now.
9224 From svaksha@gmail.com Sun Apr 10 03:13:06 2005
9225 Return-Path: <svaksha@gmail.com>
9226 X-Original-To: courses@linuxchix.org
9227 Delivered-To: courses@linuxchix.org
9228 Received: from localhost (localhost [127.0.0.1])
9229 by www.linuxchix.org (Postfix) with ESMTP id 0FC472730E0
9230 for <courses@linuxchix.org>; Sun, 10 Apr 2005 03:13:06 +1000 (EST)
9231 Received: from www.linuxchix.org ([127.0.0.1])
9232 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9233 id 25847-06 for <courses@linuxchix.org>;
9234 Sun, 10 Apr 2005 03:13:05 +1000 (EST)
9235 X-Greylist: delayed 396 seconds by postgrey-1.17 at nest;
9236 Sun, 10 Apr 2005 03:13:05 EST
9237 Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.196])
9238 by www.linuxchix.org (Postfix) with ESMTP id 11164273060
9239 for <courses@linuxchix.org>; Sun, 10 Apr 2005 03:13:04 +1000 (EST)
9240 Received: by rproxy.gmail.com with SMTP id f1so1573669rne
9241 for <courses@linuxchix.org>; Sat, 09 Apr 2005 10:12:58 -0700 (PDT)
9242 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com;
9243 h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references;
9244 b=pwAzD91WS8mXOkxic65XDOSIg8yVWquvJwuVICXav91nlcm+LPpJpPbopGYb42IhW2xV0dm9+0JJRMpRPEfnDJ22fNhOFcg8lph17iExEY2QYiqLxM53EVIgmByctZ/l2XxY5W0mUSmaST8MtbiYMHxdVg9ruVpWW9lLS3Gs6q8=
9245 Received: by 10.38.153.43 with SMTP id a43mr2359052rne;
9246 Sat, 09 Apr 2005 10:06:14 -0700 (PDT)
9247 Received: by 10.38.13.42 with HTTP; Sat, 9 Apr 2005 10:06:14 -0700 (PDT)
9248 Message-ID: <12470af005040910064c172220@mail.gmail.com>
9249 Date: Sat, 9 Apr 2005 13:06:14 -0400
9250 From: ||svaksha|| <svaksha@gmail.com>
9251 To: Avery Ke <avery@u.washington.edu>
9252 Subject: Re: [Courses] [Perl] Part 1: Getting Started
9253 In-Reply-To: <20050405165922.GF18335@gesh.kejia>
9255 Content-Type: text/plain; charset=ISO-8859-1
9256 Content-Transfer-Encoding: 7bit
9257 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
9258 <20050405044329.A12674@evita.devdas.geek>
9259 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
9260 <425216BA.1000601@spin.net.au>
9261 <133ac4eb05040422325c54b7e0@mail.gmail.com>
9262 <42523430.2020705@spin.net.au> <20050405075604.GH2091@pvv.org>
9263 <20050405165922.GF18335@gesh.kejia>
9264 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9266 Cc: courses@linuxchix.org
9267 X-BeenThere: courses@linuxchix.org
9268 X-Mailman-Version: 2.1.5
9270 Reply-To: ||svaksha|| <svaksha@gmail.com>
9271 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9272 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9273 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9274 List-Archive: <http://linuxchix.org/pipermail/courses>
9275 List-Post: <mailto:courses@linuxchix.org>
9276 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9277 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9278 <mailto:courses-request@linuxchix.org?subject=subscribe>
9279 X-List-Received-Date: Sat, 09 Apr 2005 17:13:06 -0000
9283 On Apr 5, 2005 12:59 PM, Avery Ke <avery@u.washington.edu> wrote:
9284 > Is anyone else wanting to try out the perl code in the lessons from
9287 i used emacs and even after changing permissions got this error :
9288 Can't open perl script "hello1.pl": No such file or directory
9289 am sure i missed something ... ?
9294 From avery@u.washington.edu Wed Apr 6 02:39:24 2005
9295 Return-Path: <avery@u.washington.edu>
9296 X-Original-To: courses@linuxchix.org
9297 Delivered-To: courses@linuxchix.org
9298 Received: from localhost (localhost [127.0.0.1])
9299 by www.linuxchix.org (Postfix) with ESMTP id 88C5B273086
9300 for <courses@linuxchix.org>; Wed, 6 Apr 2005 02:39:24 +1000 (EST)
9301 Received: from www.linuxchix.org ([127.0.0.1])
9302 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9303 id 23142-09 for <courses@linuxchix.org>;
9304 Wed, 6 Apr 2005 02:39:24 +1000 (EST)
9305 Received: from ms-smtp-03-eri0.southeast.rr.com
9306 (ms-smtp-03-lbl.southeast.rr.com [24.25.9.102])
9307 by www.linuxchix.org (Postfix) with ESMTP id BB5C3272FE9
9308 for <courses@linuxchix.org>; Wed, 6 Apr 2005 02:39:23 +1000 (EST)
9309 Received: from gesh.nc.rr.com (cpe-066-057-131-165.nc.res.rr.com
9311 by ms-smtp-03-eri0.southeast.rr.com (8.12.10/8.12.7) with ESMTP id
9313 for <courses@linuxchix.org>; Tue, 5 Apr 2005 12:39:15 -0400 (EDT)
9314 Received: from avery by gesh.nc.rr.com with local (Exim 3.36 #1 (Debian))
9316 for <courses@linuxchix.org>; Tue, 05 Apr 2005 12:41:24 -0400
9317 Date: Tue, 5 Apr 2005 12:41:24 -0400
9318 To: courses@linuxchix.org
9319 Subject: Re: [Courses] [Perl] Part 1: Getting Started
9320 Message-ID: <20050405164124.GE18335@gesh.kejia>
9321 References: <Pine.LNX.4.60.0504041712080.18602@tauri.jsnider.net>
9322 <20050405044329.A12674@evita.devdas.geek>
9323 <Pine.LNX.4.60.0504042318120.19968@tauri.jsnider.net>
9324 <425216BA.1000601@spin.net.au>
9325 <133ac4eb05040422325c54b7e0@mail.gmail.com>
9326 <42523430.2020705@spin.net.au>
9328 Content-Type: text/plain; charset=us-ascii; format=flowed
9329 Content-Disposition: inline
9330 In-Reply-To: <42523430.2020705@spin.net.au>
9331 User-Agent: Mutt/1.5.8i
9332 From: Avery Ke <avery@u.washington.edu>
9333 X-Virus-Scanned: Symantec AntiVirus Scan Engine
9334 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9336 X-BeenThere: courses@linuxchix.org
9337 X-Mailman-Version: 2.1.5
9339 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9340 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9341 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9342 List-Archive: <http://linuxchix.org/pipermail/courses>
9343 List-Post: <mailto:courses@linuxchix.org>
9344 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9345 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9346 <mailto:courses-request@linuxchix.org?subject=subscribe>
9347 X-List-Received-Date: Tue, 05 Apr 2005 16:39:24 -0000
9352 >Personally, yes I am the only one that has ever used my machine. (My
9353 >only housemate for the last decade has been a dog that had absolutely no
9354 >interest in using the computer) But if I was in a position where
9355 >someone else was going to use my computer even for a few hours, I would
9356 >create an account for them.
9358 Are you sure your dog has no interest? <grin> My 17 year old cat
9359 routinely steps across my keyboard, simultaniously presses
9360 Control-Alt-Fx, gets himself into a virtual terminal, and apparently
9361 tries to log in. He's done this so often that I think I should set up
9362 an account for him, and give him the password. :) Then again, he'd
9363 probably swipe my credit card and order a couple of cases of wet
9364 cat food and an automatic can opener.
9369 From dan@cellectivity.com Fri Apr 8 23:53:49 2005
9370 Return-Path: <dan@cellectivity.com>
9371 X-Original-To: courses@linuxchix.org
9372 Delivered-To: courses@linuxchix.org
9373 Received: from localhost (localhost [127.0.0.1])
9374 by www.linuxchix.org (Postfix) with ESMTP id E826F2730D7
9375 for <courses@linuxchix.org>; Fri, 8 Apr 2005 23:53:48 +1000 (EST)
9376 Received: from www.linuxchix.org ([127.0.0.1])
9377 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9378 id 16705-10 for <courses@linuxchix.org>;
9379 Fri, 8 Apr 2005 23:53:48 +1000 (EST)
9380 Received: from cellectivity.com (unknown [212.18.242.163])
9381 by www.linuxchix.org (Postfix) with ESMTP id CD7002730D3
9382 for <courses@linuxchix.org>; Fri, 8 Apr 2005 23:53:46 +1000 (EST)
9383 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
9384 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
9385 Fri, 8 Apr 2005 14:54:30 +0100
9386 Message-ID: <42568CE4.4060609@cellectivity.com>
9387 Date: Fri, 08 Apr 2005 14:53:40 +0100
9388 From: Dan <dan@cellectivity.com>
9389 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
9390 rv:1.7.5) Gecko/20050105 Debian/1.7.5-1
9391 X-Accept-Language: en
9393 To: courses@linuxchix.org
9394 Subject: [Courses] [Perl] Part 2: Scalar Variables
9395 Content-Type: text/plain; charset=us-ascii; format=flowed
9396 Content-Transfer-Encoding: 7bit
9397 X-OriginalArrivalTime: 08 Apr 2005 13:54:30.0703 (UTC)
9398 FILETIME=[7CAB33F0:01C53C42]
9399 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9401 X-BeenThere: courses@linuxchix.org
9402 X-Mailman-Version: 2.1.5
9404 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9405 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9406 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9407 List-Archive: <http://linuxchix.org/pipermail/courses>
9408 List-Post: <mailto:courses@linuxchix.org>
9409 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9410 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9411 <mailto:courses-request@linuxchix.org?subject=subscribe>
9412 X-List-Received-Date: Fri, 08 Apr 2005 13:53:49 -0000
9413 Content-Length: 5770
9416 LinuxChix Perl Course Part 2: Scalar Variables
9423 6) Answers to previous exercises
9428 ----------------------------------------
9432 There are four basic data types in Perl. They are:
9433 a) scalar - A single chunk of data
9434 b) array - A bunch of scalars, indexed by a number
9435 c) hash (hashtable) - A bunch of scalars, indexed by another scalar
9436 d) handle - A pointer enabling the opening of resources from the
9437 operating system (files, directories, etc.)
9439 For right now we're only going to look at scalars.
9441 ----------------------------------------
9445 A scalar may represent text or a number. A scalar variable will
9446 always begin with a dollar sign.
9448 To declare a scalar variable (or any other, for that matter), use the
9451 my $foo; # Initialises to the undefined value.
9452 my $bar = "some text"; # Initialises to a string (text).
9453 my $baz = -25; # Initialises to an integer.
9454 my $pi = 3.14; # Initialises to a floating point.
9456 Once you have declared a scalar variable, you can assign it any
9457 scalar value you like. There are no type restrictions.
9461 $a = $a + $b; # Now $a=30.
9462 $b = "a bunch of text"; # Now $b is a string. That's okay.
9463 my $c = "I see $a stars"; # Now $c="I see 30 stars".
9464 $b = undef; # Now $b has the undefined value.
9466 There are also no restrictions on the size of strings (text).
9467 Standard restrictions apply on the size of numeric types (for
9468 example, I think that integers are 64 bits in Perl, so an integer
9471 ----------------------------------------
9475 The reason that there are no type restrictions on scalars is that all
9476 scalars are inherently text (except maybe references, but we'll get
9477 to that later). Some operators just extract numerical information
9478 from that text. For example:
9480 my $a = 2 + 5; # Now $a=7
9481 my $b = "2" + "5"; # Okay, same as previous.
9482 my $c = " 2 " + " 5 "; # Okay, same as previous.
9483 my $d = "6" / "3"; # Now $d=2
9485 Because a scalar can represent so many types, the comparison
9486 operators could lead to ambiguity. For example, when performing the
9487 comparison "3 < 12", the result depends on whether to we want to
9488 compare as a number or as a string (because the first character of
9489 "12" is "1", which is less than "3").
9491 To resolve this ambiguity, Perl has both lexical (string) and
9492 numerical comparison operators:
9493 Numerical Lexical Meaning
9497 <= le less than or equal to
9499 >= ge greater than or equal to
9501 In addition to these, Perl has almost all the operators found in C++:
9502 arithmetic, binary and logical. The few exceptions include the unary
9503 "&" (memory address, which in Perl is a backslash) and cast
9504 operations (which don't exist in Perl).
9506 In addition, here are two other Perl operators not found in C++:
9507 . (period) string concatenation (don't use + for this)
9508 ** (two astrisks) exponentiation (don't use ^ for this)
9510 ----------------------------------------
9514 Perl treats single and double quotes similarly to a shell script:
9517 my $double = "Cute $animal!\n"; # Cute dog![newline]
9518 my $single = 'Cute $animal!\n'; # Cute [dollar]animal![slash]n
9520 Perl supports a variety of other types of string literals, such as
9528 If you're not familiar with "here documents", don't worry about it.
9530 ----------------------------------------
9534 a) Write a program called 'average.pl' to calculate the average of
9535 the following numbers: 23, 28, 31, 17, 1
9537 (Remember an average is where you add the numbers together and divide
9538 by the amount you have, this is learning perl not maths 101! On the
9539 other hand, the maths is normally the hardest part.)
9541 b) Consider the following program:
9546 my $foo = 'Suzy' x 4;
9549 What does the "x" operator do? Why can't we write "4 x 'Suzy'" as
9550 well as "'Suzy' x 4"?
9552 c) Consider the following program:
9557 my $a = q/This is string A/; # using slash
9558 my $b = q!This is string B!; # using exclamation point
9559 my $c = q:This is string C:; # using colon
9560 print "$a\n$b\n$c\n";
9562 What does the "q" operator do? What is the advantage of using it
9563 rather than a single quote?
9565 ----------------------------------------
9567 6) Answers to previous exercises
9569 a) "\n" is a newline.
9571 b) Perl stands for Practical Extraction and Report Language, or
9572 Pathologically Eclectic Rubbish Lister depending on your point of
9575 ----------------------------------------
9579 A big thank you to Jacinta Richardson for suggestions and
9580 proofreading. More advanced Perl users might want to check out the
9581 free material from Perl Training Australia
9582 <http://www.perltraining.com.au/>, which she is a part of.
9584 Other contributors include Meryll Larkin.
9586 ----------------------------------------
9590 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
9591 Richter and Alice Wood, and is released under the same license as
9592 Perl itself (Artistic License or GPL, your choice). This is the
9593 license of choice to make it easy for other people to integrate your
9594 Perl code/documentation into their own projects. It is not generally
9595 used in projects unrelated to Perl.
9597 From kproot@nerim.net Sun Apr 10 04:18:37 2005
9598 Return-Path: <kproot@nerim.net>
9599 X-Original-To: courses@linuxchix.org
9600 Delivered-To: courses@linuxchix.org
9601 Received: from localhost (localhost [127.0.0.1])
9602 by www.linuxchix.org (Postfix) with ESMTP id A073F2730E0
9603 for <courses@linuxchix.org>; Sun, 10 Apr 2005 04:18:37 +1000 (EST)
9604 Received: from www.linuxchix.org ([127.0.0.1])
9605 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9606 id 26563-03 for <courses@linuxchix.org>;
9607 Sun, 10 Apr 2005 04:18:37 +1000 (EST)
9608 Received: from kraid.nerim.net (smtp-106-saturday.nerim.net [62.4.16.106])
9609 by www.linuxchix.org (Postfix) with ESMTP id 18DFD2730D7
9610 for <courses@linuxchix.org>; Sun, 10 Apr 2005 04:18:37 +1000 (EST)
9611 Received: from gelydh (jdelvare.net1.nerim.net [213.41.160.68])
9612 by kraid.nerim.net (Postfix) with ESMTP id 3244F418E3
9613 for <courses@linuxchix.org>; Sat, 9 Apr 2005 20:18:24 +0200 (CEST)
9614 Date: Sat, 9 Apr 2005 20:18:22 +0200
9615 From: Karine Delvare <kproot@nerim.net>
9616 To: courses@linuxchix.org
9617 Subject: Re: [Courses] [Perl] Part 2: Scalar Variables
9618 Message-Id: <20050409201822.7b4283e5.kproot@nerim.net>
9619 In-Reply-To: <42568CE4.4060609@cellectivity.com>
9620 References: <42568CE4.4060609@cellectivity.com>
9621 X-Mailer: Sylpheed version 1.9.7 (GTK+ 2.6.4; i686-pc-linux-gnu)
9623 Content-Type: text/plain; charset=US-ASCII
9624 Content-Transfer-Encoding: 7bit
9625 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9627 X-BeenThere: courses@linuxchix.org
9628 X-Mailman-Version: 2.1.5
9630 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9631 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9632 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9633 List-Archive: <http://linuxchix.org/pipermail/courses>
9634 List-Post: <mailto:courses@linuxchix.org>
9635 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9636 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9637 <mailto:courses-request@linuxchix.org?subject=subscribe>
9638 X-List-Received-Date: Sat, 09 Apr 2005 18:18:37 -0000
9639 Content-Length: 1918
9642 Hello Dan, thanks for the Perl course!
9644 Below is my homework, please tell me if I'm sending this too early.
9648 > a) Write a program called 'average.pl' to calculate the average of
9649 > the following numbers: 23, 28, 31, 17, 1
9661 my $total = $num1 + $num2 + $num3 + $num4 + $num5;
9663 my $average = $total / 5;
9665 print "Average: $average\n";
9667 > b) Consider the following program:
9669 > #!/usr/bin/perl -w
9672 > my $foo = 'Suzy' x 4;
9675 > What does the "x" operator do? Why can't we write "4 x 'Suzy'" as
9676 > well as "'Suzy' x 4"?
9678 Considering that the program output was SuzySuzySuzySuzy, the "x" operator seems to concatenate a string with itself, as many times as asked for (don't know if there is a proper english term for that, but that operator is cool!)
9680 I tried to think a bit about 4 x 'Suzy'. Perl has to know which is the string and which is the "multiplicator". In that example it could guess it, but what about 4 x 3? 4 x 3 outputs "444", but 3 x 4 outputs "3333". So operands need to be ordered.
9682 > c) Consider the following program:
9684 > #!/usr/bin/perl -w
9687 > my $a = q/This is string A/; # using slash
9688 > my $b = q!This is string B!; # using exclamation point
9689 > my $c = q:This is string C:; # using colon
9690 > print "$a\n$b\n$c\n";
9692 > What does the "q" operator do? What is the advantage of using it
9693 > rather than a single quote?
9695 Another cool operator! The output was:
9699 So it allows to replace the quote delimiters with any character. It can be an advantage if the string to quote containes quotes and/or double-quotes (makes me think about the way you can change the regular expressions delimiters, but I'm pretty sure we will encounter these later!)
9703 From dan@cellectivity.com Mon Apr 11 18:41:10 2005
9704 Return-Path: <dan@cellectivity.com>
9705 X-Original-To: courses@linuxchix.org
9706 Delivered-To: courses@linuxchix.org
9707 Received: from localhost (localhost [127.0.0.1])
9708 by www.linuxchix.org (Postfix) with ESMTP id 5454F2730E5
9709 for <courses@linuxchix.org>; Mon, 11 Apr 2005 18:41:10 +1000 (EST)
9710 Received: from www.linuxchix.org ([127.0.0.1])
9711 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9712 id 07283-10 for <courses@linuxchix.org>;
9713 Mon, 11 Apr 2005 18:41:10 +1000 (EST)
9714 Received: from cellectivity.com (unknown [212.18.242.163])
9715 by www.linuxchix.org (Postfix) with ESMTP id 4B4BE272FF1
9716 for <courses@linuxchix.org>; Mon, 11 Apr 2005 18:41:08 +1000 (EST)
9717 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
9718 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
9719 Mon, 11 Apr 2005 09:41:55 +0100
9720 Message-ID: <425A3820.5070501@cellectivity.com>
9721 Date: Mon, 11 Apr 2005 09:41:04 +0100
9722 From: Dan <dan@cellectivity.com>
9723 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
9724 rv:1.7.5) Gecko/20050105 Debian/1.7.5-1
9725 X-Accept-Language: en
9727 To: courses@linuxchix.org
9728 Subject: Re: [Courses] [Perl] Part 2: Scalar Variables
9729 References: <42568CE4.4060609@cellectivity.com>
9730 <20050409201822.7b4283e5.kproot@nerim.net>
9731 In-Reply-To: <20050409201822.7b4283e5.kproot@nerim.net>
9732 Content-Type: text/plain; charset=us-ascii; format=flowed
9733 Content-Transfer-Encoding: 7bit
9734 X-OriginalArrivalTime: 11 Apr 2005 08:41:55.0750 (UTC)
9735 FILETIME=[5115BC60:01C53E72]
9736 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9738 X-BeenThere: courses@linuxchix.org
9739 X-Mailman-Version: 2.1.5
9741 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9742 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9743 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9744 List-Archive: <http://linuxchix.org/pipermail/courses>
9745 List-Post: <mailto:courses@linuxchix.org>
9746 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9747 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9748 <mailto:courses-request@linuxchix.org?subject=subscribe>
9749 X-List-Received-Date: Mon, 11 Apr 2005 08:41:10 -0000
9750 Content-Length: 1190
9753 > Below is my homework, please tell me if I'm sending this too early.
9755 In fact, I think it would be better to wait 24 hours or so before
9756 sending your answers, just to give everyone a chance to think things
9757 over, regardless of time zone. Don't hurry for me, because I don't read
9758 my e-mail on weekends (which I know is unusual for a programmer. :-) )
9760 But your answers are right on. This one is particularly insightful:
9762 > Considering that the program output was SuzySuzySuzySuzy, the "x"
9763 > operator seems to concatenate a string with itself, as many
9764 > times as asked for (don't know if there is a proper english term
9765 > for that, but that operator is cool!)
9767 > I tried to think a bit about 4 x 'Suzy'. Perl has to know which is
9768 > the string and which is the "multiplicator". In that example it could
9769 > guess it, but what about 4 x 3? 4 x 3 outputs "444", but 3 x 4
9770 > outputs "3333". So operands need to be ordered.
9773 [Larry] Wall [inventor of Perl] believes that people think about
9774 things in different ways, that natural languages accommodate many
9775 mindsets, and that programming languages should too.
9776 - Jon Udell, in his essay, "A Perl Hacker in the Land of Python"
9778 From fazia@faziarizvi.net Wed Apr 13 06:36:43 2005
9779 Return-Path: <fazia@faziarizvi.net>
9780 X-Original-To: courses@linuxchix.org
9781 Delivered-To: courses@linuxchix.org
9782 Received: from localhost (localhost [127.0.0.1])
9783 by www.linuxchix.org (Postfix) with ESMTP id A84CD2730E6
9784 for <courses@linuxchix.org>; Wed, 13 Apr 2005 06:36:43 +1000 (EST)
9785 Received: from www.linuxchix.org ([127.0.0.1])
9786 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9787 id 20763-07 for <courses@linuxchix.org>;
9788 Wed, 13 Apr 2005 06:36:43 +1000 (EST)
9789 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
9790 by www.linuxchix.org (Postfix) with ESMTP id 2C25427307A
9791 for <courses@linuxchix.org>; Wed, 13 Apr 2005 06:36:43 +1000 (EST)
9792 Received: from tauri.jsnider.net ([66.194.162.10])
9793 by tauri.jsnider.net with esmtp (Exim 4.44) id 1DLS7c-0000uB-KI
9794 for courses@linuxchix.org; Tue, 12 Apr 2005 15:36:32 -0500
9795 Date: Tue, 12 Apr 2005 15:36:32 -0500 (CDT)
9796 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
9797 X-X-Sender: fazia@tauri.jsnider.net
9798 To: courses@linuxchix.org
9799 Subject: Re: [Courses] [Perl] Part 2: Scalar Variables
9800 In-Reply-To: <20050409201822.7b4283e5.kproot@nerim.net>
9801 Message-ID: <Pine.LNX.4.60.0504121535210.3438@tauri.jsnider.net>
9802 References: <42568CE4.4060609@cellectivity.com>
9803 <20050409201822.7b4283e5.kproot@nerim.net>
9805 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
9806 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9808 X-BeenThere: courses@linuxchix.org
9809 X-Mailman-Version: 2.1.5
9811 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9812 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9813 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9814 List-Archive: <http://linuxchix.org/pipermail/courses>
9815 List-Post: <mailto:courses@linuxchix.org>
9816 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9817 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9818 <mailto:courses-request@linuxchix.org?subject=subscribe>
9819 X-List-Received-Date: Tue, 12 Apr 2005 20:36:43 -0000
9824 On Sat, 9 Apr 2005, Karine Delvare wrote:
9826 > Hello Dan, thanks for the Perl course!
9828 > Below is my homework, please tell me if I'm sending this too early.
9832 >> a) Write a program called 'average.pl' to calculate the average of
9833 >> the following numbers: 23, 28, 31, 17, 1
9835 > #!/usr/bin/perl -w
9845 > my $total = $num1 + $num2 + $num3 + $num4 + $num5;
9847 > my $average = $total / 5;
9849 > print "Average: $average\n";
9851 Nifty. I eneded up doing this:
9855 # Perl Course Program
9856 # Part 2: Scalar Variables
9857 # Exercise "a" : Write a program called 'average.pl' to calculate the average of
9858 # the following numbers: 23, 28, 31, 17, 1
9862 $sum = 23 + 28 + 31 + 17 + 1;
9864 $average = $sum / 5;
9866 print "The average of 23, 28, 31, 17 and 1 is $average\n";
9874 ) www.faziarizvi.net
9878 From swalk@ya.com Sat Apr 9 22:24:50 2005
9879 Return-Path: <swalk@ya.com>
9880 X-Original-To: courses@linuxchix.org
9881 Delivered-To: courses@linuxchix.org
9882 Received: from localhost (localhost [127.0.0.1])
9883 by www.linuxchix.org (Postfix) with ESMTP id 4FE012730D9
9884 for <courses@linuxchix.org>; Sat, 9 Apr 2005 22:24:50 +1000 (EST)
9885 Received: from www.linuxchix.org ([127.0.0.1])
9886 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9887 id 24242-08 for <courses@linuxchix.org>;
9888 Sat, 9 Apr 2005 22:24:50 +1000 (EST)
9889 X-Greylist: delayed 2846 seconds by postgrey-1.17 at nest;
9890 Sat, 09 Apr 2005 22:24:49 EST
9891 Received: from rfallback.ya.com (rfallback.ya.com [62.151.4.18])
9892 by www.linuxchix.org (Postfix) with ESMTP id ABC682730D4
9893 for <courses@linuxchix.org>; Sat, 9 Apr 2005 22:24:49 +1000 (EST)
9894 Received: from [192.168.1.192] (helo=smtp.ya.com)
9895 by rfallback.ya.com with esmtp id 1DKEGi-0005Eb-00
9896 for courses@linuxchix.org; Sat, 09 Apr 2005 13:36:52 +0200
9897 Received: from [217.127.1.90] (helo=hitchhiker.ya.com)
9898 by smtp.ya.com with esmtp id 1DKEGZ-00018e-00
9899 for courses@linuxchix.org; Sat, 09 Apr 2005 13:36:43 +0200
9900 Message-Id: <6.2.1.2.2.20050409125318.0291c6d8@pop.adsl.ya.com>
9901 X-Mailer: QUALCOMM Windows Eudora Version 6.2.1.2
9902 Date: Sat, 09 Apr 2005 13:13:55 +0200
9903 To: courses@linuxchix.org
9904 From: Steven Walker <swalk@ya.com>
9905 Subject: Re: [Courses] [Perl] Part 1:
9906 In-Reply-To: <20050409020004.2BD5E2730E5@www.linuxchix.org>
9907 References: <20050409020004.2BD5E2730E5@www.linuxchix.org>
9909 Content-Type: text/plain; charset="us-ascii"; format=flowed
9910 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9912 X-BeenThere: courses@linuxchix.org
9913 X-Mailman-Version: 2.1.5
9915 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9916 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9917 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9918 List-Archive: <http://linuxchix.org/pipermail/courses>
9919 List-Post: <mailto:courses@linuxchix.org>
9920 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9921 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9922 <mailto:courses-request@linuxchix.org?subject=subscribe>
9923 X-List-Received-Date: Sat, 09 Apr 2005 12:24:50 -0000
9927 Just letting you know that I am here and following the course as a newbie
9928 to programming. So far it all seems OK and there is not much to say. I am
9929 sure I will be squealing with pain before long :)
9931 My only comment is on the homework average problem. I did this
9935 my $sum = 23 + 28 + 31 + 17 + 1;
9936 print "The total is $sum \n";
9939 print "The average is $mean \n";
9941 Which works fine but is a bit of a cop out as I counted the number of items
9942 summed and gave the result to the program. I expect that later in the
9943 course there will be a way of counting the number of entries in a variable.
9948 From mrei0999@rz.uni-hildesheim.de Sun Apr 10 01:00:48 2005
9949 Return-Path: <mrei0999@rz.uni-hildesheim.de>
9950 X-Original-To: courses@linuxchix.org
9951 Delivered-To: courses@linuxchix.org
9952 Received: from localhost (localhost [127.0.0.1])
9953 by www.linuxchix.org (Postfix) with ESMTP id 561242730D9
9954 for <courses@linuxchix.org>; Sun, 10 Apr 2005 01:00:48 +1000 (EST)
9955 Received: from www.linuxchix.org ([127.0.0.1])
9956 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
9957 id 24994-09 for <courses@linuxchix.org>;
9958 Sun, 10 Apr 2005 01:00:48 +1000 (EST)
9959 X-Greylist: delayed 2743 seconds by postgrey-1.17 at nest;
9960 Sun, 10 Apr 2005 01:00:47 EST
9961 Received: from batman.rz.uni-hildesheim.de (batman.rz.uni-hildesheim.de
9963 by www.linuxchix.org (Postfix) with ESMTP id C007C27307A
9964 for <courses@linuxchix.org>; Sun, 10 Apr 2005 01:00:47 +1000 (EST)
9965 Envelope-to: courses@linuxchix.org
9966 Received: from [217.81.34.150] (helo=[192.168.1.101])
9967 by batman.rz.uni-hildesheim.de with asmtp (Exim 4.41)
9969 for courses@linuxchix.org; Sat, 09 Apr 2005 16:14:51 +0200
9970 Message-ID: <4257E351.4060306@rz.uni-hildesheim.de>
9971 Date: Sat, 09 Apr 2005 16:14:41 +0200
9972 From: Meike Reichle <mrei0999@rz.uni-hildesheim.de>
9973 User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
9974 X-Accept-Language: de-DE, de, en-us, en
9976 To: courses@linuxchix.org
9977 Subject: Re: [Courses] [Perl] Part 1:
9978 References: <20050409020004.2BD5E2730E5@www.linuxchix.org>
9979 <6.2.1.2.2.20050409125318.0291c6d8@pop.adsl.ya.com>
9980 In-Reply-To: <6.2.1.2.2.20050409125318.0291c6d8@pop.adsl.ya.com>
9981 X-Enigmail-Version: 0.90.1.1
9982 X-Enigmail-Supports: pgp-inline, pgp-mime
9983 Content-Type: text/plain; charset=ISO-8859-1
9984 Content-Transfer-Encoding: 7bit
9985 X-Robinquote: Holy fireworks, Batman!
9986 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
9988 X-BeenThere: courses@linuxchix.org
9989 X-Mailman-Version: 2.1.5
9991 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
9992 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9993 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
9994 List-Archive: <http://linuxchix.org/pipermail/courses>
9995 List-Post: <mailto:courses@linuxchix.org>
9996 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
9997 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
9998 <mailto:courses-request@linuxchix.org?subject=subscribe>
9999 X-List-Received-Date: Sat, 09 Apr 2005 15:00:48 -0000
10000 Content-Length: 346
10005 > I expect that later in
10006 > the course there will be a way of counting the number of entries in a
10009 That won't be a problem once we get to arrays. He haven't done them yet,
10010 but usually you would simply put all the numbers in an array, sum them up
10011 and then divide the result by the length of the array.
10016 From dan@cellectivity.com Mon Apr 11 18:22:07 2005
10017 Return-Path: <dan@cellectivity.com>
10018 X-Original-To: courses@linuxchix.org
10019 Delivered-To: courses@linuxchix.org
10020 Received: from localhost (localhost [127.0.0.1])
10021 by www.linuxchix.org (Postfix) with ESMTP id 098812730E5
10022 for <courses@linuxchix.org>; Mon, 11 Apr 2005 18:22:07 +1000 (EST)
10023 Received: from www.linuxchix.org ([127.0.0.1])
10024 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10025 id 07134-05 for <courses@linuxchix.org>;
10026 Mon, 11 Apr 2005 18:22:06 +1000 (EST)
10027 Received: from cellectivity.com (unknown [212.18.242.163])
10028 by www.linuxchix.org (Postfix) with ESMTP id E4C5727307B
10029 for <courses@linuxchix.org>; Mon, 11 Apr 2005 18:22:05 +1000 (EST)
10030 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
10031 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
10032 Mon, 11 Apr 2005 09:22:51 +0100
10033 Message-ID: <425A33A8.5000803@cellectivity.com>
10034 Date: Mon, 11 Apr 2005 09:22:00 +0100
10035 From: Dan <dan@cellectivity.com>
10036 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
10037 rv:1.7.5) Gecko/20050105 Debian/1.7.5-1
10038 X-Accept-Language: en
10040 To: courses@linuxchix.org
10041 Subject: Re: [Courses] [Perl] Part 1:
10042 References: <20050409020004.2BD5E2730E5@www.linuxchix.org>
10043 <6.2.1.2.2.20050409125318.0291c6d8@pop.adsl.ya.com>
10044 In-Reply-To: <6.2.1.2.2.20050409125318.0291c6d8@pop.adsl.ya.com>
10045 Content-Type: text/plain; charset=us-ascii; format=flowed
10046 Content-Transfer-Encoding: 7bit
10047 X-OriginalArrivalTime: 11 Apr 2005 08:22:51.0937 (UTC)
10048 FILETIME=[A751B910:01C53E6F]
10049 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10051 X-BeenThere: courses@linuxchix.org
10052 X-Mailman-Version: 2.1.5
10054 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10055 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10056 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10057 List-Archive: <http://linuxchix.org/pipermail/courses>
10058 List-Post: <mailto:courses@linuxchix.org>
10059 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10060 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10061 <mailto:courses-request@linuxchix.org?subject=subscribe>
10062 X-List-Received-Date: Mon, 11 Apr 2005 08:22:07 -0000
10063 Content-Length: 677
10066 > Just letting you know that I am here and following the course as a
10067 > newbie to programming.
10071 > Which works fine but is a bit of a cop out as I counted the number of
10072 > items summed and gave the result to the program. I expect that later in
10073 > the course there will be a way of counting the number of entries in a
10076 You may be a newby, but you certainly have the mind of a programmer!
10077 Rest assured that within two weeks we'll be able to ask the user to
10078 enter as many numbers as he likes, and then compute the average.
10081 When a program dies / what you need is a moment / of serenity.
10082 - Damian Conway, whose Perl module generates haikus
10084 From dan@cellectivity.com Sat Apr 16 00:04:12 2005
10085 Return-Path: <dan@cellectivity.com>
10086 X-Original-To: courses@linuxchix.org
10087 Delivered-To: courses@linuxchix.org
10088 Received: from localhost (localhost [127.0.0.1])
10089 by www.linuxchix.org (Postfix) with ESMTP id 22547273105
10090 for <courses@linuxchix.org>; Sat, 16 Apr 2005 00:04:12 +1000 (EST)
10091 Received: from www.linuxchix.org ([127.0.0.1])
10092 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10093 id 13613-01 for <courses@linuxchix.org>;
10094 Sat, 16 Apr 2005 00:04:11 +1000 (EST)
10095 Received: from cellectivity.com (unknown [212.18.242.163])
10096 by www.linuxchix.org (Postfix) with ESMTP id E18432730E5
10097 for <courses@linuxchix.org>; Sat, 16 Apr 2005 00:04:10 +1000 (EST)
10098 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
10099 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
10100 Fri, 15 Apr 2005 15:04:54 +0100
10101 Subject: [Courses] [Perl] Part 3: User Input and "chomp"
10102 From: Dan <dan@cellectivity.com>
10103 To: courses@linuxchix.org
10104 Content-Type: text/plain
10105 Date: Fri, 15 Apr 2005 15:04:01 +0100
10106 Message-Id: <1113573841.7918.4.camel@localhost.localdomain>
10108 X-Mailer: Evolution 2.0.4
10109 Content-Transfer-Encoding: 7bit
10110 X-OriginalArrivalTime: 15 Apr 2005 14:04:54.0890 (UTC)
10111 FILETIME=[199AD4A0:01C541C4]
10112 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10114 X-BeenThere: courses@linuxchix.org
10115 X-Mailman-Version: 2.1.5
10117 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10118 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10119 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10120 List-Archive: <http://linuxchix.org/pipermail/courses>
10121 List-Post: <mailto:courses@linuxchix.org>
10122 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10123 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10124 <mailto:courses-request@linuxchix.org?subject=subscribe>
10125 X-List-Received-Date: Fri, 15 Apr 2005 14:04:12 -0000
10126 Content-Length: 4685
10129 LinuxChix Perl Course Part 3: User Input and "chomp"
10132 2) The Line Input Operator
10133 3) The chomp Function
10135 5) Answers to Previous Exercises
10136 6) Acknowledgements
10140 ----------------------------------------
10144 Try executing the following program:
10150 my $line = <STDIN>;
10151 print "You wrote: $line";
10154 You'll notice that the program seems to "hang" rather that printing
10155 and then terminating. This means it is waiting for some user input.
10156 Type something and then press enter and see what happens.
10158 you@localhost$ ./the-program.pl
10159 Let me add some input
10160 You wrote: Let me add some input
10163 ----------------------------------------
10165 2) The Line Input Operator
10167 The line input operator <>, (also known as the diamond operator)
10168 allows your program to read data from file handles. In particular
10169 the special filehandle STDIN can be used to read input from the
10170 keyboard. For example:
10174 ----------------------------------------
10176 3) The chomp Function
10178 Let's change the introductory program a little bit:
10183 my $line = <STDIN>;
10184 print "You wrote: {$line}\n";
10186 Now when we execute it, we see this:
10188 you@localhost$ ./the-program.pl
10189 Let me add some input
10190 You wrote: {Let me add some input
10194 As you can see, the line input operator includes the newline at the
10195 end of the input. There are lots of ways to remove that newline, but
10196 the best way is to use the "chomp" function:
10201 my $line = <STDIN>;
10203 print "You wrote: {$line}\n";
10205 Note that "chomp" doesn't return the result; it changes whatever is
10206 passed in. That means we could have combined two lines in the above
10209 chomp(my $line = <STDIN>);
10213 $line = chomp($line); # No!
10215 One final note: "chomp" was introduced as a replacement for "chop",
10216 which removes the last character from a string no matter what it is.
10217 "chomp" only removes a line input separator (the newline).
10219 ----------------------------------------
10223 a) Write a currency conversion program to convert between dollars and
10226 b) To make the previous exercise more interesting, have the program
10227 ask for the exchange rate.
10229 c) Try running the following program:
10235 my $line = <STDIN>;
10238 What happens when you run it? Try entering a line without a comma
10239 followed by one with a comma. Can you explain what is happening? What
10240 effect does this have on the "chomp" function?
10242 If you need help, try reading "perldoc perlvar":
10246 and looking under "INPUT_RECORD_SEPARATOR"
10248 ----------------------------------------
10250 5) Answers to Previous Exercises
10252 a) This program computes the average of five numbers.
10257 my $sum = 23 + 28 + 31 + 17 + 1;
10258 my $average = $sum/5;
10260 print "The average is: $average\n";
10262 b) The "x" operator repeats a string.
10264 The number of repeats must always be the second parameter. As Karine
10265 put it so well, "Perl has to know which is the string and which is
10266 the "multiplicator". In that example it could guess it, but what
10267 about 4 x 3? 4 x 3 outputs '444', but 3 x 4 outputs '3333'." So
10268 operands need to be ordered.
10270 As a contrast, the Python language allows you to "multiply" strings
10271 by integers using the "*" operator. But because Python has a clear
10272 separation between text and numbers, it accepts operands in either
10275 c) The "q" operator is simply an alias for the single quote. The only
10276 advantage to using it is that it makes it easier to write strings
10277 which include single quotes, because we can choose another character
10278 to delimit the string. The "qq" operator is an alias for the double
10281 ----------------------------------------
10283 6) Acknowledgements
10285 A big thank you to Jacinta Richardson for suggestions and
10286 proofreading. More advanced Perl users might want to check out the
10287 free material from Perl Training Australia
10288 <http://www.perltraining.com.au/>, which she is a part of.
10290 Other contributors include Meryll Larkin.
10292 ----------------------------------------
10296 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
10297 Richter and Alice Wood, and is released under the same license as
10298 Perl itself (Artistic License or GPL, your choice). This is the
10299 license of choice to make it easy for other people to integrate your
10300 Perl code/documentation into their own projects. It is not generally
10301 used in projects unrelated to Perl.
10305 From kproot@nerim.net Thu Apr 21 04:28:17 2005
10306 Return-Path: <kproot@nerim.net>
10307 X-Original-To: courses@linuxchix.org
10308 Delivered-To: courses@linuxchix.org
10309 Received: from localhost (localhost [127.0.0.1])
10310 by www.linuxchix.org (Postfix) with ESMTP id C4B83273112
10311 for <courses@linuxchix.org>; Thu, 21 Apr 2005 04:28:17 +1000 (EST)
10312 Received: from www.linuxchix.org ([127.0.0.1])
10313 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10314 id 26450-10 for <courses@linuxchix.org>;
10315 Thu, 21 Apr 2005 04:28:17 +1000 (EST)
10316 Received: from mallaury.noc.nerim.net (smtp-103-wednesday.noc.nerim.net
10318 by www.linuxchix.org (Postfix) with ESMTP id 2E7C42730D3
10319 for <courses@linuxchix.org>; Thu, 21 Apr 2005 04:28:17 +1000 (EST)
10320 Received: from gelydh (jdelvare.net1.nerim.net [213.41.160.68])
10321 by mallaury.noc.nerim.net (Postfix) with ESMTP id 5F6F262D8E
10322 for <courses@linuxchix.org>; Wed, 20 Apr 2005 20:28:06 +0200 (CEST)
10323 Date: Wed, 20 Apr 2005 17:57:47 +0200
10324 From: Karine Delvare <kproot@nerim.net>
10325 To: courses@linuxchix.org
10326 Subject: Re: [Courses] [Perl] Part 3: User Input and "chomp"
10327 Message-Id: <20050420175747.3fc98472.kproot@nerim.net>
10328 In-Reply-To: <1113573841.7918.4.camel@localhost.localdomain>
10329 References: <1113573841.7918.4.camel@localhost.localdomain>
10330 X-Mailer: Sylpheed version 1.9.8 (GTK+ 2.6.4; i686-pc-linux-gnu)
10332 Content-Type: text/plain; charset=US-ASCII
10333 Content-Transfer-Encoding: 7bit
10334 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10336 X-BeenThere: courses@linuxchix.org
10337 X-Mailman-Version: 2.1.5
10339 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10340 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10341 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10342 List-Archive: <http://linuxchix.org/pipermail/courses>
10343 List-Post: <mailto:courses@linuxchix.org>
10344 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10345 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10346 <mailto:courses-request@linuxchix.org?subject=subscribe>
10347 X-List-Received-Date: Wed, 20 Apr 2005 18:28:18 -0000
10348 Content-Length: 2825
10351 Oops... saturday I told myself I should wait this time, and then I completely forgot about the course! sorry
10354 On Fri, 15 Apr 2005 15:04:01 +0100
10355 Dan <dan@cellectivity.com> wrote:
10357 > LinuxChix Perl Course Part 3: User Input and "chomp"
10361 > a) Write a currency conversion program to convert between dollars and
10367 print "Conversion from dollars to euros
10368 Please enter an amount:\n";
10369 my $dollars = <STDIN>;
10372 my $euros = $dollars * 0.76;
10373 print "$dollars dollars are $euros euros\n";
10375 > b) To make the previous exercise more interesting, have the program
10376 > ask for the exchange rate.
10378 For that exercise I tried to write the line: my $rate = chomp(<STDIN>);
10379 because it was more "natural" for me than a function (chomp) being applied to a assignment. Which is very bad as you just told chomp does not return any value. Moreover, it cannot modify <STDIN>, I got that error: Can't modify <HANDLE> in chomp. Bad, bad me :)
10384 print "Conversion from dollars to euros
10385 Please enter exchange rate:\n";
10386 chomp(my $rate = <STDIN>);
10387 print "Please enter an amount:\n";
10388 chomp(my $dollars = <STDIN>);
10390 my $euros = $dollars * $rate;
10391 print "$dollars dollars are $euros euros\n";
10393 > c) Try running the following program:
10395 > #!/usr/bin/perl -w
10399 > my $line = <STDIN>;
10402 > What happens when you run it? Try entering a line without a comma
10403 > followed by one with a comma. Can you explain what is happening?
10405 When I enter a line without a comma, a new blank line seems to be waiting for more input. I then entered a line with a comma (like "hello, somebody here?"), and I got my first line printed, and the second one printed without anything after the comma.
10408 1- there is not any loop in this perl file, so it seems that $line contains my two lines.
10409 2- the program stops asking from input as soon as it gets a coma. It does not even go any further than the coma.
10410 3- that behaviour was observed before with the newline character
10412 So it seems the comma has now replaced the newline character, but only for the "stop taking input" part (that is, I didn't see my first newline turn into a comma at the end of my first line).
10413 By "default", that strange $/ variable must equal "\n", and must be what you called : line input separator.
10415 > What effect does this have on the "chomp" function?
10417 Adding chomp to the input line, I saw I got the same result, but cut just before the comma instead of just after. So chomp behaves well and removes the line input separator, which is not the newline in that case, but the comma.
10419 > If you need help, try reading "perldoc perlvar":
10421 > $ perldoc perlvar
10423 > and looking under "INPUT_RECORD_SEPARATOR"
10425 Interesting reading :)
10429 From dan@cellectivity.com Wed Apr 20 23:17:57 2005
10430 Return-Path: <dan@cellectivity.com>
10431 X-Original-To: courses@linuxchix.org
10432 Delivered-To: courses@linuxchix.org
10433 Received: from localhost (localhost [127.0.0.1])
10434 by www.linuxchix.org (Postfix) with ESMTP id 07AFF273111
10435 for <courses@linuxchix.org>; Wed, 20 Apr 2005 23:17:56 +1000 (EST)
10436 Received: from www.linuxchix.org ([127.0.0.1])
10437 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10438 id 24807-04 for <courses@linuxchix.org>;
10439 Wed, 20 Apr 2005 23:17:56 +1000 (EST)
10440 Received: from cellectivity.com (unknown [212.18.242.163])
10441 by www.linuxchix.org (Postfix) with ESMTP id BFCF32730D7
10442 for <courses@linuxchix.org>; Wed, 20 Apr 2005 23:17:55 +1000 (EST)
10443 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
10444 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
10445 Wed, 20 Apr 2005 14:18:45 +0100
10446 Subject: [Courses] [Perl] My bad ideas
10447 From: Dan <dan@cellectivity.com>
10448 To: courses@linuxchix.org
10449 Content-Type: text/plain
10450 Date: Wed, 20 Apr 2005 14:17:49 +0100
10451 Message-Id: <1114003069.16704.53.camel@localhost.localdomain>
10453 X-Mailer: Evolution 2.0.4
10454 Content-Transfer-Encoding: 7bit
10455 X-OriginalArrivalTime: 20 Apr 2005 13:18:45.0718 (UTC)
10456 FILETIME=[7B1D7760:01C545AB]
10457 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10459 X-BeenThere: courses@linuxchix.org
10460 X-Mailman-Version: 2.1.5
10462 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10463 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10464 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10465 List-Archive: <http://linuxchix.org/pipermail/courses>
10466 List-Post: <mailto:courses@linuxchix.org>
10467 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10468 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10469 <mailto:courses-request@linuxchix.org?subject=subscribe>
10470 X-List-Received-Date: Wed, 20 Apr 2005 13:17:57 -0000
10471 Content-Length: 637
10476 Last week I asked everyone to wait about a day before sending your
10477 solutions to the list. I now realise that this was a mistake, so please
10478 disregard what I said.
10480 I wanted to make sure people didn't feel left out because they happened
10481 to be asleep when our discussions took place. However, I seem to have
10482 spoilt some of the fun and spontenaity(sp?) of the group. If you're
10483 going to program, it's got to be fun!!!
10485 So please resume sending your ideas and solutions to the list whenever
10489 Distributed computing is like driving a wagon pulled
10490 by a thousand chickens.
10495 From dan@cellectivity.com Fri Apr 22 23:41:49 2005
10496 Return-Path: <dan@cellectivity.com>
10497 X-Original-To: courses@linuxchix.org
10498 Delivered-To: courses@linuxchix.org
10499 Received: from localhost (localhost [127.0.0.1])
10500 by www.linuxchix.org (Postfix) with ESMTP id B7D64273113
10501 for <courses@linuxchix.org>; Fri, 22 Apr 2005 23:41:49 +1000 (EST)
10502 Received: from www.linuxchix.org ([127.0.0.1])
10503 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10504 id 16279-05 for <courses@linuxchix.org>;
10505 Fri, 22 Apr 2005 23:41:49 +1000 (EST)
10506 Received: from cellectivity.com (unknown [212.18.242.163])
10507 by www.linuxchix.org (Postfix) with ESMTP id 0925127310C
10508 for <courses@linuxchix.org>; Fri, 22 Apr 2005 23:41:47 +1000 (EST)
10509 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
10510 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
10511 Fri, 22 Apr 2005 14:42:38 +0100
10512 Subject: [Courses] [Perl] Part 4: Simple File Access
10513 From: Dan <dan@cellectivity.com>
10514 To: courses@linuxchix.org
10515 Content-Type: text/plain
10516 Date: Fri, 22 Apr 2005 14:41:42 +0100
10517 Message-Id: <1114177303.11292.46.camel@localhost.localdomain>
10519 X-Mailer: Evolution 2.0.4
10520 Content-Transfer-Encoding: 7bit
10521 X-OriginalArrivalTime: 22 Apr 2005 13:42:38.0781 (UTC)
10522 FILETIME=[261D06D0:01C54741]
10523 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10525 X-BeenThere: courses@linuxchix.org
10526 X-Mailman-Version: 2.1.5
10528 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10529 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10530 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10531 List-Archive: <http://linuxchix.org/pipermail/courses>
10532 List-Post: <mailto:courses@linuxchix.org>
10533 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10534 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10535 <mailto:courses-request@linuxchix.org?subject=subscribe>
10536 X-List-Received-Date: Fri, 22 Apr 2005 13:41:50 -0000
10537 Content-Length: 6466
10540 LinuxChix Perl Course Part 4: Simple File Access
10543 2) The "die" Command
10544 3) An Example Program
10546 5) Answers to Previous Exercises
10547 6) Acknowledgements
10550 ----------------------------------------
10554 Last week we saw how to read input from the keyboard. This week we
10555 are going to expand on that topic to read from and write to files.
10557 ----------------------------------------
10559 2) The "die" Command
10561 Before getting into file access, we'll explain a handy command:
10562 "die". This command does exactly what its name suggests: it kills
10563 your program with an error message. Example:
10567 Of course, you'll usually want to make the command conditional and
10568 give a helpful error message. You could use it inside an "if"
10569 statement, but we're going to use it with "or" instead:
10571 chomp($line) or die "Expected newline at end of $line";
10573 The "or" operator works similarly to "||" in shell scripts. Without
10574 going into detail, it causes the "die" command to be executed only if
10575 the "chomp" command returns zero (failure).
10577 By the way, if you want to terminate a program without an error, use
10580 ----------------------------------------
10582 3) An Example Program
10584 The following example program creates a duplicate of a file, but with
10590 # Open file for input (file name preceded by "<").
10591 open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
10593 # Open file for output (file name preceded by ">").
10594 open MY_OUTPUT, "> file2.txt" or die "Couldn't open output file:
10597 # Read from input file and write to output file.
10599 while ( defined( my $line = <MY_INPUT> ) ) {
10600 print MY_OUTPUT "$counter: $line";
10608 Four lines of this program are particularly interesting: the two
10609 "open" statements, the "while" loop, and the "print" statement. Let's
10610 look at each one in turn.
10612 The open statement takes two arguments. The first is a file handle,
10613 which by convention is written in all CAPS. Note that, also by
10614 convention, we don't put quotes around it; it's called a "bareword".
10615 STDIN is a predefined file handle, but in this case we're declaring a
10618 The second parameter to "open" is the name of the file. It should be
10619 preceded by "<" if it's we're opening it for reading and by ">" if
10620 we're opening it for writing (or ">>" if we're appending). Any spaces
10621 before or after the filename are trimmed off. If you want to open a
10622 file with strange characters like spaces, use "sysopen" instead.
10624 If the "open" statement fails, the "die" statement is executed.
10625 Notice the "$!": this special variable contains an explanation of the
10626 last error encountered.
10628 The "while" loop continues until "<MY_INPUT>" returns the undefined
10629 value, which is the signal that we've reached the end of the file.
10630 (Note: in Perl a "while" loop must be contained in braces, even if it
10631 only has one statement. The same is true with "if".)
10633 Finally, the "print" statement outputs its string to MY_OUTPUT. Note
10634 that there is no comma after MY_OUTPUT. Also note that we didn't have
10635 to add newlines because we never removed the newlines from the input.
10637 ----------------------------------------
10641 a) Write a Perl program that counts the number of lines in a file.
10642 (Just hard-code the name of the file.)
10644 b) Write a Perl program that reads a file, strips blank lines and
10645 outputs the result to another file. To test if a line is (not) blank,
10648 if ( $line ne "\n" ) {
10652 Of course, if you chomp() the newline, compare with the empty string
10655 c) If you don't specify "<" or ">" in the open statement, the file
10656 will be opened for reading. Why is it still important to include a
10659 d) Upon reaching the end of a file, the line input operator "<>"
10660 returns the undefined value, which evaluates to false. So why is it
10661 necessary to include "defined" in the while loop? Why can't we
10662 instead it write it like this:
10664 while ( my $line = <INPUT> ) # Don't do this! Use "defined"!
10666 e) Try running the following program:
10671 open MY_OUTPUT, "| grep 'foo' " or die "Couldn't run grep: $!";
10673 print MY_OUTPUT "A line with 'foo' passes.\n";
10674 print MY_OUTPUT "A line without it doesn't pass.\n";
10678 What does the vertical bar mean to the "open" statement? What happens
10679 if you put the vertical bar after the command rather than before?
10681 f) Try running the following program:
10686 open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
10689 my $content = <MY_INPUT>;
10690 print "Content is: [$content]\n";
10694 What effect does it have to set "$/" to the undefined value?
10696 ----------------------------------------
10698 5) Answers to Previous Exercises
10700 (a) and (b) The following program takes converts dollars to euros:
10705 print "How many euros in a dollar? ";
10706 my $conversion_factor = <STDIN>;
10708 print "Give me a number of dollars (no dollar sign, please). ";
10709 chomp(my $amount = <STDIN>);
10711 my $answer = $conversion_factor * $amount;
10712 print "$amount dollars equals $answer euros.\n";
10714 Notice that I chomped $amount (removed the newline) because it's
10715 output on the last line. I could have chomped $conversion_factor as
10716 well, but that wasn't necessary in this case.
10718 c) The special variable $/ controls where the <> operator breaks
10719 "lines", and what "chomp" removes. If you set it to a comma, then
10720 <STDIN> will ignore newlines and break at a comma.
10722 ----------------------------------------
10724 6) Acknowledgements
10726 A big thank you to Jacinta Richardson for suggestions and
10727 proofreading. More advanced Perl users might want to check out the
10728 free material from Perl Training Australia
10729 <http://www.perltraining.com.au/>, which she is a part of.
10731 Other contributors include Meryll Larkin.
10733 ----------------------------------------
10737 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
10738 Richter and Alice Wood, and is released under the same license as
10739 Perl itself (Artistic License or GPL, your choice). This is the
10740 license of choice to make it easy for other people to integrate your
10741 Perl code/documentation into their own projects. It is not generally
10742 used in projects unrelated to Perl.
10745 From devdas@dvb.homelinux.org Sat Apr 23 00:24:15 2005
10746 Return-Path: <devdas@dvb.homelinux.org>
10747 X-Original-To: courses@linuxchix.org
10748 Delivered-To: courses@linuxchix.org
10749 Received: from localhost (localhost [127.0.0.1])
10750 by www.linuxchix.org (Postfix) with ESMTP id 4AB35273113
10751 for <courses@linuxchix.org>; Sat, 23 Apr 2005 00:24:15 +1000 (EST)
10752 Received: from www.linuxchix.org ([127.0.0.1])
10753 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10754 id 16675-01 for <courses@linuxchix.org>;
10755 Sat, 23 Apr 2005 00:24:15 +1000 (EST)
10756 Received: from tin.nixcartel.org (ns1.nixcartel.com [66.98.212.93])
10757 by www.linuxchix.org (Postfix) with ESMTP id C45B72730D7
10758 for <courses@linuxchix.org>; Sat, 23 Apr 2005 00:24:14 +1000 (EST)
10759 Received: from dvb.homelinux.org (unknown [210.18.157.242])
10760 by tin.nixcartel.org (Postfix) with ESMTP id C1F951AC08F
10761 for <courses@linuxchix.org>; Fri, 22 Apr 2005 09:48:32 -0500 (CDT)
10762 Received: by dvb.homelinux.org (Postfix, from userid 500)
10763 id 23E00340D0; Fri, 22 Apr 2005 19:53:10 +0530 (IST)
10764 Date: Fri, 22 Apr 2005 19:53:10 +0530
10765 From: Devdas Bhagat <devdas@dvb.homelinux.org>
10766 To: courses@linuxchix.org
10767 Subject: Re: [Courses] [Perl] Part 4: Simple File Access
10768 Message-ID: <20050422195310.A3337@evita.devdas.geek>
10769 References: <1114177303.11292.46.camel@localhost.localdomain>
10771 Content-Type: text/plain; charset=us-ascii
10772 Content-Disposition: inline
10773 User-Agent: Mutt/1.2.5.1i
10774 In-Reply-To: <1114177303.11292.46.camel@localhost.localdomain>;
10775 from dan@cellectivity.com on Fri, Apr 22, 2005 at 02:41:42PM +0100
10776 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10778 X-BeenThere: courses@linuxchix.org
10779 X-Mailman-Version: 2.1.5
10781 Reply-To: Devdas Bhagat <devdas@dvb.homelinux.org>
10782 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10783 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10784 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10785 List-Archive: <http://linuxchix.org/pipermail/courses>
10786 List-Post: <mailto:courses@linuxchix.org>
10787 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10788 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10789 <mailto:courses-request@linuxchix.org?subject=subscribe>
10790 X-List-Received-Date: Fri, 22 Apr 2005 14:24:15 -0000
10791 Content-Length: 614
10794 On 22/04/05 14:41 +0100, Dan wrote:
10796 > The open statement takes two arguments. The first is a file handle,
10798 Nit: The open function takes one, two or three arguments. See perldoc -f
10799 open for the differences.
10803 > The "while" loop continues until "<MY_INPUT>" returns the undefined
10804 > value, which is the signal that we've reached the end of the file.
10805 > (Note: in Perl a "while" loop must be contained in braces, even if it
10806 > only has one statement. The same is true with "if".)
10808 Nit: while, for, foreach and if take a block as a parameter.
10809 while (EXPR) { BLOCK }
10811 {} are block delimiters.
10815 From kproot@nerim.net Sun Apr 24 21:06:54 2005
10816 Return-Path: <kproot@nerim.net>
10817 X-Original-To: courses@linuxchix.org
10818 Delivered-To: courses@linuxchix.org
10819 Received: from localhost (localhost [127.0.0.1])
10820 by www.linuxchix.org (Postfix) with ESMTP id 33C2C273114
10821 for <courses@linuxchix.org>; Sun, 24 Apr 2005 21:06:54 +1000 (EST)
10822 Received: from www.linuxchix.org ([127.0.0.1])
10823 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
10824 id 32600-09 for <courses@linuxchix.org>;
10825 Sun, 24 Apr 2005 21:06:54 +1000 (EST)
10826 Received: from kraid.nerim.net (smtp-100-sunday.nerim.net [62.4.16.100])
10827 by www.linuxchix.org (Postfix) with ESMTP id E2170273106
10828 for <courses@linuxchix.org>; Sun, 24 Apr 2005 21:06:52 +1000 (EST)
10829 Received: from gelydh (jdelvare.net1.nerim.net [213.41.160.68])
10830 by kraid.nerim.net (Postfix) with ESMTP id 99A0041C1C
10831 for <courses@linuxchix.org>; Sun, 24 Apr 2005 13:06:41 +0200 (CEST)
10832 Date: Sun, 24 Apr 2005 13:59:23 +0200
10833 From: Karine Delvare <kproot@nerim.net>
10834 To: courses@linuxchix.org
10835 Subject: Re: [Courses] [Perl] Part 4: Simple File Access
10836 Message-Id: <20050424135923.512b14e0.kproot@nerim.net>
10837 In-Reply-To: <1114177303.11292.46.camel@localhost.localdomain>
10838 References: <1114177303.11292.46.camel@localhost.localdomain>
10839 X-Mailer: Sylpheed version 1.9.8 (GTK+ 2.6.4; i686-pc-linux-gnu)
10841 Content-Type: text/plain; charset=UTF-8
10842 Content-Transfer-Encoding: quoted-printable
10843 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
10845 X-BeenThere: courses@linuxchix.org
10846 X-Mailman-Version: 2.1.5
10848 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
10849 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10850 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
10851 List-Archive: <http://linuxchix.org/pipermail/courses>
10852 List-Post: <mailto:courses@linuxchix.org>
10853 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
10854 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
10855 <mailto:courses-request@linuxchix.org?subject=subscribe>
10856 X-List-Received-Date: Sun, 24 Apr 2005 11:06:54 -0000
10857 Content-Length: 5207
10860 On Fri, 22 Apr 2005 14:41:42 +0100
10861 Dan <dan@cellectivity.com> wrote:
10863 > LinuxChix Perl Course Part 4: Simple File Access
10867 > a) Write a Perl program that counts the number of lines in a file.
10868 > (Just hard-code the name of the file.)
10870 That one was easily done by removing code from your example :
10875 # Open file for input (file name preceded by "<").
10876 open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
10878 # Read from input file and count lines.
10880 while ( defined( my $line =3D <MY_INPUT> ) ) {
10888 print "The file contains $counter lines.\n";
10890 > b) Write a Perl program that reads a file, strips blank lines and
10891 > outputs the result to another file. To test if a line is (not) blank,
10894 > if ( $line ne "\n" ) {
10898 > Of course, if you chomp() the newline, compare with the empty string
10901 I thought the second solution was nicest and I chomped the lines, but of =
10902 course I forgot to add a newline when writing to the second file... so he=
10903 re is the second version.
10908 # Open file for input (file name preceded by "<").
10909 open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
10911 # Open file for output (file name preceded by ">").
10912 open MY_OUTPUT, "> file3.txt" or die "Couldn't open output file: $!";
10914 # Read from input file and write to output file.
10915 while ( defined( my $line =3D <MY_INPUT> ) ) {
10918 print MY_OUTPUT "$line\n";
10926 > c) If you don't specify "<" or ">" in the open statement, the file
10927 > will be opened for reading. Why is it still important to include a
10930 I'd say, for clarity ?
10932 Ok, I tried to go a bit further and called a file "<". But, I don't know =
10933 how to escape it correctly in the perl script and I couldn't go much furt=
10936 > d) Upon reaching the end of a file, the line input operator "<>"
10937 > returns the undefined value, which evaluates to false. So why is it
10938 > necessary to include "defined" in the while loop? Why can't we
10939 > instead it write it like this:
10941 > while ( my $line =3D <INPUT> ) # Don't do this! Use "defined"!
10943 Looks like the good ol' error everyone makes in every language. On the la=
10944 st line, <INPUT> will return undefined, so my $line =3D <INPUT> will succ=
10945 esfully assign the undefined value to $line, and the code in the while lo=
10946 op will be executed one more time, using an undefined value.
10948 > e) Try running the following program:
10950 > #!/usr/bin/perl -w
10953 > open MY_OUTPUT, "| grep 'foo' " or die "Couldn't run grep: $!";
10955 > print MY_OUTPUT "A line with 'foo' passes.\n";
10956 > print MY_OUTPUT "A line without it doesn't pass.\n";
10960 > What does the vertical bar mean to the "open" statement? What=20
10961 > happens if you put the vertical bar after the command rather than=20
10964 I had to run that one to guess what it was doing, and I'm still not sure =
10965 I fully understand it - I'm not a shell genius. Happily, I use "|grep 'fo=
10966 o' " enough for my own use to know that it will only output the lines tha=
10967 t contain 'foo' from what the command before the vertical bar (the pipe i=
10968 f I'm not mistaking) should output.
10969 But here, I have difficulties to find what is the command before.
10971 Running the script, I see that only the first line, with 'foo' in it, is =
10972 printed, which is not so surprising, but I'm still very confused at the w=
10975 So, MY_OUTPUT is a file handle, but not a file on disk (as I don't see an=
10976 y filename). And, the second argument to open, which should be the name o=
10977 f a file, preceded by "<" or ">" or ">>". That's where I'm lost.
10979 I "intuitively" understand that the two lines written to MY_OUTPUT are im=
10980 mediately processed with "|grep 'foo' ", but I wouldn't be able to explai=
10983 Reading perldoc -f open doesn't help either as it also states that the se=
10984 cond argument gives the filename.
10986 Hmmm... readin further in perldoc, I get :
10987 If the filename begins with =E2=80=99=E2=8E=AA=E2=80=99, t=
10988 he filename is interpreted as
10989 a command to which output is to be piped, and if the filen=
10991 ends with a =E2=80=99=E2=8E=AA=E2=80=99, the filename is i=
10992 nterpreted as a command which
10993 pipes output to us.
10995 So there it is. I wonder if lack of shell knowledge was the problem, but =
10996 that one was really not easy.
10998 Even though the second question seems to be answered too, I get errors wh=
10999 en I try to put the pipe at the end :
11000 Filehandle MY_OUTPUT opened only for input
11002 > f) Try running the following program:
11004 > #!/usr/bin/perl -w
11007 > open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
11009 > local $/ =3D undef;
11010 > my $content =3D <MY_INPUT>;
11011 > print "Content is: [$content]\n";
11015 > What effect does it have to set "$/" to the undefined value?
11017 I did not see any effet, the script outputs my file as is... I was expect=
11018 ing getting the file with no newlines.
11022 From fazia@faziarizvi.net Tue Apr 26 02:06:24 2005
11023 Return-Path: <fazia@faziarizvi.net>
11024 X-Original-To: courses@linuxchix.org
11025 Delivered-To: courses@linuxchix.org
11026 Received: from localhost (localhost [127.0.0.1])
11027 by www.linuxchix.org (Postfix) with ESMTP id E40EF273117
11028 for <courses@linuxchix.org>; Tue, 26 Apr 2005 02:06:23 +1000 (EST)
11029 Received: from www.linuxchix.org ([127.0.0.1])
11030 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11031 id 10179-01 for <courses@linuxchix.org>;
11032 Tue, 26 Apr 2005 02:06:23 +1000 (EST)
11033 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
11034 by www.linuxchix.org (Postfix) with ESMTP id 6962F273113
11035 for <courses@linuxchix.org>; Tue, 26 Apr 2005 02:06:23 +1000 (EST)
11036 Received: from tauri.jsnider.net ([66.194.162.10])
11037 by tauri.jsnider.net with esmtp (Exim 4.44) id 1DQ66F-0007dh-OL
11038 for courses@linuxchix.org; Mon, 25 Apr 2005 11:06:19 -0500
11039 Date: Mon, 25 Apr 2005 11:06:19 -0500 (CDT)
11040 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
11041 X-X-Sender: fazia@tauri.jsnider.net
11042 To: courses@linuxchix.org
11043 Subject: Re: [Courses] [Perl] Part 4: Simple File Access
11044 In-Reply-To: <20050424135923.512b14e0.kproot@nerim.net>
11045 Message-ID: <Pine.LNX.4.60.0504251105180.29119@tauri.jsnider.net>
11046 References: <1114177303.11292.46.camel@localhost.localdomain>
11047 <20050424135923.512b14e0.kproot@nerim.net>
11049 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
11050 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11052 X-BeenThere: courses@linuxchix.org
11053 X-Mailman-Version: 2.1.5
11055 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11056 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11057 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11058 List-Archive: <http://linuxchix.org/pipermail/courses>
11059 List-Post: <mailto:courses@linuxchix.org>
11060 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11061 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11062 <mailto:courses-request@linuxchix.org?subject=subscribe>
11063 X-List-Received-Date: Mon, 25 Apr 2005 16:06:24 -0000
11064 Content-Length: 110
11068 Hrm. I didn't get the original lesson/exercise email on this part. Could
11069 someone forward that to me?
11074 From dan@cellectivity.com Tue Apr 26 02:18:58 2005
11075 Return-Path: <dan@cellectivity.com>
11076 X-Original-To: courses@linuxchix.org
11077 Delivered-To: courses@linuxchix.org
11078 Received: from localhost (localhost [127.0.0.1])
11079 by www.linuxchix.org (Postfix) with ESMTP id A375B273117
11080 for <courses@linuxchix.org>; Tue, 26 Apr 2005 02:18:58 +1000 (EST)
11081 Received: from www.linuxchix.org ([127.0.0.1])
11082 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11083 id 10244-04 for <courses@linuxchix.org>;
11084 Tue, 26 Apr 2005 02:18:58 +1000 (EST)
11085 Received: from cellectivity.com (unknown [212.18.242.163])
11086 by www.linuxchix.org (Postfix) with ESMTP id 86D99273113
11087 for <courses@linuxchix.org>; Tue, 26 Apr 2005 02:18:57 +1000 (EST)
11088 Received: from [192.168.100.247] ([192.168.100.247] RDNS failed) by
11089 cellectivity.com with Microsoft SMTPSVC(6.0.3790.211);
11090 Mon, 25 Apr 2005 17:19:44 +0100
11091 Subject: Re: [Courses] [Perl] Part 4: Simple File Access
11092 From: Dan <dan@cellectivity.com>
11093 To: courses@linuxchix.org
11094 In-Reply-To: <Pine.LNX.4.60.0504251105180.29119@tauri.jsnider.net>
11095 References: <1114177303.11292.46.camel@localhost.localdomain>
11096 <20050424135923.512b14e0.kproot@nerim.net>
11097 <Pine.LNX.4.60.0504251105180.29119@tauri.jsnider.net>
11098 Content-Type: text/plain
11099 Date: Mon, 25 Apr 2005 17:18:43 +0100
11100 Message-Id: <1114445923.18091.104.camel@localhost.localdomain>
11102 X-Mailer: Evolution 2.0.4
11103 Content-Transfer-Encoding: 7bit
11104 X-OriginalArrivalTime: 25 Apr 2005 16:19:44.0421 (UTC)
11105 FILETIME=[9778CD50:01C549B2]
11106 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11108 X-BeenThere: courses@linuxchix.org
11109 X-Mailman-Version: 2.1.5
11111 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11112 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11113 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11114 List-Archive: <http://linuxchix.org/pipermail/courses>
11115 List-Post: <mailto:courses@linuxchix.org>
11116 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11117 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11118 <mailto:courses-request@linuxchix.org?subject=subscribe>
11119 X-List-Received-Date: Mon, 25 Apr 2005 16:18:58 -0000
11120 Content-Length: 490
11123 > Hrm. I didn't get the original lesson/exercise email on this part. Could
11124 > someone forward that to me?
11126 All messages to this list are stored in the LinuxChix archives. The one
11128 http://mailman.linuxchix.org/pipermail/courses/2005-April/001892.html
11131 [Mozilla] Firefox is better than Explorer by leaps and bounds. I
11132 don't miss Explorer one iota. Give Firefox a day's worth of Web
11133 surfing, and you won't either.
11134 - Forbes (short link: http://tinyurl.com/56j8m )
11138 From fazia@faziarizvi.net Tue Apr 26 04:12:44 2005
11139 Return-Path: <fazia@faziarizvi.net>
11140 X-Original-To: courses@linuxchix.org
11141 Delivered-To: courses@linuxchix.org
11142 Received: from localhost (localhost [127.0.0.1])
11143 by www.linuxchix.org (Postfix) with ESMTP id 3D77E273117
11144 for <courses@linuxchix.org>; Tue, 26 Apr 2005 04:12:44 +1000 (EST)
11145 Received: from www.linuxchix.org ([127.0.0.1])
11146 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11147 id 10661-08 for <courses@linuxchix.org>;
11148 Tue, 26 Apr 2005 04:12:44 +1000 (EST)
11149 Received: from tauri.jsnider.net (tauri.jsnider.net [66.194.162.10])
11150 by www.linuxchix.org (Postfix) with ESMTP id C0E08273112
11151 for <courses@linuxchix.org>; Tue, 26 Apr 2005 04:12:43 +1000 (EST)
11152 Received: from tauri.jsnider.net ([66.194.162.10])
11153 by tauri.jsnider.net with esmtp (Exim 4.44)
11154 id 1DQ84T-0007q8-FY; Mon, 25 Apr 2005 13:12:37 -0500
11155 Date: Mon, 25 Apr 2005 13:12:37 -0500 (CDT)
11156 From: Fazia Begum Rizvi <fazia@faziarizvi.net>
11157 X-X-Sender: fazia@tauri.jsnider.net
11158 To: Dan <dan@cellectivity.com>
11159 Subject: Re: [Courses] [Perl] Part 4: Simple File Access
11160 In-Reply-To: <1114445923.18091.104.camel@localhost.localdomain>
11161 Message-ID: <Pine.LNX.4.60.0504251312210.30135@tauri.jsnider.net>
11162 References: <1114177303.11292.46.camel@localhost.localdomain>
11163 <20050424135923.512b14e0.kproot@nerim.net>
11164 <Pine.LNX.4.60.0504251105180.29119@tauri.jsnider.net>
11165 <1114445923.18091.104.camel@localhost.localdomain>
11167 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
11168 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11170 Cc: courses@linuxchix.org
11171 X-BeenThere: courses@linuxchix.org
11172 X-Mailman-Version: 2.1.5
11174 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11175 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11176 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11177 List-Archive: <http://linuxchix.org/pipermail/courses>
11178 List-Post: <mailto:courses@linuxchix.org>
11179 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11180 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11181 <mailto:courses-request@linuxchix.org?subject=subscribe>
11182 X-List-Received-Date: Mon, 25 Apr 2005 18:12:44 -0000
11183 Content-Length: 402
11187 On Mon, 25 Apr 2005, Dan wrote:
11189 >> Hrm. I didn't get the original lesson/exercise email on this part. Could
11190 >> someone forward that to me?
11192 > All messages to this list are stored in the LinuxChix archives. The one
11194 > http://mailman.linuxchix.org/pipermail/courses/2005-April/001892.html
11202 Bits and Bytes: http://uweb.txstate.edu/~fr06/weblog/
11204 From dan@cellectivity.com Sat Apr 30 00:19:15 2005
11205 Return-Path: <dan@cellectivity.com>
11206 X-Original-To: courses@linuxchix.org
11207 Delivered-To: courses@linuxchix.org
11208 Received: from localhost (localhost [127.0.0.1])
11209 by www.linuxchix.org (Postfix) with ESMTP id 781D9273123
11210 for <courses@linuxchix.org>; Sat, 30 Apr 2005 00:19:15 +1000 (EST)
11211 Received: from www.linuxchix.org ([127.0.0.1])
11212 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11213 id 10929-09 for <courses@linuxchix.org>;
11214 Sat, 30 Apr 2005 00:19:15 +1000 (EST)
11215 Received: from cellectivity.com (unknown [212.18.242.163])
11216 by www.linuxchix.org (Postfix) with ESMTP id 9EA2327311E
11217 for <courses@linuxchix.org>; Sat, 30 Apr 2005 00:19:13 +1000 (EST)
11218 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
11219 with Microsoft SMTPSVC(6.0.3790.211);
11220 Fri, 29 Apr 2005 15:20:04 +0100
11221 Subject: [Courses] [Perl] Part 5: The m// Operator
11222 From: Dan <dan@cellectivity.com>
11223 To: courses@linuxchix.org
11224 Content-Type: text/plain
11225 Date: Fri, 29 Apr 2005 15:19:05 +0100
11226 Message-Id: <1114784345.11915.26.camel@localhost.localdomain>
11228 X-Mailer: Evolution 2.0.4
11229 Content-Transfer-Encoding: 7bit
11230 X-OriginalArrivalTime: 29 Apr 2005 14:20:04.0828 (UTC)
11231 FILETIME=[89C0D9C0:01C54CC6]
11232 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11234 X-BeenThere: courses@linuxchix.org
11235 X-Mailman-Version: 2.1.5
11237 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11238 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11239 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11240 List-Archive: <http://linuxchix.org/pipermail/courses>
11241 List-Post: <mailto:courses@linuxchix.org>
11242 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11243 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11244 <mailto:courses-request@linuxchix.org?subject=subscribe>
11245 X-List-Received-Date: Fri, 29 Apr 2005 14:19:15 -0000
11246 Content-Length: 7018
11249 LinuxChix Perl Course Part 5: The m// Operator
11252 2) An Example Program
11253 3) Perl Regular Expressions
11254 4) The Many Forms of m//
11256 6) Answers to Previous Exercises
11257 7) Acknowledgements
11261 ----------------------------------------
11265 Now we're getting into the truly interesting parts of Perl. In my
11266 opinion the m// operator is one of the two most fun things about the
11267 language. (The other is the s/// operator, which we'll get to in two
11268 weeks.) These two operators alone make the language worth learning.
11269 If you remember nothing else, remember these two operators.
11271 ----------------------------------------
11273 2) An Example Program
11275 Try executing the following program, then typing a few lines of
11276 input. (Hint: try entering a line containing "foo".)
11281 while ( defined( my $line = <STDIN> ) ) {
11282 if ( $line =~ m/foo/ ) {
11283 print "You entered a line containing 'foo'.\n";
11286 print "You entered a line that doesn't contain 'foo'.\n";
11290 Notice that the line with m// is does not use a single or double
11291 equals sign, but rather an operator that we haven't seen before: an
11292 equals sign followed by a tilde.
11294 This example doesn't do justice to the power of m//, because m//
11295 actually does regular expression matching. So let's look at Perl
11296 regular expressions in a little more detail.
11298 ----------------------------------------
11300 3) Perl Regular Expressions
11302 I'm going to assume that you have some familiarity with regular
11303 expressions. If you don't, feel free to ask the list.
11305 Perl adds additional wildcards and capabilities to the regular
11306 expression syntax provided by some other tools. This makes Perl
11307 regular expressions somewhat proprietary, though people like them so
11308 much that they've been built into several other languages (such as
11309 PHP) as extensions. But remember that what works in Perl isn't
11310 necessarily going to work in grep or some other tool.
11312 In addition to the standard wildcards ("." for any character, etc.),
11315 \S anything but white space
11317 \W non-word character
11318 \d digit: equivalent to [0-9]
11321 Perl also supports all the repetition flags you've probably seen in
11326 {n} exactly "n" instances
11327 {m,n} between "m" and "n" instances
11329 Here are the characters that have special meaning in Perl regular
11330 expressions (I hope I got them all):
11331 . * + ? | ^ $ @ ( ) [ ] { } \
11332 and the delimiter (usually slash)
11333 You escape a character (make it lose its special meaning) by
11334 preceding it with a backslash.
11336 Here are some quick examples of regular expressions in Perl:
11338 if ( $x =~ m/[a-z]{4}/ ) { ... } # 4-letter word (lowercase)
11339 if ( $x =~ m/[A-Za-z]{4}/ ) { ... } # 4-letter word (any case)
11340 if ( $x =~ m/\@/ ) { ... } # e-mail address (see below)
11342 That last example reminds us of an important point: if part of the
11343 string matches, the whole string matches. If the regular expression
11344 must match the entire string, use ^ and $, like this:
11346 if ( $x =~ m/abc/ ) { ... } # matches "abc" and "xabcx"
11347 if ( $x =~ m/^abc$/ ) { ... } # matches only "abc"
11349 ----------------------------------------
11351 4) The Many Forms of m//
11353 In our examples we have been using a slash as a delimiter, but the
11354 delimiter can be almost any punctuation. In other words, all of the
11355 following are equivalent (they match one or more capital letters):
11357 if ( $x =~ m/[A-Z]+/ ) { ... }
11358 if ( $x =~ m:[A-Z]+: ) { ... }
11359 if ( $x =~ m![A-Z]+! ) { ... }
11360 if ( $x =~ m#[A-Z]+# ) { ... }
11361 if ( $x =~ m<[A-Z]+> ) { ... }
11363 This avoids the need to escape slashes. However, if you do use a
11364 slash as the delimiter, you can leave out the "m":
11366 if ( $x =~ /[A-Z]+/ ) { ... }
11368 ----------------------------------------
11372 a) Write a program that reads /etc/passwd and outputs the line
11373 corresponding to your account.
11375 b) Write a program that reads a C++ file and outputs all the #include
11376 statements. Assume that no include statement spans multiple lines,
11377 but consider that it might be indented.
11379 ----------------------------------------
11381 6) Answers to Previous Exercises
11383 a) The following program counts the number of lines in a file.
11388 open MY_INPUT, "< file1.txt" or die "Couldn't open input file: $!";
11391 while ( defined( <MY_INPUT> ) ) {
11397 print "The file has $count lines.\n";
11399 b) The following program copies only non-blank lines from one file to
11405 open MY_INPUT, "< file1.txt" or die "Couldn't open input file:
11407 open MY_OUTPUT, "> file2.txt" or die "Couldn't open output file:
11410 while ( defined( my $line = <MY_INPUT> ) ) {
11411 if ( $line ne "\n" ) {
11412 print MY_OUTPUT $line;
11419 c) If you don't specify "<" or ">" in the open statement, the file
11420 will be opened for reading. But you should get in the habit of
11421 specifying a "<" anyway, because you'll often be reading a file that
11422 the user passed on the command line. You don't want to erase a
11423 valuable file just because the user accidentally (or maliciously)
11424 preceded the filename with a ">".
11426 d) The reason we must use "defined" to determine if we have reached
11427 the end of the file is that some strings evaluate numerically to
11428 zero, which evaluates to false. For example, if the very last line of
11429 a file is the digit zero without a newline, we might skip over it if
11430 we don't test it using "defined". That's the kind of bug that will
11431 take you forever to find!
11433 e) A vertical bar causes "open" to execute a program instead of
11434 opening a file. If the vertical bar comes before the program name,
11435 you can use "print" to write to the program's standard input. If the
11436 vertical bar comes after the program name, you can use the <>
11437 operator to read the program's output.
11439 f) If you set $/ to undef, <> will continue reading until it reaches
11440 the end of the file (which doesn't make sense with STDIN, unless you
11441 pipe the input from a file). This is called "slurp mode".
11443 ----------------------------------------
11445 7) Acknowledgements
11447 A big thank you to Jacinta Richardson for suggestions and
11448 proofreading. More advanced Perl users might want to check out the
11449 free material from Perl Training Australia
11450 <http://www.perltraining.com.au/>, which she is a part of.
11452 Other contributors include Meryll Larkin.
11454 ----------------------------------------
11458 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
11459 Richter and Alice Wood, and is released under the same license as
11460 Perl itself (Artistic License or GPL, your choice). This is the
11461 license of choice to make it easy for other people to integrate your
11462 Perl code/documentation into their own projects. It is not generally
11463 used in projects unrelated to Perl.
11467 From kproot@nerim.net Mon May 2 02:52:30 2005
11468 Return-Path: <kproot@nerim.net>
11469 X-Original-To: courses@linuxchix.org
11470 Delivered-To: courses@linuxchix.org
11471 Received: from localhost (localhost [127.0.0.1])
11472 by www.linuxchix.org (Postfix) with ESMTP id BC4C327312A
11473 for <courses@linuxchix.org>; Mon, 2 May 2005 02:52:30 +1000 (EST)
11474 Received: from www.linuxchix.org ([127.0.0.1])
11475 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11476 id 30371-06 for <courses@linuxchix.org>;
11477 Mon, 2 May 2005 02:52:30 +1000 (EST)
11478 Received: from mallaury.noc.nerim.net (smtp-100-sunday.noc.nerim.net
11480 by www.linuxchix.org (Postfix) with ESMTP id 2C2292730F6
11481 for <courses@linuxchix.org>; Mon, 2 May 2005 02:52:30 +1000 (EST)
11482 Received: from gelydh (jdelvare.net1.nerim.net [213.41.160.68])
11483 by mallaury.noc.nerim.net (Postfix) with ESMTP id 3456A62D0C
11484 for <courses@linuxchix.org>; Sun, 1 May 2005 18:52:08 +0200 (CEST)
11485 Date: Sun, 1 May 2005 18:52:10 +0200
11486 From: Karine Delvare <kproot@nerim.net>
11487 To: courses@linuxchix.org
11488 Subject: Re: [Courses] [Perl] Part 5: The m// Operator
11489 Message-Id: <20050501185210.70a17857.kproot@nerim.net>
11490 In-Reply-To: <1114784345.11915.26.camel@localhost.localdomain>
11491 References: <1114784345.11915.26.camel@localhost.localdomain>
11492 X-Mailer: Sylpheed version 1.9.9 (GTK+ 2.6.4; i686-pc-linux-gnu)
11494 Content-Type: text/plain; charset=US-ASCII
11495 Content-Transfer-Encoding: 7bit
11496 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11498 X-BeenThere: courses@linuxchix.org
11499 X-Mailman-Version: 2.1.5
11501 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11502 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11503 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11504 List-Archive: <http://linuxchix.org/pipermail/courses>
11505 List-Post: <mailto:courses@linuxchix.org>
11506 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11507 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11508 <mailto:courses-request@linuxchix.org?subject=subscribe>
11509 X-List-Received-Date: Sun, 01 May 2005 16:52:31 -0000
11510 Content-Length: 1364
11513 On Fri, 29 Apr 2005 15:19:05 +0100
11514 Dan <dan@cellectivity.com> wrote:
11516 > LinuxChix Perl Course Part 5: The m// Operator
11520 > a) Write a program that reads /etc/passwd and outputs the line
11521 > corresponding to your account.
11526 open MY_INPUT, "< /etc/passwd" or die "Couldn't open input file: $!";
11528 while (defined(my $line = <MY_INPUT>))
11530 if ($line =~ /^edhel:/)
11538 I wonder if there is a way to know when your regular expression is "enough". I started to search for /edhel/, then thought that would match zedhel or edhel2 if such users existed, so I tried /^edhel:/, but then I thought, what if it catches more than I want (like, if I had not thought about zedhel2). Do I have to be careful enough, or are there tips / techniques to get as accurate as possible with regexps ?
11540 > b) Write a program that reads a C++ file and outputs all the #include
11541 > statements. Assume that no include statement spans multiple lines,
11542 > but consider that it might be indented.
11547 print "Please enter the path to a C++ file:\n";
11548 my $file = <STDIN>;
11550 open MY_INPUT, "< $file" or die "Couldn't open input file: $!";
11552 while (defined(my $line = <MY_INPUT>))
11554 if ($line =~ /\s*#include /)
11564 http://edhel.nerim.net/
11566 From dan@cellectivity.com Sat May 7 01:11:11 2005
11567 Return-Path: <dan@cellectivity.com>
11568 X-Original-To: courses@linuxchix.org
11569 Delivered-To: courses@linuxchix.org
11570 Received: from localhost (localhost [127.0.0.1])
11571 by www.linuxchix.org (Postfix) with ESMTP id CA19D273136
11572 for <courses@linuxchix.org>; Sat, 7 May 2005 01:11:11 +1000 (EST)
11573 Received: from www.linuxchix.org ([127.0.0.1])
11574 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11575 id 06912-06 for <courses@linuxchix.org>;
11576 Sat, 7 May 2005 01:11:11 +1000 (EST)
11577 Received: from cellectivity.com (unknown [212.18.242.163])
11578 by www.linuxchix.org (Postfix) with ESMTP id 11471273116
11579 for <courses@linuxchix.org>; Sat, 7 May 2005 01:11:09 +1000 (EST)
11580 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
11581 with Microsoft SMTPSVC(6.0.3790.211); Fri, 6 May 2005 16:12:02 +0100
11582 Subject: [Courses] [Perl] Part 6: Data Extraction with m//
11583 From: Dan <dan@cellectivity.com>
11584 To: courses@linuxchix.org
11585 Content-Type: text/plain
11586 Date: Fri, 06 May 2005 16:11:00 +0100
11587 Message-Id: <1115392260.10937.17.camel@localhost.localdomain>
11589 X-Mailer: Evolution 2.0.4
11590 Content-Transfer-Encoding: 7bit
11591 X-OriginalArrivalTime: 06 May 2005 15:12:02.0453 (UTC)
11592 FILETIME=[F4E4EC50:01C5524D]
11593 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11595 X-BeenThere: courses@linuxchix.org
11596 X-Mailman-Version: 2.1.5
11598 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11599 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11600 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11601 List-Archive: <http://linuxchix.org/pipermail/courses>
11602 List-Post: <mailto:courses@linuxchix.org>
11603 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11604 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11605 <mailto:courses-request@linuxchix.org?subject=subscribe>
11606 X-List-Received-Date: Fri, 06 May 2005 15:11:12 -0000
11607 Content-Length: 4269
11610 LinuxChix Perl Course Part 6: Data Extraction with m//
11613 2) Match Extraction
11616 5) Answers to Previous Exercises
11617 6) Acknowledgements
11621 ----------------------------------------
11625 The m// operator is actually much more powerful than we saw in the
11626 last lesson. In this lesson we're going to see how to use it to
11627 extract information.
11629 ----------------------------------------
11631 2) Match Extraction
11633 The m// operator allows you to extract text from a string. Simply
11634 enclose the relevant part of the pattern in parentheses. The content
11635 of the first set of parentheses will be stored in the variable $1,
11636 the second in $2, etc.
11639 # Match digits-colon-digits-colon-digits
11640 if ( $time =~ /(\d+):(\d+):(\d+)/ ) {
11641 $hour = $1; # First set of parentheses
11642 $minute = $2; # Second set of parentheses
11643 $second = $3; # Third set of parentheses
11646 print "Time wasn't valid.\n";
11649 Alternatively, m// returns a list of the matches, so we can compress
11650 the above example into one line:
11652 if ( ($hour, $minute, $second) = ( $time =~ /(\d+):(\d+):(\d+)/ ) )
11654 When extracting text, we don't technically need to put the m// in an
11655 "if" statement, but it's highly recommended. You should always take
11656 into account the possibility that the pattern doesn't match.
11658 ----------------------------------------
11662 Examine the following program. What will it output?
11667 my $html = '<p>foo</p> <p>bar</p> <p>baz</p>';
11669 if ( $html =~ m:<p>(.*)</p>: ) {
11673 It doesn't output "foo", "bar" or "baz". Instead, it outputs "foo</p>
11674 <p>bar</p> <p>baz" This is because the asterisk wildcard is "greedy":
11675 it always includes the maximum amount of text possible.
11677 If you don't want a greedy match, add a question mark after the
11680 if ( $html =~ m:<p>(.*?)</p>: ) # Output is "foo".
11682 Ditto for the plus sign:
11684 if ( $html =~ m:<p>(.+?)</p>: )
11686 Greediness never affects WHETHER there is a match; it only affects
11687 exactly what text is matched.
11689 ----------------------------------------
11693 Modify your program from last week's exercise, the one that reads
11694 /etc/passwd, so that it outputs only your home directory and your
11695 shell (not the rest of the line).
11697 ----------------------------------------
11699 5) Answers to Previous Exercises
11701 a) The following program reads /etc/passwd and outputs the line
11702 corresponding to my account name:
11707 my $username = 'dan';
11709 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
11711 while ( defined( my $line = <PASSWD> ) ) {
11712 if ( $line =~ m/^$username:/ ) {
11719 To avoid hard-coding the account name, we could use the UID instead:
11724 my $uid = $<; # $< is system account number (UID).
11726 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
11728 while ( defined( my $line = <PASSWD> ) ) {
11729 if ( $line =~ m/^[^:]+:[^:]*:$uid:/ ) {
11736 b) This program reads a C++ file and outputs all #include statements:
11741 open IN, "< foo.c" or die "Couldn't open file.";
11743 while ( defined( my $line = <IN> ) ) {
11744 if ( $line =~ m/^\s*#include/ ) {
11751 ----------------------------------------
11753 6) Acknowledgements
11755 A big thank you to Jacinta Richardson for suggestions and
11756 proofreading. More advanced Perl users might want to check out the
11757 free material from Perl Training Australia
11758 <http://www.perltraining.com.au/>, which she is a part of.
11760 Other contributors include Meryll Larkin.
11762 ----------------------------------------
11766 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
11767 Richter and Alice Wood, and is released under the same license as
11768 Perl itself (Artistic License or GPL, your choice). This is the
11769 license of choice to make it easy for other people to integrate your
11770 Perl code/documentation into their own projects. It is not generally
11771 used in projects unrelated to Perl.
11774 From kproot@nerim.net Mon May 9 00:45:00 2005
11775 Return-Path: <kproot@nerim.net>
11776 X-Original-To: courses@linuxchix.org
11777 Delivered-To: courses@linuxchix.org
11778 Received: from localhost (localhost [127.0.0.1])
11779 by www.linuxchix.org (Postfix) with ESMTP id 85B2327313A
11780 for <courses@linuxchix.org>; Mon, 9 May 2005 00:45:00 +1000 (EST)
11781 Received: from www.linuxchix.org ([127.0.0.1])
11782 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11783 id 22947-06 for <courses@linuxchix.org>;
11784 Mon, 9 May 2005 00:45:00 +1000 (EST)
11785 Received: from kraid.nerim.net (smtp-100-sunday.nerim.net [62.4.16.100])
11786 by www.linuxchix.org (Postfix) with ESMTP id DAA8F273138
11787 for <courses@linuxchix.org>; Mon, 9 May 2005 00:44:59 +1000 (EST)
11788 Received: from gelydh (jdelvare.net1.nerim.net [213.41.160.68])
11789 by kraid.nerim.net (Postfix) with ESMTP id 7D5F440E22
11790 for <courses@linuxchix.org>; Sun, 8 May 2005 16:44:51 +0200 (CEST)
11791 Date: Sun, 8 May 2005 16:44:54 +0200
11792 From: Karine Delvare <kproot@nerim.net>
11793 To: courses@linuxchix.org
11794 Subject: Re: [Courses] [Perl] Part 6: Data Extraction with m//
11795 Message-Id: <20050508164454.7ed27587.kproot@nerim.net>
11796 In-Reply-To: <1115392260.10937.17.camel@localhost.localdomain>
11797 References: <1115392260.10937.17.camel@localhost.localdomain>
11798 X-Mailer: Sylpheed version 1.9.9 (GTK+ 2.6.4; i686-pc-linux-gnu)
11800 Content-Type: text/plain; charset=US-ASCII
11801 Content-Transfer-Encoding: 7bit
11802 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11804 X-BeenThere: courses@linuxchix.org
11805 X-Mailman-Version: 2.1.5
11807 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11808 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11809 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11810 List-Archive: <http://linuxchix.org/pipermail/courses>
11811 List-Post: <mailto:courses@linuxchix.org>
11812 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11813 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11814 <mailto:courses-request@linuxchix.org?subject=subscribe>
11815 X-List-Received-Date: Sun, 08 May 2005 14:45:00 -0000
11816 Content-Length: 1018
11819 On Fri, 06 May 2005 16:11:00 +0100
11820 Dan <dan@cellectivity.com> wrote:
11822 > LinuxChix Perl Course Part 6: Data Extraction with m//
11826 > Modify your program from last week's exercise, the one that reads
11827 > /etc/passwd, so that it outputs only your home directory and your
11828 > shell (not the rest of the line).
11833 open MY_INPUT, "< /etc/passwd" or die "Couldn't open input file: $!";
11835 print "Please enter a username:\n";
11836 chomp(my $user = <STDIN>);
11842 while (defined(my $line = <MY_INPUT>))
11844 if (($home, $shell) = ($line =~ /^$user:.*:(.*):(.*)$/))
11846 print "Home is: $home\nShell is: $shell\n";
11853 print "This user does not exist.\n";
11860 Provided I'm sure shell is the last info and home the one before that, I decided to use a greedy match for all the info between the user and the home directory.
11861 Does greedy / non-greedy affect performances?
11865 http://edhel.nerim.net/
11867 From dan@cellectivity.com Sat May 7 01:48:21 2005
11868 Return-Path: <dan@cellectivity.com>
11869 X-Original-To: courses@linuxchix.org
11870 Delivered-To: courses@linuxchix.org
11871 Received: from localhost (localhost [127.0.0.1])
11872 by www.linuxchix.org (Postfix) with ESMTP id 7E000273138
11873 for <courses@linuxchix.org>; Sat, 7 May 2005 01:48:21 +1000 (EST)
11874 Received: from www.linuxchix.org ([127.0.0.1])
11875 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11876 id 07203-05 for <courses@linuxchix.org>;
11877 Sat, 7 May 2005 01:48:21 +1000 (EST)
11878 Received: from cellectivity.com (unknown [212.18.242.163])
11879 by www.linuxchix.org (Postfix) with ESMTP id C3091273136
11880 for <courses@linuxchix.org>; Sat, 7 May 2005 01:48:19 +1000 (EST)
11881 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
11882 with Microsoft SMTPSVC(6.0.3790.211); Fri, 6 May 2005 16:49:14 +0100
11883 Subject: [Courses] [Perl] Re: exercises
11884 From: Dan <dan@cellectivity.com>
11885 To: Karine Delvare <kproot@nerim.net>
11886 Content-Type: text/plain
11887 Date: Fri, 06 May 2005 16:48:12 +0100
11888 Message-Id: <1115394492.10937.27.camel@localhost.localdomain>
11890 X-Mailer: Evolution 2.0.4
11891 Content-Transfer-Encoding: 7bit
11892 X-OriginalArrivalTime: 06 May 2005 15:49:14.0406 (UTC)
11893 FILETIME=[273DEC60:01C55253]
11894 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11896 Cc: courses@linuxchix.org
11897 X-BeenThere: courses@linuxchix.org
11898 X-Mailman-Version: 2.1.5
11900 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11901 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11902 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11903 List-Archive: <http://linuxchix.org/pipermail/courses>
11904 List-Post: <mailto:courses@linuxchix.org>
11905 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11906 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11907 <mailto:courses-request@linuxchix.org?subject=subscribe>
11908 X-List-Received-Date: Fri, 06 May 2005 15:48:21 -0000
11909 Content-Length: 1018
11914 I'm sorry: for some reason I didn't receive your post. I only saw it as
11915 I browsed the LinuxChix archives.
11917 Your first exercise is excellent. Good for you for remembering to begin
11918 with "^" and end with ":".
11920 > I wonder if there is a way to know when your regular expression
11923 That's a classic problem with regular expressions: it really is hard to
11924 know that you won't get any false matches. Caveat hacker.
11926 Now on to your second example:
11928 > my $file = <STDIN>;
11930 > open MY_INPUT, "< $file" or die "Couldn't open input file: $!";
11932 Notice that you didn't chomp the input, but the "open" statement takes it anyway.
11934 > if ($line =~ /\s*#include /)
11936 This is good, but you forgot to start the pattern with "^" this time. Caveat hackor!
11939 [Larry] Wall [inventor of Perl] believes that people think about
11940 things in different ways, that natural languages accommodate many
11941 mindsets, and that programming languages should too.
11942 - Jon Udell, in his essay, "A Perl Hacker in the Land of Python"
11946 From dan@cellectivity.com Fri May 13 18:58:34 2005
11947 Return-Path: <dan@cellectivity.com>
11948 X-Original-To: courses@linuxchix.org
11949 Delivered-To: courses@linuxchix.org
11950 Received: from localhost (localhost [127.0.0.1])
11951 by www.linuxchix.org (Postfix) with ESMTP id 431A527313F
11952 for <courses@linuxchix.org>; Fri, 13 May 2005 18:58:34 +1000 (EST)
11953 Received: from www.linuxchix.org ([127.0.0.1])
11954 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
11955 id 31541-10 for <courses@linuxchix.org>;
11956 Fri, 13 May 2005 18:58:34 +1000 (EST)
11957 Received: from cellectivity.com (unknown [212.18.242.163])
11958 by www.linuxchix.org (Postfix) with ESMTP id BA50F273134
11959 for <courses@linuxchix.org>; Fri, 13 May 2005 18:58:32 +1000 (EST)
11960 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
11961 with Microsoft SMTPSVC(6.0.3790.211);
11962 Fri, 13 May 2005 09:59:30 +0100
11963 Subject: [Courses] [Perl] Part 7: Changing Text with the s/// Operator
11964 From: Dan <dan@cellectivity.com>
11965 To: courses@linuxchix.org
11966 Content-Type: text/plain
11967 Date: Fri, 13 May 2005 09:58:25 +0100
11968 Message-Id: <1115974705.8220.6.camel@localhost.localdomain>
11970 X-Mailer: Evolution 2.0.4
11971 Content-Transfer-Encoding: 7bit
11972 X-OriginalArrivalTime: 13 May 2005 08:59:30.0296 (UTC)
11973 FILETIME=[12DCAF80:01C5579A]
11974 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
11976 X-BeenThere: courses@linuxchix.org
11977 X-Mailman-Version: 2.1.5
11979 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
11980 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11981 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
11982 List-Archive: <http://linuxchix.org/pipermail/courses>
11983 List-Post: <mailto:courses@linuxchix.org>
11984 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
11985 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
11986 <mailto:courses-request@linuxchix.org?subject=subscribe>
11987 X-List-Received-Date: Fri, 13 May 2005 08:58:34 -0000
11988 Content-Length: 7292
11991 LinuxChix Perl Course Part 7: Changing Text with the s/// Operator
11995 3) Data Extraction with s///
11998 6) Answer to Previous Exercise
11999 7) Acknowledgements
12003 ----------------------------------------
12007 The s/// operator is my (personal) favorite operator in Perl. It
12008 provides a very powerful means of changing text based on regular
12011 ----------------------------------------
12015 Try running this program:
12020 my $message = 'You need to RTFM.';
12022 $message =~ s/RTFM/Read The Fine Manual/;
12023 print "$message\n";
12025 As you can see, the s/// operator substitutes one string for another.
12027 Although the above example doesn't demonstrate it very well, s///
12028 takes a regular expression. So the following substitution would also
12031 $message =~ s/[A-Z]{4}/Read The Fine Manual/;
12033 By the way, s/// can take any delimiter, just like its cousin m//:
12040 ----------------------------------------
12042 3) Data Extraction with s///
12044 Like m//, s/// can use parentheses to extract data. The data can then
12045 be inserted into the substitution expression, like this:
12047 # Change reference from GIF to PNG.
12048 $html =~ s/<img src="(.*?)\.gif"/<img src="$1.png"/;
12050 The "$1" on the right refers to the "(.*?)" on the left.
12052 ----------------------------------------
12056 There are three common sources of errors when using s///. Let's
12057 illustrate them with an example.
12062 my $html = '<p>Just <!-- xxx --> some <!-- xxx --> text</p>';
12063 $html =~ s/<!--.*-->//; # Strip HTML comments - maybe.
12066 The first problem is greediness, just as we saw with m//. The s///
12067 will match from the beginning of the first comment to the end of the
12068 second comment, and strip out the word "some" in the middle. Instead,
12071 $html =~ s/<!--.*?-->//; # Notice the "?".
12073 That turns off greediness: the match will be as small as possible.
12075 But there's another problem: only one comment will be removed. If we
12076 want to remove both comments, we could execute the s/// line twice,
12077 but a better way is to use the "g" option ("g" for "global
12080 $html =~ s/<!--.*?-->//g; # Notice the "g" on the end.
12082 Now every match will be substituted, no matter how many there are.
12084 As for the final pitfall, we'll have to change the example a little:
12089 my $html = "<p>A <!-- \n multi-line \n --> string in Perl</p>";
12090 $html =~ s/<!--.*?-->//g; # Strip HTML comments - maybe.
12093 Each "\n" is a newline. It's silly to hard-code them like that, but
12094 if we had read the HTML code from a file there would probably be a
12095 lot of newlines scattered around and we'd have to deal with them.
12097 If you try executing this program, you'll see that the substitution
12098 doesn't occur. That's because by default the "." pattern matches any
12099 character EXCEPT a newline. To get "." to match newlines as well, use
12100 the "s" option (for "treat as Single line"):
12102 $html =~ s/<!--.*?-->//gs; # Note the "s" after the "g".
12104 In this case, we combined the "g" and "s" options. That's fine, and
12105 the order doesn't matter.
12107 So to conclude, there are three common pitfalls with s///:
12108 a) greediness: use "?" to turn it off
12109 b) number of replacements: use "g" to replace all occurrences
12110 c) newlines: use "s" to match across newlines
12112 ----------------------------------------
12116 a) Harry Potter fans know that Professor Lockhart succeeded Professor
12117 Quirrel as Dark Arts teacher. What they don't know is that Headmaster
12118 Dumbledore got a Perl hacker to write a quick script to change the
12119 professor's name on Hogwarts' web site. It read from standard input
12120 and sent the result to standard output. What did the script look
12123 b) Harvard cardiologist Thomas Michel writes in his "Guide to
12124 Politically Correct Cardiology" about the importance of using
12125 inoffensive medical terms. For example, he suggests saying
12126 "metabolically different" instead of the highly offensive "dead"[*].
12127 Write a Perl program that reads a medical diagnosis (or any other
12128 input) and uses "s///" to change the word "dead" to "metabolically
12129 different". How are you going to avoid false matches such as
12132 [*] Yes, the paper is real and the scientist is real. Of course, he
12133 wasn't taking himself too seriously when he wrote the paper.
12135 c) American English is slightly different from UK English in several
12136 respects, one of which is spelling[*]. For example, words ending in
12137 "ise" in England generally end in "ize" in the United States, e.g.,
12138 "exercise" becomes "exercize". Write a Perl program that "translates"
12139 such words from American to UK English, i.e., changes words ending in
12140 "ize" to end in "ise". For extra credit, take into account variations
12141 like "exercizes" and "exercizing".
12143 [*] Blame it on Noah Webster. The writer of the first dictionary in
12144 America, Webster deliberately chose to spell words differently for
12145 reasons both practical (making the language easier to learn for
12146 immigrants) and patriotic (declaring linguistic independence).
12148 d) Consider the following program:
12154 99 bottles of beer on the wall.
12155 99 bottles of beer.
12156 Take one down, pass it around,
12157 98 bottles of beer on the wall.\n\n";
12160 $verse =~ s/(\d+)/ $1 - 1 /ge;
12163 Notice the "e" option in the "s///" statement. What does it do?
12165 (In case you're wondering, I don't drink beer. The words come from an
12166 old and boring song which counts from 99 to 1.)
12168 ----------------------------------------
12170 6) Answer to Previous Exercise
12172 Here is a program that outputs your home directory and shell.
12177 my $uid = $<; # $< is system UID.
12179 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
12181 while ( defined( my $line = <PASSWD> ) ) {
12182 if ( $line =~ m/^[^:]+:[^:]*:$uid:.*:(.*?):(.*?)$/ ) {
12183 print "Home dir = $1\n";
12184 print "Shell = $2\n";
12190 Note that the regular expression demonstrates two ways to prevent
12191 greediness. On the left side of the regular expression, I use /[^:]/
12192 to ensure that the match can't include a colon, which separates the
12193 fields. On the right side I use /.*?/, which causes the match to be
12194 as short as possible.
12196 ----------------------------------------
12198 7) Acknowledgements
12200 A big thank you to Jacinta Richardson for suggestions and
12201 proofreading. More advanced Perl users might want to check out the
12202 free material from Perl Training Australia
12203 <http://www.perltraining.com.au/>, which she is a part of.
12205 Other contributors include Meryll Larkin.
12207 ----------------------------------------
12211 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
12212 Richter and Alice Wood, and is released under the same license as
12213 Perl itself (Artistic License or GPL, your choice). This is the
12214 license of choice to make it easy for other people to integrate your
12215 Perl code/documentation into their own projects. It is not generally
12216 used in projects unrelated to Perl.
12219 From dan@cellectivity.com Sat May 21 01:11:05 2005
12220 Return-Path: <dan@cellectivity.com>
12221 X-Original-To: courses@linuxchix.org
12222 Delivered-To: courses@linuxchix.org
12223 Received: from localhost (localhost [127.0.0.1])
12224 by www.linuxchix.org (Postfix) with ESMTP id 276BA273148
12225 for <courses@linuxchix.org>; Sat, 21 May 2005 01:11:05 +1000 (EST)
12226 Received: from www.linuxchix.org ([127.0.0.1])
12227 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
12228 id 27978-03 for <courses@linuxchix.org>;
12229 Sat, 21 May 2005 01:11:05 +1000 (EST)
12230 Received: from cellectivity.com (unknown [212.18.242.163])
12231 by www.linuxchix.org (Postfix) with ESMTP id B1229273007
12232 for <courses@linuxchix.org>; Sat, 21 May 2005 01:11:03 +1000 (EST)
12233 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
12234 with Microsoft SMTPSVC(6.0.3790.211);
12235 Fri, 20 May 2005 16:12:00 +0100
12236 Subject: [Courses] [Perl] Part 8: The Special Variable $_
12237 From: Dan <dan@cellectivity.com>
12238 To: courses@linuxchix.org
12239 Content-Type: text/plain
12240 Date: Fri, 20 May 2005 16:10:51 +0100
12241 Message-Id: <1116601851.3353.50.camel@localhost.localdomain>
12243 X-Mailer: Evolution 2.0.4
12244 Content-Transfer-Encoding: 7bit
12245 X-OriginalArrivalTime: 20 May 2005 15:12:00.0156 (UTC)
12246 FILETIME=[454EEDC0:01C55D4E]
12247 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
12249 X-BeenThere: courses@linuxchix.org
12250 X-Mailman-Version: 2.1.5
12252 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
12253 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12254 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
12255 List-Archive: <http://linuxchix.org/pipermail/courses>
12256 List-Post: <mailto:courses@linuxchix.org>
12257 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
12258 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12259 <mailto:courses-request@linuxchix.org?subject=subscribe>
12260 X-List-Received-Date: Fri, 20 May 2005 15:11:05 -0000
12261 Content-Length: 7054
12264 LinuxChix Perl Course Part 8: The Special Variable $_
12267 2) An Implied Variable
12268 3) $_ in While Loops
12269 4) Back to the Original Example
12271 6) Answers to Previous Exercises
12272 7) Acknowledgements
12276 ----------------------------------------
12280 If you've read real-life Perl code before, you may remember being
12281 puzzled by parts that read like this:
12283 while ( <STDIN> ) {
12288 Most of this already looks familiar from the previous few weeks. But
12289 this code is confusing because it doesn't look like any variables are
12290 assigned or changed. We're now going to unravel this mystery.
12292 ----------------------------------------
12294 2) An Implied Variable
12296 We've seen that Perl has some funny variables, like "$<" for your
12297 UID. But by far the most useful of these is "$_" (pronounced
12298 "dollar-underscore").
12300 This variable is passed as an understood parameter to many functions
12301 and operators if an expected parameter is not explicitly passed.
12304 chomp; # Equivalent to chomp($_);
12305 print; # Equivalent to print $_;
12307 "$_" is also the understood subject of "m//" and "s///":
12309 s/foo/bar/; # Equivalent to $_ =~ s/foo/bar/
12310 if ( /baz/ ) { ... } # Equivalent to $_ =~ m/baz/
12312 By the way, despite the "magic" nature of "$_", it can still be
12313 assigned to or from just like any other variable.
12315 ----------------------------------------
12317 3) $_ in While Loops
12319 For any file handle "HANDLE", the following are equivalent:
12321 while ( <HANDLE> ) { ... }
12322 # ... is equivalent to ...
12323 while ( defined( $_ = <HANDLE> ) ) { ... }
12325 Note that this trick only works if "<HANDLE>" is used as the sole
12326 criterion of a while loop. So the following do NOT set "$_":
12328 <HANDLE>; # NOT equivalent to $_ = <HANDLE>
12329 if ( <HANDLE> ) { ... } # NOT equivalent to $_ = <HANDLE>
12331 ----------------------------------------
12333 4) Back to the Original Example
12335 After this explanation, the example we started with looks a lot
12338 while ( <STDIN> ) { # while ( defined( $_ = <STDIN> ) ) {
12339 s/foo/bar/; # $_ =~ s/foo/bar/;
12343 ----------------------------------------
12347 a) Modify a recent program that we've written to use "$_" instead of
12348 a variable. Try to avoid explicitly naming "$_".
12350 b) Another "implied" variable is "ARGV", a which is the default
12351 argument to the diamond operator "<>". Try running the following
12352 program to find out what it does.
12361 Hint: try specifying several file names on the command line.
12363 ----------------------------------------
12365 6) Answers to Previous Exercises
12367 a) The following program changes "Quirrel" to "Lockhart":
12372 while ( defined(my $line = <STDIN>) ) {
12373 $line =~ s/Quirrel/Lockhart/g;
12377 To simply things we don't worry about false matches. However, there
12378 is a small chance that we'll run into trouble if there's a discussion
12379 of squirrels. The answer to the next exercise explains how to avoid
12382 b) Rather than just explaining the solution to this one, I'm going to
12383 go through the reasoning to find the solution.
12385 When we try to convert "dead" to "metabolically different", we want
12386 to match the whole word "dead", but not words that contain it. So we
12387 do something like this:
12389 $line =~ s/ dead / metabolically different /g;
12391 The spaces around the word avoid false matches like "deaden", but
12392 they also mean that the word won't be replaced if it's followed by a
12393 comma or some other punctuation. So we want a line like this:
12395 $line =~ s/[^A-Za-z]dead[^A-Za-z]/metabolically different/g;
12397 But that causes the characters before and after the word to be
12398 replaced as well, so we have to include them on the right side of the
12402 s/([^A-Za-z])dead([^A-Za-z])/${1}metabolically different$2/g;
12404 Oh dear: now we won't match a line that begins or ends with "dead"
12405 because our pattern requires a character before and after the word.
12406 So we'll have to code some special cases:
12409 s/([^A-Za-z])dead([^A-Za-z])/${1}metabolically different$2/g;
12410 $line =~ s/^dead([^A-Za-z])/metabolically different$1/g;
12411 $line =~ s/([^A-Za-z])dead$/${1}metabolically different/g;
12412 $line =~ s/^dead$/metabolically different/g;
12414 Relax: it gets better now. The point of this whole mess was to bring
12415 up some useful Perl functionality. We can avoid having to write
12416 "[^A-Za-z]" all the time by using \W instead. \w (lowercase) matches
12417 a "word character" (letter, digit or underscore). \W (uppercase)
12418 matches anything that's not \w. So we can write this instead:
12420 $line =~ s/(\W)dead(\W)/${1}metabolically different$2/g;
12423 But an even better solution is to use the word boundary character:
12424 \b. This handy little guy matches the space between two characters:
12425 between a \w and a \W (in either order) or between a \w and the
12426 beginning or end of the string. It's actually zero-length, so we
12427 don't have to worry about replacing it.
12429 So we can do everything in one substitution:
12431 $line =~ s/\bdead\b/metabolically different/g;
12433 And the whole program looks like this:
12438 while ( defined(my $line = <STDIN>) ) {
12439 $line =~ s/\bdead\b/metabolically different/g;
12443 c) To change U.S. spelling to U.K. spelling, we use \b as in the
12449 while ( defined(my $line = <STDIN>) ) {
12450 $line =~ s/ize\b/ise/g;
12454 Here is a more advanced version that takes into account variations
12455 like "realised" and "desensitising". It also avoids changing the
12456 spelling of the words "size", "resize" and "downsize", which are
12457 spelt the same way in the U.K. as in America:
12462 while ( defined(my $line = <STDIN>) ) {
12463 $line =~ s/iz(e|es|ed|ing)\b/is$1/g;
12464 $line =~ s/\b(re|down)?sis(e|es|ed|ing)\b/$1siz$2/g;
12468 d) The "e" option causes the substitution to be evaluated as Perl
12469 code. You can use this to cause "s///" to call an arbitrary function
12472 ----------------------------------------
12474 7) Acknowledgements
12476 A big thank you to Jacinta Richardson for suggestions and
12477 proofreading. More advanced Perl users might want to check out the
12478 free material from Perl Training Australia
12479 <http://www.perltraining.com.au/>, which she is a part of.
12481 Other contributors include Meryll Larkin.
12483 ----------------------------------------
12487 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
12488 Richter and Alice Wood, and is released under the same license as
12489 Perl itself (Artistic License or GPL, your choice). This is the
12490 license of choice to make it easy for other people to integrate your
12491 Perl code/documentation into their own projects. It is not generally
12492 used in projects unrelated to Perl.
12495 From dan@cellectivity.com Sat May 28 00:57:16 2005
12496 Return-Path: <dan@cellectivity.com>
12497 X-Original-To: courses@linuxchix.org
12498 Delivered-To: courses@linuxchix.org
12499 Received: from localhost (localhost [127.0.0.1])
12500 by www.linuxchix.org (Postfix) with ESMTP id CB75F273151
12501 for <courses@linuxchix.org>; Sat, 28 May 2005 00:57:15 +1000 (EST)
12502 Received: from www.linuxchix.org ([127.0.0.1])
12503 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
12504 id 23039-02 for <courses@linuxchix.org>;
12505 Sat, 28 May 2005 00:57:15 +1000 (EST)
12506 Received: from cellectivity.com (unknown [212.18.242.163])
12507 by www.linuxchix.org (Postfix) with ESMTP id B1780273028
12508 for <courses@linuxchix.org>; Sat, 28 May 2005 00:57:13 +1000 (EST)
12509 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
12510 with Microsoft SMTPSVC(6.0.3790.211);
12511 Fri, 27 May 2005 15:58:12 +0100
12512 Subject: [Courses] [Perl] Part 9: Intro to Arrays
12513 From: Dan <dan@cellectivity.com>
12514 To: courses@linuxchix.org
12515 Content-Type: text/plain
12516 Date: Fri, 27 May 2005 15:57:01 +0100
12517 Message-Id: <1117205821.7713.60.camel@localhost.localdomain>
12519 X-Mailer: Evolution 2.0.4
12520 Content-Transfer-Encoding: 7bit
12521 X-OriginalArrivalTime: 27 May 2005 14:58:12.0906 (UTC)
12522 FILETIME=[811ED8A0:01C562CC]
12523 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
12525 X-BeenThere: courses@linuxchix.org
12526 X-Mailman-Version: 2.1.5
12528 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
12529 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12530 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
12531 List-Archive: <http://linuxchix.org/pipermail/courses>
12532 List-Post: <mailto:courses@linuxchix.org>
12533 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
12534 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12535 <mailto:courses-request@linuxchix.org?subject=subscribe>
12536 X-List-Received-Date: Fri, 27 May 2005 14:57:16 -0000
12537 Content-Length: 5395
12540 LinuxChix Perl Course Part 9: Intro to Arrays
12543 2) A Simple Example
12545 4) Filling an Array
12547 6) Answers to Previous Exercises
12548 7) Acknowledgements
12551 ----------------------------------------
12555 As you may recall, there are four basic data types in Perl:
12556 a) scalar - A single chunk of data
12557 b) array - A bunch of scalars, indexed by a number
12558 c) hash (hashtable) - A bunch of scalars, indexed by another scalar
12560 d) handle - A pointer enabling the opening of resources from the
12561 operating system (files, directories, etc.)
12563 Up until now we've only looked at scalars and handles, but now we're
12564 going to look at arrays.
12566 Arrays in Perl are both handy and powerful, but it takes time to
12567 learn to use them properly. For this reason, we're going to spend
12568 three weeks looking at arrays:
12569 a) This week we'll see array basics.
12570 b) Next week we'll look at some helpful array functions.
12571 c) The following week we'll see the painful details of arrays.
12573 ----------------------------------------
12575 2) A Simple Example
12577 Here's a simple program that outputs what I'll eat for lunch today.
12582 my @lunch = ('sandwich', 'apple', 'cookie');
12583 print "My lunch today: @lunch\n";
12585 As you can see, an array variable is preceded by "@" rather than "$".
12587 ----------------------------------------
12591 The above example wasn't very helpful because we didn't access the
12592 array elements separately. Let's try a slightly more complicated
12598 my @lunch = ('sandwich', 'apple', 'cookie');
12599 foreach my $item (@lunch) {
12600 print "I'll eat a $item.\n";
12603 In addition to accessing each member of an array, "foreach" can be
12604 used to change members of an array. This program is a demonstration.
12609 my @arr = ('metadata', 'meta-information', 'metaphysics');
12610 foreach my $item (@arr) {
12611 $item =~ s/meta-?/useful /;
12615 ----------------------------------------
12617 4) Filling an Array
12619 There are many ways to fill an array. Here are a few.
12621 a) From a file handle:
12623 my @lines = <HANDLE>; # Read whole file.
12625 b) From another array:
12629 In the above case, "@b" contains a copy of "@a", not a pointer to it.
12630 Changes to either will not affect the other.
12634 # The following finds everything that comes after the letter "Q".
12635 my $text = "There's an Iraqi squid on my qwerty keyboard!";
12636 my @letters = ( $text =~ m/q([a-z])/g );
12637 print "@letters\n"; # Output is: i u w
12639 The "g" option means a global match: return everything at once.
12641 ----------------------------------------
12645 a) Write a program that reads an HTML file and removes all the HTML
12646 tags, leaving just the text. (Yes, I know we could do this without
12647 arrays, but read the file as an array of lines.)
12649 Note: if spaces magically appear at the beginning of the lines in
12650 your output file, it's because you wrote the "print" statement like
12652 print OUTPUT "@lines"; # Quotes around @lines
12653 Instead, try writing it like this:
12654 print OUTPUT @lines; # No quotes
12656 b) Consider this program:
12661 my @arr = ('sing', 'ring', 'fling');
12667 If no scalar variable is specified in the "foreach" statement, what
12670 c) Consider this program:
12675 my @a = ('aardvark', 'beaver', 'ant', 'squirrel');
12676 my @b = grep /^a/, @a; # Match words starting with A.
12679 What does "grep" do? "grep" is a very powerful tool, and well worth
12680 reading the manual for:
12684 ----------------------------------------
12686 6) Answers to Previous Exercises
12688 a) This is a variation of the program that converts U.S. spelling to
12689 U.K. spelling. This one changes the spelling of e.g. "color" to
12695 while ( <STDIN> ) {
12696 s/(\w\w[^aeiou])or\b/$1our/g;
12700 (Actually, this example isn't as good as the previous one because
12701 there are more exceptions to the rule. For example, the word
12702 "monitor" is spelt the same way in the U.S. and the U.K.)
12704 b) The "ARGV" file handle, which is implied by an empty diamond
12705 operator "<>", treats each command-line parameter as a file name and
12706 reads from each of these files in succession. If no file is specified
12707 on the command-line, it reads from standard input. This is the
12708 standard behaviour for many Unix commands.
12710 ----------------------------------------
12712 7) Acknowledgements
12714 A big thank you to Jacinta Richardson for suggestions and
12715 proofreading. More advanced Perl users might want to check out the
12716 free material from Perl Training Australia
12717 <http://www.perltraining.com.au/>, which she is a part of.
12719 Other contributors include Meryll Larkin.
12721 ----------------------------------------
12725 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
12726 Richter and Alice Wood, and is released under the same license as
12727 Perl itself (Artistic License or GPL, your choice). This is the
12728 license of choice to make it easy for other people to integrate your
12729 Perl code/documentation into their own projects. It is not generally
12730 used in projects unrelated to Perl.
12733 From dan@cellectivity.com Fri Jun 3 19:02:42 2005
12734 Return-Path: <dan@cellectivity.com>
12735 X-Original-To: courses@linuxchix.org
12736 Delivered-To: courses@linuxchix.org
12737 Received: from localhost (localhost [127.0.0.1])
12738 by www.linuxchix.org (Postfix) with ESMTP id 9041B273158
12739 for <courses@linuxchix.org>; Fri, 3 Jun 2005 19:02:42 +1000 (EST)
12740 Received: from www.linuxchix.org ([127.0.0.1])
12741 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
12742 id 16720-02 for <courses@linuxchix.org>;
12743 Fri, 3 Jun 2005 19:02:42 +1000 (EST)
12744 Received: from cellectivity.com (unknown [212.18.242.163])
12745 by www.linuxchix.org (Postfix) with ESMTP id E1454273007
12746 for <courses@linuxchix.org>; Fri, 3 Jun 2005 19:02:40 +1000 (EST)
12747 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
12748 with Microsoft SMTPSVC(6.0.3790.211); Fri, 3 Jun 2005 10:03:39 +0100
12749 Subject: [Courses] [Perl] Part 10: Array Functions
12750 From: Dan <dan@cellectivity.com>
12751 To: courses@linuxchix.org
12752 Content-Type: text/plain
12753 Date: Fri, 03 Jun 2005 10:02:30 +0100
12754 Message-Id: <1117789350.7553.10.camel@localhost.localdomain>
12756 X-Mailer: Evolution 2.0.4
12757 Content-Transfer-Encoding: 7bit
12758 X-OriginalArrivalTime: 03 Jun 2005 09:03:39.0953 (UTC)
12759 FILETIME=[22581210:01C5681B]
12760 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
12762 X-BeenThere: courses@linuxchix.org
12763 X-Mailman-Version: 2.1.5
12765 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
12766 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12767 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
12768 List-Archive: <http://linuxchix.org/pipermail/courses>
12769 List-Post: <mailto:courses@linuxchix.org>
12770 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
12771 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12772 <mailto:courses-request@linuxchix.org?subject=subscribe>
12773 X-List-Received-Date: Fri, 03 Jun 2005 09:02:42 -0000
12774 Content-Length: 5896
12777 LinuxChix Perl Course Part 10: Array Functions
12780 2) push, pop, shift, unshift
12782 4) sort and reverse
12783 5) Consider the Context
12785 7) Answers to Previous Exercises
12786 8) Acknowledgements
12790 ----------------------------------------
12794 Last week we saw some basic uses of arrays. This week we're going to
12795 look at the powerful functions that Perl provides for manipulating
12798 Next week we're going to see how to index arrays. The reason we save
12799 that for last is that you don't need to index arrays very often: Perl
12800 does most of the low-level array manipulation for you.
12802 ----------------------------------------
12804 2) push, pop, shift, unshift
12806 "push" and "pop" add and remove items from the end of an array.
12808 my @arr = ('A', 'B', 'C', 'D');
12809 my $popped = pop(@arr); # now $popped = 'D'
12810 push(@arr, 'X', 'Y'); # now @arr = A B C X Y
12812 "shift" and "unshift" are similar, but they add or remove items from
12813 the beginning. If you don't pass an array to "shift", the list of
12814 command-line parameters ("@ARGV") is used instead. This is the most
12815 common use if "shift".
12817 ----------------------------------------
12821 "split" splits a string:
12823 my $password_file_entry = 'root:x:0:0:root:/root:/bin/bash';
12824 my @fields = split(/:/, $password_file_entry);
12825 print "@fields\n"; # Output is: root x 0 0 root /root /bin/bash
12827 The first parameter to "split" is a regular expression, so it can
12828 split strings in very sophisticated ways.
12830 The opposite of "split" is "join":
12832 my @path = ('/bin', '/usr/sbin', '/usr/bin' );
12833 print join(':', @path); # Output is: /bin:/usr/sbin:/usr/bin
12835 ----------------------------------------
12837 4) sort and reverse
12839 "sort" returns an array in alphabetical order (NOT numerical order).
12841 my @animals = ('gopher', 'bat', 'yak');
12842 my @sorted = sort(@animals);
12843 print "@sorted\n"; # Output is: bat gopher yak
12845 "sort" allows you to give a code block or a reference to a comparison
12846 function as the first argument. We haven't gotten to function
12847 references yet, but the following convenient trick allows you to sort
12848 in numerical order:
12850 @arr = sort( {$a<=>$b} @array_of_numbers );
12852 "reverse" reverses the order of an array. This is NOT the same as
12853 sorting it in reverse order, unless the array was already sorted.
12855 ----------------------------------------
12857 5) Consider the Context
12859 Many Perl functions and operators behave differently depending on the
12860 "context". Perl has three possible contexts: scalar context, list
12861 context and void context:
12863 my @arr = ('foo', 'bar', 'baz');
12864 my @a = reverse(@arr); # Array context
12865 my $s = reverse(@arr); # Scalar context
12866 reverse(@arr); # Void context
12868 print "@a\n"; # Output is: baz bar foo
12869 print "$s\n"; # Output is: zabraboof (foobarbaz backwards)
12871 Note that the context is determined by how the return value is used,
12872 NOT the parameters passed to the function/operator. Many languages
12873 allow a function to change according to its input, but Perl is the
12874 only language that I know that allows a function to change its
12875 behaviour depending on how its output is used!
12877 The use of an array in scalar context returns the length of the
12880 In the above cases the context was obvious because we assigned to an
12881 array or scalar variable. But the context can also be indicated by an
12882 operator. For example, arithmetic operators are all scalar, as is
12883 string concatenation:
12885 print "The length of the array is: " . @array . "\n";
12887 ----------------------------------------
12891 a) Write a program that mimics the Unix command "sort". (No, you
12892 don't have to implement all the command-line options; just read from
12893 standard in and write to standard out.)
12895 b) Write a summation program that accepts input composed of positive
12896 integers separated by plus signs with any amount of space in between.
12897 For example, use it to compute this line:
12901 Remember, There Is More Than One Way To Do It (TIMTOWTDI).
12903 ----------------------------------------
12905 7) Answers to Previous Exercises
12907 a) The following program strips HTML tags from a file:
12912 open MY_INPUT, "< file1.html" or die "Couldn't open input file:
12914 my @lines = <MY_INPUT>; # Read whole file.
12917 foreach my $line (@lines) {
12918 $line =~ s/<.*?>//g;
12921 open MY_OUTPUT, "> file2.html" or die "Couldn't open output file:
12923 print MY_OUTPUT @lines;
12926 b) If no scalar variable is specified in a "foreach" loop, the
12927 special variable "$_" is used.
12929 c) The "grep" command in Perl compares returns all elements of an
12930 array that meet a certain condition. Each element is stored
12931 sequentially in "$_" an the condition is tested. Examples of "grep"
12934 @a = grep /foo/, @b; # Containing "foo"
12935 @a = grep( $_ > 5, @b ); # Numbers greater than 5
12936 @a = grep defined, @b; # Filter undef.
12938 As with all Perl functions, parentheses are optional with "grep".
12940 ----------------------------------------
12942 8) Acknowledgements
12944 A big thank you to Jacinta Richardson for suggestions and
12945 proofreading. More advanced Perl users might want to check out the
12946 free material from Perl Training Australia
12947 <http://www.perltraining.com.au/>, which she is a part of.
12949 Other contributors include Meryll Larkin.
12951 ----------------------------------------
12955 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
12956 Richter and Alice Wood, and is released under the same license as
12957 Perl itself (Artistic License or GPL, your choice). This is the
12958 license of choice to make it easy for other people to integrate your
12959 Perl code/documentation into their own projects. It is not generally
12960 used in projects unrelated to Perl.
12963 From dan@cellectivity.com Sat Jun 11 01:00:58 2005
12964 Return-Path: <dan@cellectivity.com>
12965 X-Original-To: courses@linuxchix.org
12966 Delivered-To: courses@linuxchix.org
12967 Received: from localhost (localhost [127.0.0.1])
12968 by www.linuxchix.org (Postfix) with ESMTP id 43971273166
12969 for <courses@linuxchix.org>; Sat, 11 Jun 2005 01:00:58 +1000 (EST)
12970 Received: from www.linuxchix.org ([127.0.0.1])
12971 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
12972 id 17004-02 for <courses@linuxchix.org>;
12973 Sat, 11 Jun 2005 01:00:58 +1000 (EST)
12974 Received: from cellectivity.com (unknown [212.18.242.163])
12975 by www.linuxchix.org (Postfix) with ESMTP id A08E5273158
12976 for <courses@linuxchix.org>; Sat, 11 Jun 2005 01:00:56 +1000 (EST)
12977 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
12978 with Microsoft SMTPSVC(6.0.3790.211);
12979 Fri, 10 Jun 2005 16:02:00 +0100
12980 Subject: [Courses] [Perl] Part 11: More About Arrays
12981 From: Dan <dan@cellectivity.com>
12982 To: courses@linuxchix.org
12983 Content-Type: text/plain
12984 Date: Fri, 10 Jun 2005 16:00:47 +0100
12985 Message-Id: <1118415647.7499.24.camel@localhost.localdomain>
12987 X-Mailer: Evolution 2.0.4
12988 Content-Transfer-Encoding: 7bit
12989 X-OriginalArrivalTime: 10 Jun 2005 15:02:00.0390 (UTC)
12990 FILETIME=[5A7EA660:01C56DCD]
12991 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
12993 X-BeenThere: courses@linuxchix.org
12994 X-Mailman-Version: 2.1.5
12996 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
12997 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
12998 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
12999 List-Archive: <http://linuxchix.org/pipermail/courses>
13000 List-Post: <mailto:courses@linuxchix.org>
13001 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
13002 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13003 <mailto:courses-request@linuxchix.org?subject=subscribe>
13004 X-List-Received-Date: Fri, 10 Jun 2005 15:00:58 -0000
13005 Content-Length: 6051
13008 LinuxChix Perl Course Part 11: More About Arrays
13014 5) Answers to Previous Exercises
13015 6) Acknowledgements
13018 ----------------------------------------
13022 In the previous two weeks we've had a lot of fun with arrays. This
13023 week is going to be a little "heavier", focusing on the details.
13025 ----------------------------------------
13029 As you might have guessed, arrays are indexed using good old "[]" -
13030 but with a twist. The array name is (usually) preceded by a dollar
13031 sign rather than an at-sign:
13033 my @list = ('some', 'words');
13034 print "$list[0]\n"; # Indexes start at zero.
13036 As "man perldata" puts it: "$foo[1] is a part of @foo, not a part of
13037 $foo. This may seem a bit weird, but that's okay, because it is
13040 Don't let the humility of those words fool you: the system is
13041 actually quite clever when you fully understand it. The "[]"
13042 indicates that the variable is an array; the dollar sign indicates
13043 that what you want from it is a scalar. It may seem obvious that you
13044 would want a scalar, but Perl allows you to "slice" an array by
13045 specifying multiple indices:
13047 my @chars = ('A', 'B', 'C', 'D', 'E', 'F');
13048 my @a = @chars[1,5]; # Gets B and F.
13049 my @b = @chars[1 .. 4]; # Gets B, C, D and E.
13050 my @c = @chars[1,3..5]; # Gets B, D, E and F.
13053 my @d = @chars[@n]; # Gets C and F.
13054 @chars[@n] = @n; # Now @chars = A B 2 D E 5.
13056 When you slice, you get back an array, so the variable name is
13057 preceded by an "@".
13059 A negative subscript is interpreted as though it had been added to
13060 the length of the array, so negative numbers can be thought of as
13061 "counting backwards from the end":
13063 my @chars = ('A', 'B', 'C', 'D', 'E', 'F');
13064 print "$chars[-2]\n"; # Prints "E".
13066 It's not an error to access a non-existant subscript; you just get
13067 back "undef" (the undefined value). If you assign to a non-existant
13068 subscript, the array will be automatically extended.
13070 ----------------------------------------
13074 An array is a type of list. All lists in Perl are "flat": they can
13075 only contain scalars, not other lists. Example:
13077 my @men = ('Bob', 'Joe');
13078 my @women = ('Alice', 'Ronda', 'Patricia');
13079 my @everyone = (@men, @women); # Copy, not reference.
13080 my $length = $#everyone + 1; # $length is 5.
13081 print "$length names: @everyone\n";
13083 Trying to assign a whole array to a single array element won't work.
13084 It would seem that you can't create an array of arrays, but Perl
13085 provides a rather quirky way of getting around this limitation. We'll
13086 see it when we get to references, but if you're really curious (and
13087 bold), read "perldoc perllol". Note: in that document, brackets are
13088 used not only for array indexing, but also for array references.
13090 ----------------------------------------
13094 a) It's often convenient to communicate with spreadsheets and
13095 databases by writing files in Comma Separated Values (CSV) format.
13096 Under Windows, if you double-click a file ending in ".CSV", it will
13097 automatically be opened by Excel.
13099 A CSV file might look like this:
13101 This is A1,This is B1,This is C1,
13102 This is A2,This is B2,This is C2,This is D2
13103 This row contains numbers,51,187,,
13105 Suppose that this spreadsheet holds your old bank account records:
13107 Deposit,1000,1988-04-25,Payday
13108 Withdrawal,50,1988-04-28,
13109 Cheque,150,1988-04-30,Roof repair
13111 You realise that it would make more sense for everything except
13112 deposits to be negative, but the data are used in other calculations,
13113 so you decide to insert a column with the changed data rather than
13114 altering the original data.
13116 Write a Perl program that inserts a new column between the amount and
13117 the date (column C). The new column C should be exactly the same as
13118 column B, except that it should be positive for deposits and negative
13119 for everything else.
13121 For extra credit, sort by date. (Don't do any complex date
13122 conversions: the date is represented conveniently for this!)
13124 ----------------------------------------
13126 5) Answers to Previous Exercises
13128 a) The following program sorts its input:
13133 my @input = <>; # or use <STDIN> if you want.
13134 print sort(@input);
13136 Note that we use "<>" instead of "<STDIN>". This is for convenience:
13137 any file names provided on the command line will automatically be
13140 b) The following program computes a sum:
13145 my $line = <STDIN>;
13146 my @numbers = split( /\s*\+\s*/, $line );
13148 foreach my $n (@numbers) {
13153 The point of the example was to use "split". But it would make just
13154 as much sense to use "m//g":
13156 my @numbers = ( $line =~ /(\d+)/g );
13158 In fact, using "m//g" would have the advantage that we could more
13159 easily extend the program to work with subtraction:
13164 my $line = <STDIN>;
13165 my @numbers = ( $line =~ /(-|\+|\d+)/g );
13168 foreach my $x (@numbers) {
13169 if ( $x =~ /\d/ ) {
13170 if ( $sign eq '-' ) {
13183 ----------------------------------------
13185 6) Acknowledgements
13187 A big thank you to Jacinta Richardson for suggestions and
13188 proofreading. More advanced Perl users might want to check out the
13189 free material from Perl Training Australia
13190 <http://www.perltraining.com.au/>, which she is a part of.
13192 Other contributors include Meryll Larkin.
13194 ----------------------------------------
13198 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
13199 Richter and Alice Wood, and is released under the same license as
13200 Perl itself (Artistic License or GPL, your choice). This is the
13201 license of choice to make it easy for other people to integrate your
13202 Perl code/documentation into their own projects. It is not generally
13203 used in projects unrelated to Perl.
13206 From dan@cellectivity.com Sat Jun 18 01:04:21 2005
13207 Return-Path: <dan@cellectivity.com>
13208 X-Original-To: courses@linuxchix.org
13209 Delivered-To: courses@linuxchix.org
13210 Received: from localhost (localhost [127.0.0.1])
13211 by www.linuxchix.org (Postfix) with ESMTP id E648527316F
13212 for <courses@linuxchix.org>; Sat, 18 Jun 2005 01:04:20 +1000 (EST)
13213 Received: from www.linuxchix.org ([127.0.0.1])
13214 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
13215 id 10884-10 for <courses@linuxchix.org>;
13216 Sat, 18 Jun 2005 01:04:20 +1000 (EST)
13217 Received: from cellectivity.com (unknown [212.18.242.163])
13218 by www.linuxchix.org (Postfix) with ESMTP id 99F5F27316E
13219 for <courses@linuxchix.org>; Sat, 18 Jun 2005 01:04:18 +1000 (EST)
13220 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
13221 with Microsoft SMTPSVC(6.0.3790.211);
13222 Fri, 17 Jun 2005 16:05:16 +0100
13223 Subject: [Courses] [Perl] Part 12: Hashes
13224 From: Dan <dan@cellectivity.com>
13225 To: courses@linuxchix.org
13226 Content-Type: text/plain
13227 Date: Fri, 17 Jun 2005 16:04:00 +0100
13228 Message-Id: <1119020640.6538.31.camel@localhost.localdomain>
13230 X-Mailer: Evolution 2.0.4
13231 Content-Transfer-Encoding: 7bit
13232 X-OriginalArrivalTime: 17 Jun 2005 15:05:16.0718 (UTC)
13233 FILETIME=[F8682CE0:01C5734D]
13234 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
13236 X-BeenThere: courses@linuxchix.org
13237 X-Mailman-Version: 2.1.5
13239 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
13240 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13241 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
13242 List-Archive: <http://linuxchix.org/pipermail/courses>
13243 List-Post: <mailto:courses@linuxchix.org>
13244 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
13245 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13246 <mailto:courses-request@linuxchix.org?subject=subscribe>
13247 X-List-Received-Date: Fri, 17 Jun 2005 15:04:21 -0000
13248 Content-Length: 6435
13251 LinuxChix Perl Course Part 12: Hashes
13255 3) Hashes and Lists
13258 6) Answer to Previous Exercise
13259 7) Acknowledgements
13263 ----------------------------------------
13267 Recall that there are four basic data types in Perl:
13268 a) scalar - A single chunk of data
13269 b) array - A bunch of scalars, indexed by a number
13270 c) hash (hashtable) - A bunch of scalars, indexed by another scalar
13271 d) handle - A pointer enabling the opening of resources from the
13272 operating system (files, directories, etc.)
13274 We've seen three of these four, so there's only one to go: hashes. I
13275 assume that you've already been introduced to hashtables, but if you
13276 haven't, you should still be able to follow. Don't be afraid to ask
13277 the list for examples of when hashtables are useful.
13279 ----------------------------------------
13283 The following is an example program.
13288 my %capitals; # percent sign = hash
13289 $capitals{'England'} = 'London';
13290 $capitals{China} = 'Beijing'; # No quotes around China. OK.
13292 print "Enter a country. ";
13293 chomp(my $country = <STDIN>);
13295 if ( defined( $capitals{$country} ) ) {
13296 print "The capital of $country is $capitals{$country}\n";
13299 print "I don't know that country.\n";
13302 ----------------------------------------
13304 3) Hashes and Lists
13306 If a hash is assigned to or from a list, the items alternate key1,
13307 value1, key2, value2...
13309 my %h = ('a', 1, 'b', 2, 'c', 3);
13310 print $h{b}; # Output is: 2
13312 To make the association more apparent, we can use the "=>" operator,
13313 also called "fat comma":
13315 my %h = ('a' => 1, 'b' => 2, 'c' => 3);
13316 print $h{b}; # Output is: 2
13318 However, apart from the key-value pairing, a hash has no order:
13320 my %h = ('a' => 1, 'b' => 2, 'c' => 3);
13321 my @a = %h; # No problem, but order is lost.
13322 print $a[0]; # It will be one of a,b,c - but we don't know which!
13324 ----------------------------------------
13328 The "keys" function returns the list of keys in a hashtable:
13330 foreach my $k ( keys(%hash) ) {
13331 print "$k : $hash{$k}\n";
13334 The less used "values" function returns the list of values associated
13337 ----------------------------------------
13341 a) Write a program that reads /etc/passwd and creates a mapping of
13342 usernames to UIDs. Test by inviting the user to enter a username and
13343 outputting the UID.
13345 b) Write a Perl program that reads standard input and removes
13346 duplicate lines (so if you enter "foo", then "bar", then "foo" again,
13347 the second "foo" is not output). Remember: There Is More Than One Way
13348 To Do It (TIMTOWTDI).
13350 c) The "reverse" function reverses the order of an array:
13352 @arr = reverse(@arr);
13354 What happens if you use "reverse" on a hash?
13356 d) In our examples we haven't used numbers as hash keys, even though
13357 Perl allows it. If you are indexing by number, you would normally use
13358 an array rather than a hash. However, there are cases when a hash
13359 with integer keys is more appropriate than an array. Can you name
13362 e) The original BASIC interpreter might be considered a primitive
13363 IDE, because it was both an editor and an interpreter. You might use
13364 it something like this (with "user" and "comp" added for clarity):
13366 user> 10 PRINT "What is your name?";
13369 user> 20 INPUT NAME$
13372 user> 30 PRINT "Hello "; NAME$
13376 comp> 10 PRINT "What is your name?"
13377 comp> 20 INPUT NAME$
13378 comp> 30 PRINT "Hello "; NAME$
13381 comp> What is your name?
13383 comp> Hello Patricia
13386 user> SAVE "hello.bas"
13389 User commands beginning with a number mean lines in the program.
13390 Programs are always executed in order by line number, regardless of
13391 the order in which the lines were entered. Each line entered
13392 overwrites any line with the same number that may already exist. This
13393 was one way of editing programs before we had visual editors.
13395 Write your own subset of the BASIC interpreter. The choice of
13396 commands is up to you, but I recommend PRINT, INPUT, LIST and (of
13399 Hint 1: There are several good uses for hashes here, not the least of
13400 which is mapping line numbers to lines.
13402 Hint 2: Though you can require the user to enter uppercase if you
13403 want, a better solution is to use the "uc" function and/or "m//i".
13405 Hint 3: Remember that to sort a list in numerical order, you use
13406 "sort( {$a<=>$b} @array )"
13408 ----------------------------------------
13410 6) Answer to Previous Exercise
13412 a) This program processes a CSV file, including sorting rows by date.
13417 open MY_INPUT, "< input.csv" or die "Couldn't open input file:
13419 my @lines = <MY_INPUT>; # Read whole file.
13422 foreach my $line (@lines) {
13423 # Split line into fields.
13424 my @fields = split /,/, $line;
13426 # Decide whether amount should be negative.
13427 my $amount = $fields[1];
13428 $amount = -$amount if $fields[0] !~ /deposit/i;
13430 # Reconstruct fields, with date at the beginning
13431 # (for sorting) and new amount inserted in the middle.
13432 @fields = ( $fields[2], @fields[0,1], $amount,
13433 @fields[2..$#fields] );
13434 $line = join(',', @fields);
13437 # Sort by date (which is currently at the beginning).
13438 @lines = sort @lines;
13440 # Fast way of removing date from the beginning.
13441 s/^.*?,// for (@lines);
13443 open MY_OUTPUT, "> output.csv" or die "Couldn't open output file:
13445 print MY_OUTPUT @lines; # Write whole file (with newlines).
13448 ----------------------------------------
13450 7) Acknowledgements
13452 A big thank you to Jacinta Richardson for suggestions and
13453 proofreading. More advanced Perl users might want to check out the
13454 free material from Perl Training Australia
13455 <http://www.perltraining.com.au/>, which she is a part of.
13457 Other contributors include Meryll Larkin.
13459 ----------------------------------------
13463 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
13464 Richter and Alice Wood, and is released under the same license as
13465 Perl itself (Artistic License or GPL, your choice). This is the
13466 license of choice to make it easy for other people to integrate your
13467 Perl code/documentation into their own projects. It is not generally
13468 used in projects unrelated to Perl.
13471 From dan@cellectivity.com Sat Jun 25 00:59:59 2005
13472 Return-Path: <dan@cellectivity.com>
13473 X-Original-To: courses@linuxchix.org
13474 Delivered-To: courses@linuxchix.org
13475 Received: from localhost (localhost [127.0.0.1])
13476 by www.linuxchix.org (Postfix) with ESMTP id 3F440273177
13477 for <courses@linuxchix.org>; Sat, 25 Jun 2005 00:59:59 +1000 (EST)
13478 Received: from www.linuxchix.org ([127.0.0.1])
13479 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
13480 id 10821-08 for <courses@linuxchix.org>;
13481 Sat, 25 Jun 2005 00:59:59 +1000 (EST)
13482 Received: from cellectivity.com (unknown [212.18.242.163])
13483 by www.linuxchix.org (Postfix) with ESMTP id 6322827306B
13484 for <courses@linuxchix.org>; Sat, 25 Jun 2005 00:59:56 +1000 (EST)
13485 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
13486 with Microsoft SMTPSVC(6.0.3790.211);
13487 Fri, 24 Jun 2005 16:01:06 +0100
13488 Subject: [Courses] [Perl] Part 13: Functions
13489 From: Dan <dan@cellectivity.com>
13490 To: courses@linuxchix.org
13491 Content-Type: text/plain
13492 Date: Fri, 24 Jun 2005 15:59:48 +0100
13493 Message-Id: <1119625188.30749.20.camel@localhost.localdomain>
13495 X-Mailer: Evolution 2.0.4
13496 Content-Transfer-Encoding: 7bit
13497 X-OriginalArrivalTime: 24 Jun 2005 15:01:06.0968 (UTC)
13498 FILETIME=[8C6F9980:01C578CD]
13499 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
13501 X-BeenThere: courses@linuxchix.org
13502 X-Mailman-Version: 2.1.5
13504 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
13505 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13506 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
13507 List-Archive: <http://linuxchix.org/pipermail/courses>
13508 List-Post: <mailto:courses@linuxchix.org>
13509 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
13510 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13511 <mailto:courses-request@linuxchix.org?subject=subscribe>
13512 X-List-Received-Date: Fri, 24 Jun 2005 14:59:59 -0000
13513 Content-Length: 10863
13516 LinuxChix Perl Course Part 13: Functions
13519 2) A Simple Example
13520 3) A More Complicated Example
13522 5) Answers to Previous Exercises
13523 6) Acknowledgements
13527 ----------------------------------------
13531 As we write larger and larger programs, the need for functions
13532 becomes apparent. This week we're (finally) going to see how to write
13535 ----------------------------------------
13537 2) A Simple Example
13539 We're going to start with a simple example that returns whatever is
13542 sub identity_function {
13543 return @_; # @_ is array of parameters.
13546 my @result = identity_function('foo', 'bar', 'baz');
13549 Notice that the function accepts any number of parameters (in the
13550 form of an array, namely "@_") and returns any number of parameters
13551 (also in the form of an array). Try calling it like this:
13553 my @arr = ('b', 'c', 'd');
13554 my @result = identity_function('a', @arr, 'e');
13555 print "@result\n"; # Output is: a b c d e
13557 As you can see, the array is expanded when passed in. There is no way
13558 to pass in or return two distinct arrays; they are always merged.
13559 However, when we learn about references, we'll see that it's possible
13560 to pass in or return references to two distinct arrays.
13562 Perl provides wonderful flexibility in allowing any number of
13563 parameters to be passed in or returned. USE IT! For example, if you
13564 write a "max" function to return the largest number, don't limit the
13565 user to two: accept any number of parameters.
13567 ----------------------------------------
13569 3) A More Complicated Example
13571 Now let's try an example that's a little more interesting. In Perl
13572 there is no command to perform an s/// operation on an array, so
13573 here's a function to do just that.
13579 my $find = shift; # Get first parameter.
13580 my $replacement = shift; # Get second parameter.
13582 foreach ( @_ ) { # Now do the work.
13583 s/$find/$replacement/g; # s///g = Global replacement.
13586 # No return statement: change was made to input params.
13591 my $z = 'samaritan';
13592 array_s('[aeiou]', '_', $x, $y, $z);
13594 print "$x $y $z\n";
13596 A few things to note:
13597 a) "shift" is applied to "@_" if you provide it with no arguments.
13598 This has an effect similar to the "shift" statement in shell scripts.
13599 b) A change to "@_" also changes one of the variables passed in. (In
13600 other words, non-literals are passed by reference.)
13602 I don't actually recommend changing "@_"; it would probably be better
13603 to return the result instead. But Perl gives you lots of options, and
13604 one of those options is changing what's passed in.
13606 By the way, in Perl 4 we would have called the function with a
13607 preceeding ampersand, like this:
13609 &array_s('[aeiou]', '_', $x, $y, $z);
13611 This is no longer necessary in Perl 5, but you may see it in legacy
13612 code, and the ampersand is associated with functions in other ways
13613 which we'll see later.
13615 ----------------------------------------
13619 a) The "reverse" function, when used in scalar context, reverses the
13620 order of the characters in a string. It also acts on "$_", but we
13621 still can't do this:
13623 @arr = map(reverse, @arr); # Doesn't work.
13625 Write a "strictly_scalar_reverse" function that returns all the
13626 elements of an array written backwards. So
13627 "strictly_scalar_reverse('foo','bar');" will return "'oof','rab'".
13629 Bonus credit to anyone who can determine why using "map" doesn't
13630 work, and double bonus if you find a work-around.
13632 b) Consider the following code:
13635 print "You called foo with arguments @_\n";
13638 What happens if you call "foo" with exactly three scalar parameters?
13639 What happens if you call it with more or fewer? What happens if you
13640 call it with an array of exactly three elements?
13642 c) Consider the following code:
13646 return ('a', 'lot', 'of', 'scalars');
13658 What does the "wantarray" function do?
13660 ----------------------------------------
13662 5) Answers to Previous Exercises
13664 a) The following program uses /etc/passwd to map usernames to UIDs.
13665 Note that it takes advantage of "$_", which will no doubt delight
13666 some and disgust others.
13673 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
13674 while ( <PASSWD> ) {
13675 if ( /^(.*?):.*?:(\d+):/ ) {
13679 die "Encountered weird data in password file.";
13684 print "Enter a username and I'll give you a UID.\n";
13686 while ( my $username = <STDIN> ) {
13688 if ( defined($map{$username}) ) {
13689 print "User $username has UID $map{$username}\n";
13692 print "Sorry: I don't know that user.\n";
13696 b) The right way to write a program that removes duplicate lines is
13697 to store previous lines as keys in a hash:
13703 while ( my $line = <STDIN> ) {
13704 if ( ! defined( $previous{$line} ) ) {
13705 $previous{$line} = 1; # 1 is a dummy value.
13706 print $line; # No newline: wasn't chomped.
13710 Of course, There Is More Than One Way To Do It (TIMTOWTDI). A less
13711 elegant way is to store previous lines in an array and "grep" for
13712 them. This is much less efficient because it doesn't take advantage
13713 of the fast lookup abilities of a hashtable. But here's an example of
13720 # A much less efficient way to do the same thing.
13724 while ( my $line = <STDIN> ) {
13725 if ( ! grep {$_ eq $line} @previous ) {
13726 push(@previous, $line);
13727 print $line; # No need for \n : wasn't chomped.
13731 c) Calling "reverse" on a hash causes keys to become values and
13732 values to become keys.
13734 d) A hash is more appropriate than an array for indexing by number if
13735 the indeces are sparsely populated. For example, the first exercise
13736 was to map usernames to UIDs, but mapping UIDs to usernames would
13737 also be an appropriate use of a hash, because a system with only a
13738 few users will usually have UIDs over 500 (sometimes over 1000). You
13739 could store these in an array, but a lot of space (memory) would be
13740 wasted, so a hash is probably a better choice. The following exercise
13741 uses a hash to map line numbers to commands in BASIC for the same
13744 e) Here's the BASIC interpreter I came up with. It supports the
13745 PRINT, INPUT, LIST, RUN and SAVE commands, though it's very light on
13751 my %code; # Mapping of lines to commands.
13752 my %vars; # Mapping of variables to values.
13754 while ( my $line = <STDIN> ) {
13756 # Convert to uppercase, except stuff in quotes.
13757 # Adding a space prevents split from dropping last quote.
13758 my @chunks = split /"/, $line . ' ';
13759 for (my $i=0; $i<@chunks; $i+=2) {
13760 $chunks[$i] = uc($chunks[$i]);
13762 $line = join('"', @chunks);
13764 $line =~ s/^\s*|\s*$//g; # Remove white space.
13766 # User entered a line of code (starts with number).
13767 if ( $line =~ /^(\d+)\s*(.*)$/ ) {
13769 delete $code{$1} if $2 eq ''; # Blank line = delete.
13773 # User asked for a LIST of his code.
13774 elsif ( $line =~ /^LIST\s*(\d*)$/ ) {
13775 my @line_nums = ($1 ne '' ? $1 : keys(%code));
13776 @line_nums = sort( {$a <=> $b} @line_nums);
13777 foreach my $num (@line_nums) {
13778 print "$num $code{$num}\n";
13783 # User asked to RUN his code.
13784 elsif ( $line eq 'RUN' ) {
13785 my @line_nums = sort( {$a <=> $b} keys(%code));
13786 foreach my $num (@line_nums) {
13787 my($command,$rest) = split( /\s+/, $code{$num}, 2 );
13788 #print "Debug: $command|$rest\n";
13789 if ( $command eq 'INPUT' ) {
13791 chomp($vars{$rest} = <STDIN>);
13793 elsif ( $command eq 'PRINT' ) {
13794 # A PRINT takes args separated by semicolons.
13795 my $ends_in_semicolon = ($rest =~ /;\s*$/);
13796 while ( defined($rest) && $rest ne '' ) {
13797 if ($rest =~ /^"/) { # If it's a string.
13798 (my $string, $rest) = ($rest =~
13799 /^"(.*?)"\s*;?\s*(.*)$/);
13803 (my $var, undef, $rest) = ($rest =~
13804 /^(.*?)\s*(;\s*(.*)|$)/);
13805 print $vars{$var} if defined($vars{$var});
13808 # A PRINT command ending in a semicolon gets no newline.
13809 print "\n" unless $ends_in_semicolon;
13812 print "Unrecognised command at line $num: $command\n";
13817 # User asked to SAVE the program.
13818 elsif ( $line =~ /^SAVE\s+"(.*)"$/ ) {
13819 my $ok = open OUTPUT, "> $1";
13821 foreach my $number (sort keys %code) {
13822 print OUTPUT "$number $code{$number}\n";
13827 else { warn "Couldn't write $1: $!\n"; }
13830 elsif ( $line eq 'QUIT' ) {
13835 elsif ( $line eq '' ) {
13840 print "Unrecognised command\n";
13844 As you can see, there's a lot of functionality in a little code. We
13845 might be tempted to think that we could easily add more features and
13846 write a real BASIC interpreter, but it turns out that we're already
13847 approaching the limits of what Perl can do easily. (For example, we
13848 haven't considered considered assignments to variables or
13849 mathematical expressions.) Perl's power comes from manipulating text
13850 without worrying about the details, but in a programming language you
13851 MUST worry about the details. You could write a
13852 parser/lexer/interpreter in Perl, but it turns out that it wouldn't
13853 be any easier than implementing it in any other language.
13855 ----------------------------------------
13857 6) Acknowledgements
13859 A big thank you to Jacinta Richardson for suggestions and
13860 proofreading. More advanced Perl users might want to check out the
13861 free material from Perl Training Australia
13862 <http://www.perltraining.com.au/>, which she is a part of.
13864 Other contributors include Meryll Larkin.
13866 ----------------------------------------
13870 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
13871 Richter and Alice Wood, and is released under the same license as
13872 Perl itself (Artistic License or GPL, your choice). This is the
13873 license of choice to make it easy for other people to integrate your
13874 Perl code/documentation into their own projects. It is not generally
13875 used in projects unrelated to Perl.
13878 From dan@cellectivity.com Sat Jul 2 01:10:31 2005
13879 Return-Path: <dan@cellectivity.com>
13880 X-Original-To: courses@linuxchix.org
13881 Delivered-To: courses@linuxchix.org
13882 Received: from localhost (localhost [127.0.0.1])
13883 by www.linuxchix.org (Postfix) with ESMTP id 43E5E273180
13884 for <courses@linuxchix.org>; Sat, 2 Jul 2005 01:10:31 +1000 (EST)
13885 Received: from www.linuxchix.org ([127.0.0.1])
13886 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
13887 id 03596-02 for <courses@linuxchix.org>;
13888 Sat, 2 Jul 2005 01:10:31 +1000 (EST)
13889 Received: from cellectivity.com (unknown [212.18.242.163])
13890 by www.linuxchix.org (Postfix) with ESMTP id 093F727317C
13891 for <courses@linuxchix.org>; Sat, 2 Jul 2005 01:10:16 +1000 (EST)
13892 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
13893 with Microsoft SMTPSVC(6.0.3790.211); Fri, 1 Jul 2005 16:11:08 +0100
13894 Subject: [Courses] [Perl] Part 14: References
13895 From: Dan <dan@cellectivity.com>
13896 To: courses@linuxchix.org
13897 Content-Type: text/plain
13898 Date: Fri, 01 Jul 2005 16:09:47 +0100
13899 Message-Id: <1120230587.22906.11.camel@localhost.localdomain>
13901 X-Mailer: Evolution 2.0.4
13902 Content-Transfer-Encoding: 7bit
13903 X-OriginalArrivalTime: 01 Jul 2005 15:11:08.0109 (UTC)
13904 FILETIME=[1BA2AFD0:01C57E4F]
13905 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
13907 X-BeenThere: courses@linuxchix.org
13908 X-Mailman-Version: 2.1.5
13910 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
13911 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13912 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
13913 List-Archive: <http://linuxchix.org/pipermail/courses>
13914 List-Post: <mailto:courses@linuxchix.org>
13915 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
13916 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
13917 <mailto:courses-request@linuxchix.org?subject=subscribe>
13918 X-List-Received-Date: Fri, 01 Jul 2005 15:10:31 -0000
13919 Content-Length: 8845
13922 LinuxChix Perl Course Part 14: References
13925 2) Learning About Scalars - Again!
13927 4) An Array of Hashes
13928 5) Where to Find Out More
13930 7) Answers to Previous Exercises
13931 8) Acknowledgements
13935 ----------------------------------------
13939 The programs that we've been writing are fairly simple, but you may
13940 have already run up against the limitations imposed by flat lists.
13941 For example, we might want to read the password file and convert each
13942 line into a list of fields, but we have no good way to join the lists
13943 into a list of lists representing the whole file.
13945 With references we're going to solve that problem. There are lots of
13946 uses for references, including:
13947 a) creating an array of arrays, or a hashtable of arrays, or a
13948 hashtable of hashtables.
13949 b) passing several distinct arrays to a function, or returning
13950 several distinct arrays.
13951 c) creating a "function pointer", allowing us to chain
13953 d) writing our own function that can take code blocks, allowing us to
13954 extend the language (sort of).
13956 This lesson is rather long and counterintuitive, but try to stick
13957 with it to the end. References aren't as much fun as some other
13958 concepts (like s///) but they're essential to writing non-trivial
13959 programs, and in particular for object-oriented programming in Perl.
13961 ----------------------------------------
13963 2) Learning About Scalars - Again!
13965 Recall that there are four basic data types in Perl. They are:
13966 a) scalar - A single chunk of data
13967 b) array - A bunch of scalars, indexed by a number
13968 c) hash (hashtable) - A bunch of scalars, indexed by another scalar
13969 d) handle - A pointer enabling the opening of resources from the
13970 operating system (files, directories, etc.)
13972 We have seen all four types. One would think that the simplest type,
13973 scalars, would hold no more secrets from us. But there is one type of
13974 scalar that we haven't seen yet: a reference.
13976 Try running this program:
13981 print '$x = ' . "$x\n"; # Output is: bar
13982 print '${$r} = ' . "${$r}\n"; # Output is: bar
13984 As you can see, "$r" is a pointer to "$x". But in Perl it's called a
13985 reference, and it doesn't have any of the painful properties
13986 associated with pointers in C++. You can't generate a memory leak in
13987 Perl even if you try. In that sense, Perl's references are much more
13988 like Java's pointers than C++'s pointers.
13990 Because a reference is a scalar, it can be stored in an array. Which
13991 is where most of its utility comes from, as we'll see next.
13993 ----------------------------------------
13997 The following code creates a reference to an array:
13999 my @a = ('foo', 'bar', 'baz');
14001 ${$r}[1] = 'x'; # Replace "bar" with "x".
14002 $r->[2] = 'y'; # Replace "baz" with "y" (nicer syntax).
14005 Knowing this, we can create an array of arrays.
14008 for (my $i=0; $i<5; $i++) {
14009 my @sub_list = ('foo', 'bar');
14010 $main_list[$i] = \@sub_list;
14012 print "${$main_list[1]}[0]\n"; # Output is "foo"
14013 print "$main_list[1]->[0]\n"; # Shorter form of above.
14014 print "$main_list[1][0]\n"; # Shortest form of above.
14016 NOTE: "@sub_list" is declared inside the loop, so each iteration
14017 creates a new variable. If it was declared outside the loop, every
14018 array index would point to the same variable, and that probably isn't
14021 The final optimisation we can perform on the syntax is replacing the
14022 "\@sub_list" with a list in square brakets:
14024 for (my $i=0; $i<5; $i++) {
14025 $main_list[$i] = ['foo', 'bar'];
14028 The square brackets generate a reference to a list without using an
14029 intermediate temporary variable.
14031 ----------------------------------------
14033 4) An Array of Hashes
14035 As you might have guessed, we can create a reference to a hash as
14040 Just as square brackets create a reference to a list, braces create a
14041 reference to a hash. So we can create a list of hashes like this:
14043 my @list_of_hashes;
14044 for (my $i=0; $i<5; $i++) {
14045 $list_of_hashes[$i] = { 'tea'=>'coffee', 'pear'=>'lemon' };
14047 print "$list_of_hashes[1]{pear}\n"; # Output is "lemon"
14049 ----------------------------------------
14051 5) Where to Find Out More
14053 Hashes aren't as intuitive as some other aspects of Perl, but we've
14054 gotten through the basics. You'll find more uses for references as
14055 you read other people's code and documentation.
14057 If you want to dig in further, here are some good places:
14065 ----------------------------------------
14069 a) Write a Perl program that reads "/etc/passwd" and creates an array
14070 of arrays of fields such that "$passwd[$i][$j]" represents line "$i",
14071 field "$j". (The first field is the username, the second field is the
14072 password, etc.) How would you search this array for all the users who
14075 b) Change the previous program so that it uses an array of hashes
14076 instead, with each user account a hash mapping "username" to
14077 username, "uid" to UID, etc. Now how will you implement a search?
14079 c) Consider the following program:
14084 my $s = 'a scalar';
14086 my $r2 = ['a', 'reference', 'to', 'an', 'array'];
14091 what does the "ref" function do? Where do you think it's usually
14094 d) Perl has another mechanism for references, illustrated as follows:
14098 print $$bar; # output is: xyz
14100 (By the way, Perl won't let you do this if you "use strict".)
14102 This is the only reference mechanism that existed before Perl 5. Why
14103 is Perl 5's mechanism such an improvement?
14105 e) Consider the following code:
14110 sub make_uppercase {
14112 foreach my $item ( @result ) {
14113 $item = uc($item); # uc = upper-case
14118 my $transformation = \&make_uppercase;
14119 my @transformed = &$transformation('foo', 'bar');
14120 print "@transformed\n";
14122 What does the "\&" operator do?
14124 f) Consider this code:
14133 sub do_if_in_debug_mode (&) {
14138 do_if_in_debug_mode {
14139 print "Just testing.\n";
14142 Now write your own "extention" to the Perl language.
14144 ----------------------------------------
14146 7) Answers to Previous Exercises
14148 a) The first exercise was to write a function to do what one might
14151 @arr = map(reverse, @arr); # Doesn't work.
14153 and then maybe to explain why the above code doesn't work as
14156 Here's a function that will do what we want:
14158 sub scalar_reverse {
14160 foreach my $element (@result) {
14161 $element = reverse($element);
14166 As for why the code with "map" doesn't work, the reason is the
14167 difference between scalar context and list context. "reverse" in
14168 scalar context reverses the order of the characters in its input, but
14169 in list context it does something different. "map" always calls a
14170 function in list context. To override this, use the "scalar" keyword:
14172 my @arr = ('foo', 'bar');
14173 @arr = map {scalar reverse} @arr;
14174 print "@arr\n"; # Output is: oof rab
14176 b) In this declaration:
14180 The part in parentheses is called the prototype. In this case, it
14181 specifies that it is expecting three scalar parameters. Perl will
14182 complain if you provide more or fewer, or even if you provide an
14183 array with exactly three elements.
14185 Incidentally, some people prefer to write a "normal" (unlimited
14186 arguments) Perl function like this:
14190 c) The "wantarray" function returns true if its containing function
14191 was called in list context. It returns false if the function was
14192 called in scalar context, and it returns the undefined value if the
14193 function was called in void context (i.e., the return value is not
14196 ----------------------------------------
14198 8) Acknowledgements
14200 A big thank you to Jacinta Richardson for suggestions and
14201 proofreading. More advanced Perl users might want to check out the
14202 free material from Perl Training Australia
14203 <http://www.perltraining.com.au/>, which she is a part of.
14205 Other contributors include Meryll Larkin.
14207 ----------------------------------------
14211 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
14212 Richter and Alice Wood, and is released under the same license as
14213 Perl itself (Artistic License or GPL, your choice). This is the
14214 license of choice to make it easy for other people to integrate your
14215 Perl code/documentation into their own projects. It is not generally
14216 used in projects unrelated to Perl.
14219 From dan@cellectivity.com Sat Jul 9 01:26:19 2005
14220 Return-Path: <dan@cellectivity.com>
14221 X-Original-To: courses@linuxchix.org
14222 Delivered-To: courses@linuxchix.org
14223 Received: from localhost (localhost [127.0.0.1])
14224 by www.linuxchix.org (Postfix) with ESMTP id 5DCA227318B
14225 for <courses@linuxchix.org>; Sat, 9 Jul 2005 01:26:19 +1000 (EST)
14226 Received: from www.linuxchix.org ([127.0.0.1])
14227 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
14228 id 02744-04 for <courses@linuxchix.org>;
14229 Sat, 9 Jul 2005 01:26:19 +1000 (EST)
14230 Received: from cellectivity.com (unknown [212.18.242.163])
14231 by www.linuxchix.org (Postfix) with ESMTP id C9ED527318A
14232 for <courses@linuxchix.org>; Sat, 9 Jul 2005 01:26:17 +1000 (EST)
14233 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
14234 with Microsoft SMTPSVC(6.0.3790.211); Fri, 8 Jul 2005 16:27:36 +0100
14235 Subject: [Courses] [Perl] Part 15
14236 From: Dan <dan@cellectivity.com>
14237 To: courses@linuxchix.org
14238 Content-Type: text/plain
14239 Date: Fri, 08 Jul 2005 16:26:13 +0100
14240 Message-Id: <1120836373.20218.27.camel@localhost.localdomain>
14242 X-Mailer: Evolution 2.0.4
14243 Content-Transfer-Encoding: 7bit
14244 X-OriginalArrivalTime: 08 Jul 2005 15:27:36.0125 (UTC)
14245 FILETIME=[916E36D0:01C583D1]
14246 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
14248 X-BeenThere: courses@linuxchix.org
14249 X-Mailman-Version: 2.1.5
14251 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
14252 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14253 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
14254 List-Archive: <http://linuxchix.org/pipermail/courses>
14255 List-Post: <mailto:courses@linuxchix.org>
14256 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
14257 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14258 <mailto:courses-request@linuxchix.org?subject=subscribe>
14259 X-List-Received-Date: Fri, 08 Jul 2005 15:26:19 -0000
14260 Content-Length: 8132
14263 LinuxChix Perl Course Part 15: Using Modules and OOP
14268 4) Where to go from here
14270 6) Answers to Previous Exercises
14271 7) Acknowledgements
14275 ----------------------------------------
14279 Even though it seems quite long, this course just scratches the
14280 surface of Perl. Perl really does have a whole universe of
14281 functionalities and tricks, and this course is already drawing to a
14284 But before we end things, we're going to see how to use Perl modules,
14285 which (as it turns out) is closely related to object-oriented
14286 programming in Perl. We're not going to see how to WRITE a module or
14287 class; just how to USE one, because we're not yet at a level where it
14288 would be useful to write a module. (But see the further reading
14289 section if you want to write your own.)
14291 ----------------------------------------
14295 Here's an example of a Perl program that uses a module:
14299 use HTML::Entities;
14301 my $line = <STDIN>; # Read line of text.
14302 print encode_entities($line);
14304 As you might have noticed, we start by declaring that we use the
14305 "HTML::Entities" module. This handy module encodes and decodes HTML
14306 entities (e.g., replacing "<" with "<". After we declare that we
14307 use the module, we can call the functions that it contains, in this
14308 case "encode_entities".
14310 By the way, we could also have written the function call like this:
14312 HTML::Entities::encode_entities($line)
14314 As you might have noticed, the double-colon separates namespaces in
14315 Perl (just as it does in C++).
14317 All Perl modules include documentation, so you can read more about
14318 "HTML::Entities" like this:
14320 perldoc HTML::Entities
14322 ----------------------------------------
14326 Now that we've seen basic module use, we're going to look at a module
14327 that contains a class.
14331 use LWP::UserAgent;
14333 my $browser = LWP::UserAgent->new();
14335 my $response = $browser->get('http://www.w3.org/');
14337 if ($response->is_success) {
14338 print $response->content;
14341 die $response->status_line;
14344 This program retrieves the W3C's home page. Let's go through it
14347 The first thing we do is declare that we're going to use the
14348 "LWP::UserAgent" module. This module contains a user-agent (often
14349 called a web browser) which retrieves web pages.
14351 Next we create a new object with "LWP::UserAgent->new()". A few
14352 things to note here:
14353 a) The class name is "LWP::UserAgent", just like the package name.
14354 This is a byproduct of the way OOP is built into Perl.
14355 b) The object is created with "new". This is NOT a requirement (the
14356 constructor could be called anything), but it is common.
14357 c) The object is in fact a reference. This means that if you write
14358 "$object1=$object2", both variables will refer to the same object.
14360 When we call "$browser->get()", the user-agent contacts the web
14361 server and returns its response. The response is actually a
14362 "HTTP::Response", but notice that we don't have to import that module
14363 or even use its name.
14365 Finally, we check whether the request was successful and output the
14366 result. Note that "$response->is_success", "$response->content" and
14367 "$response->status_line" are method (function) calls just like
14368 "$browser->get", even though there are no parentheses.
14370 ----------------------------------------
14372 4) Where to go from here
14374 Whatever you want to do, there's probably a Perl module that does a
14375 good portion of it. Search the Comprehensive Perl Archive Network
14376 (CPAN: http://www.cpan.org/ ).
14378 Many of the modules on CPAN are actually already installed on your
14379 system, so check before going to the trouble of installing a module.
14380 The easiest way to see if a module is installed is to try to "use"
14381 it, or try to read its documentation (as explained next).
14383 To read the documentation of an (installed) module, use "perldoc":
14385 perldoc LWP::UserAgent
14387 (You can also use "man"; both work. There Is More Than One Way To Do
14390 To see all the modules installed on your system, you might want to
14391 see the fourth item under:
14395 However, that doesn't work for me :-/ so I do this:
14399 One last thing: while you could download and install modules from
14400 CPAN, the most popular Perl modules are probably available via your
14401 Linux distro's package management system (such as "rpm" or "apt"). Do
14402 a quick search there first.
14404 ----------------------------------------
14408 a) Consider this program:
14411 use LWP::UserAgent;
14413 my $ua = LWP::UserAgent->new();
14414 print ref($ua) . "\n";
14416 What does "ref" return when passed an object?
14418 b) Consider this code:
14424 $INPUT_RECORD_SEPARATOR = ':';
14427 What does "use English" accomplish?
14429 For more information, you can consult:
14433 ----------------------------------------
14435 6) Answers to Previous Exercises
14437 a) This Perl program stores the password file in a list of lists:
14444 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
14445 while ( <PASSWD> ) {
14446 push @list, [ split /:/ ];
14450 Here is one way to search such a list:
14452 print "Enter a username and I'll give you a UID.\n";
14454 while ( defined( my $username = <STDIN> ) ) {
14456 my @matches = grep { $_->[0] eq $username } @list;
14458 print "User $username has UID $matches[0][2]\n";
14461 print "Sorry: I don't know that user.\n";
14465 b) Here's a way of storing the password file as an array of hashes:
14472 open PASSWD, "< /etc/passwd" or die "Couldn't open file.";
14473 while ( <PASSWD> ) {
14474 my @fields = split /:/;
14475 push @list, { username=>$fields[0], password=>$fields[1],
14476 uid=>$fields[2], gid=>$fields[3],
14477 name=>$fields[4], home=>$fields[5],
14478 shell=>$fields[6] };
14482 It can be searched like this:
14484 print "Enter a username and I'll give you a UID.\n";
14486 while ( defined( my $username = <STDIN> ) ) {
14488 my @matches = grep { $_->{username} eq $username } @list;
14490 print "User $username has UID $matches[0]{uid}\n";
14493 print "Sorry: I don't know that user.\n";
14497 c) The "ref" function allows you to determine whether a scalar is a
14498 reference, and even the data type to which it refers.
14500 d) Perl 4 included a mechanism called "symbolic" references, which
14501 allowed one variable to access another using its name. This method is
14502 not recommended because it can make your life even more miserable
14503 than normal pointers can.
14505 e) The "\&" operator creates a reference to a function.
14507 f) Larry Wall provides us with some very interesting syntax:
14510 my($try,$catch) = @_;
14517 sub catch (&) { $_[0] }
14522 /phooey/ and print "unphooey\n";
14525 Briefly, this example takes advantage of Perl's "$@" variable, which
14526 stores the latest "exception" (a "die" inside an "eval").
14528 ----------------------------------------
14530 7) Acknowledgements
14532 A big thank you to Jacinta Richardson for suggestions and
14533 proofreading. More advanced Perl users might want to check out the
14534 free material from Perl Training Australia
14535 <http://www.perltraining.com.au/>, which she is a part of.
14537 Other contributors include Meryll Larkin.
14539 ----------------------------------------
14543 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
14544 Richter and Alice Wood, and is released under the same license as
14545 Perl itself (Artistic License or GPL, your choice). This is the
14546 license of choice to make it easy for other people to integrate your
14547 Perl code/documentation into their own projects. It is not generally
14548 used in projects unrelated to Perl.
14551 From dan@cellectivity.com Sat Jul 16 01:09:31 2005
14552 Return-Path: <dan@cellectivity.com>
14553 X-Original-To: courses@linuxchix.org
14554 Delivered-To: courses@linuxchix.org
14555 Received: from localhost (localhost [127.0.0.1])
14556 by www.linuxchix.org (Postfix) with ESMTP id 000EF273192
14557 for <courses@linuxchix.org>; Sat, 16 Jul 2005 01:09:30 +1000 (EST)
14558 Received: from www.linuxchix.org ([127.0.0.1])
14559 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
14560 id 17035-06 for <courses@linuxchix.org>;
14561 Sat, 16 Jul 2005 01:09:30 +1000 (EST)
14562 Received: from cellectivity.com (unknown [212.18.242.163])
14563 by www.linuxchix.org (Postfix) with ESMTP id DF9AE27318A
14564 for <courses@linuxchix.org>; Sat, 16 Jul 2005 01:09:29 +1000 (EST)
14565 Received: from miami.cellectivity.com ([192.168.100.44]) by cellectivity.com
14566 with Microsoft SMTPSVC(6.0.3790.211);
14567 Fri, 15 Jul 2005 16:10:49 +0100
14568 Subject: [Courses] [Perl] Part 16: The End
14569 From: Dan <dan@cellectivity.com>
14570 To: courses@linuxchix.org
14571 Content-Type: text/plain
14572 Date: Fri, 15 Jul 2005 16:09:24 +0100
14573 Message-Id: <1121440164.8445.53.camel@localhost.localdomain>
14575 X-Mailer: Evolution 2.0.4
14576 Content-Transfer-Encoding: 7bit
14577 X-OriginalArrivalTime: 15 Jul 2005 15:10:49.0906 (UTC)
14578 FILETIME=[6291A120:01C5894F]
14579 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
14581 X-BeenThere: courses@linuxchix.org
14582 X-Mailman-Version: 2.1.5
14584 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
14585 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14586 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
14587 List-Archive: <http://linuxchix.org/pipermail/courses>
14588 List-Post: <mailto:courses@linuxchix.org>
14589 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
14590 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14591 <mailto:courses-request@linuxchix.org?subject=subscribe>
14592 X-List-Received-Date: Fri, 15 Jul 2005 15:09:31 -0000
14593 Content-Length: 4655
14596 LinuxChix Perl Course Part 16: The End
14600 3) Use Strict and Warnings
14602 5) Platform Independent Shell Scripts
14604 7) Don't Use Perl for Everything
14605 8) Answers to Previous Exercises
14606 9) Acknowledgements
14610 ----------------------------------------
14614 Congratulations: you have finished the Perl course. You are now ready
14615 to write your own small Perl scripts or, better yet, dig into someone
14616 else's Perl code to learn more.
14618 In this "epilog" I give my final words of advice.
14620 ----------------------------------------
14624 Perl truly has almost endless tools and tricks, and even the most
14625 seemingly banal parts of the language have surprises. Browse through
14626 the avalanche of documentation a little at a time. The complete list
14629 [you@localhost]$ man perl
14631 As Larry Wall said in an interview
14632 <http://www.linuxjournal.com/article/3394>, "I've certainly tried to
14633 put a universe of ideas into Perl, with some amount of success."
14635 Don't reinvent the wheel! RTFM, and then search CPAN.
14637 ----------------------------------------
14639 3) Use Strict and Warnings
14641 I said this at the beginning of the course, and I'm repeating it now:
14642 begin all of your Perl programs like this:
14647 Those two lines will save you hours of debugging.
14649 ----------------------------------------
14653 The standard rules on making maintainable code apply all the more to
14654 Perl, because Perl doesn't force you to write readable code. In
14655 particular, think about how you'll scratch your head a year from now
14656 wondering what that big regular expression means. You'd better put in
14659 ----------------------------------------
14661 5) Platform Independent Shell Scripts
14663 Consider using Perl as a platform independent shell scripting
14664 language. Most shell scripts depend on a host of other programs (like
14665 "grep" and "sed") whose exact implementations vary from one machine
14666 to another - not to mention the fact that they're not available on
14667 Windows. Perl is platform independent, with certain obvious
14668 exceptions such as the "chmod" function.
14670 Perl even allows you to think in shell script mode, as this mini Perl
14671 script demonstrates:
14676 unlink "some_unwanted_file";
14678 ----------------------------------------
14682 Perl hackers insist that There Is More Than One Way To Do It
14683 (TIMTOWDTI, pronounced Tim-Towdty). Look at all the possibilities.
14685 But also remember to use the best tool for every job. For example,
14686 don't do heavy XML processing with m// and s///. Instead, use Perl's
14687 own XML libraries, which are introduced at
14688 http://www.xml.com/pub/a/2000/04/05/feature/
14690 ----------------------------------------
14692 7) Don't Use Perl for Everything
14694 Finally, extend TIMTOWDTI by considering possibilities beyond Perl.
14695 Perl is the best tool for a lot of things, but it's not the best tool
14696 for everything. If you don't need Perl's text processing power, then
14697 consider other languages like Python and Ruby. If you're mostly
14698 manipulating XML, consider using XSLT with a small Perl front-end.
14700 ----------------------------------------
14702 8) Answers to Previous Exercises
14704 a) The "ref" function, when called on an object, returns the object's
14707 In Perl a class is identified by the package (namespace) in which the
14708 it is defined. Every module must also have its own package, so a
14709 class is package and a module is a package. To find out more about
14714 b) The "English" module provides more intuitive names for weird Perl
14715 variables like "$(" (group ID) or "$|" (automatic output flushing).
14716 For more information on the "English" module:
14720 For more information on those funny Perl variables:
14724 ----------------------------------------
14726 9) Acknowledgements
14728 A big thank you to Jacinta Richardson for suggestions and
14729 proofreading. More advanced Perl users might want to check out the
14730 free material from Perl Training Australia
14731 <http://www.perltraining.com.au/>, which she is a part of.
14733 Other contributors include Meryll Larkin.
14735 ----------------------------------------
14739 This course (i.e., all parts of it) is copyright 2003-2005 by Dan
14740 Richter and Alice Wood, and is released under the same license as
14741 Perl itself (Artistic License or GPL, your choice). This is the
14742 license of choice to make it easy for other people to integrate your
14743 Perl code/documentation into their own projects. It is not generally
14744 used in projects unrelated to Perl.
14747 From basic_meo@hotmail.com Fri Dec 17 23:22:58 2004
14748 Return-Path: <basic_meo@hotmail.com>
14749 X-Original-To: courses@linuxchix.org
14750 Delivered-To: courses@linuxchix.org
14751 Received: from localhost (localhost [127.0.0.1])
14752 by www.linuxchix.org (Postfix) with ESMTP id B106E272FC8
14753 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:22:58 +1100 (EST)
14754 Received: from www.linuxchix.org ([127.0.0.1])
14755 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
14756 id 29450-05 for <courses@linuxchix.org>;
14757 Fri, 17 Dec 2004 23:22:58 +1100 (EST)
14758 Received: from hotmail.com (bay101-f9.bay101.hotmail.com [64.4.56.19])
14759 by www.linuxchix.org (Postfix) with ESMTP id 222C0272FC3
14760 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:22:57 +1100 (EST)
14761 Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
14762 Fri, 17 Dec 2004 04:23:03 -0800
14763 Message-ID: <BAY101-F938731A3F4DB6A85EA9FD8DAF0@phx.gbl>
14764 Received: from 80.81.115.131 by by101fd.bay101.hotmail.msn.com with HTTP;
14765 Fri, 17 Dec 2004 12:22:03 GMT
14766 X-Originating-IP: [80.81.115.131]
14767 X-Originating-Email: [basic_meo@hotmail.com]
14768 X-Sender: basic_meo@hotmail.com
14769 In-Reply-To: <200407062259.20028.suzo@spin.net.au>
14770 From: "nameBasic One" <basic_meo@hotmail.com>
14771 To: suzo@spin.net.au, courses@linuxchix.org
14772 Date: Fri, 17 Dec 2004 12:22:03 +0000
14774 Content-Type: text/plain; format=flowed
14775 X-OriginalArrivalTime: 17 Dec 2004 12:23:03.0471 (UTC)
14776 FILETIME=[27C20BF0:01C4E433]
14777 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
14779 Subject: [Courses] contolling parallel port with perl
14780 X-BeenThere: courses@linuxchix.org
14781 X-Mailman-Version: 2.1.5
14783 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
14784 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14785 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
14786 List-Archive: <http://linuxchix.org/pipermail/courses>
14787 List-Post: <mailto:courses@linuxchix.org>
14788 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
14789 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14790 <mailto:courses-request@linuxchix.org?subject=subscribe>
14791 X-List-Received-Date: Fri, 17 Dec 2004 12:22:59 -0000
14792 Content-Length: 200
14797 do anybody have an idea how to control parralell port with perl command?
14799 the basic thing I want to do is send a command to device in parallel por and
14800 be able to read the respond...
14804 From lawgon@thenilgiris.com Fri Dec 17 23:46:49 2004
14805 Return-Path: <lawgon@thenilgiris.com>
14806 X-Original-To: courses@linuxchix.org
14807 Delivered-To: courses@linuxchix.org
14808 Received: from localhost (localhost [127.0.0.1])
14809 by www.linuxchix.org (Postfix) with ESMTP id A111E272FC8
14810 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:46:49 +1100 (EST)
14811 Received: from www.linuxchix.org ([127.0.0.1])
14812 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
14813 id 29883-10 for <courses@linuxchix.org>;
14814 Fri, 17 Dec 2004 23:46:49 +1100 (EST)
14815 Received: from sharp.noc24x7.com (unknown [69.56.128.138])
14816 by www.linuxchix.org (Postfix) with ESMTP id 37255272FC1
14817 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:46:49 +1100 (EST)
14818 Received: from [61.3.110.149] (helo=thenilgiris.com)
14819 by sharp.noc24x7.com with esmtpa (Exim 4.43)
14820 id 1CfHVV-0004ET-Lt; Fri, 17 Dec 2004 18:16:54 +0530
14821 Received: from localhost (localhost [[UNIX: localhost]])
14822 by thenilgiris.com (8.11.2/8.11.2) id iBHCedr31652;
14823 Fri, 17 Dec 2004 18:10:39 +0530
14824 Content-Type: text/plain;
14825 charset="iso-8859-1"
14826 From: Kenneth Gonsalves <lawgon@thenilgiris.com>
14827 To: "nameBasic One" <basic_meo@hotmail.com>, suzo@spin.net.au,
14828 courses@linuxchix.org
14829 Subject: Re: [Courses] contolling parallel port with perl
14830 Date: Fri, 17 Dec 2004 18:10:39 +0530
14831 X-Mailer: KMail [version 1.2]
14832 References: <BAY101-F938731A3F4DB6A85EA9FD8DAF0@phx.gbl>
14833 In-Reply-To: <BAY101-F938731A3F4DB6A85EA9FD8DAF0@phx.gbl>
14835 Message-Id: <0412171810391L.06834@thenilgiris.com>
14836 X-AntiAbuse: This header was added to track abuse,
14837 please include it with any abuse report
14838 X-AntiAbuse: Primary Hostname - sharp.noc24x7.com
14839 X-AntiAbuse: Original Domain - linuxchix.org
14840 X-AntiAbuse: Originator/Caller UID/GID - [0 0] / [47 12]
14841 X-AntiAbuse: Sender Address Domain - thenilgiris.com
14845 Content-Transfer-Encoding: quoted-printable
14846 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
14848 X-BeenThere: courses@linuxchix.org
14849 X-Mailman-Version: 2.1.5
14851 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
14852 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14853 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
14854 List-Archive: <http://linuxchix.org/pipermail/courses>
14855 List-Post: <mailto:courses@linuxchix.org>
14856 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
14857 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14858 <mailto:courses-request@linuxchix.org?subject=subscribe>
14859 X-List-Received-Date: Fri, 17 Dec 2004 12:46:49 -0000
14860 Content-Length: 374
14863 On Friday 17 December 2004 05:52 pm, nameBasic One wrote:
14865 > do anybody have an idea how to control parralell port with perl command=
14868 > the basic thing I want to do is send a command to device in parallel po=
14870 > and be able to read the respond...
14873 http://search.cpan.org/~scott/Device-ParallelPort-1.00/lib/Device/Paralle=
14879 From suzo@spin.net.au Fri Dec 17 23:42:54 2004
14880 Return-Path: <suzo@spin.net.au>
14881 X-Original-To: courses@linuxchix.org
14882 Delivered-To: courses@linuxchix.org
14883 Received: from localhost (localhost [127.0.0.1])
14884 by www.linuxchix.org (Postfix) with ESMTP id 91F36272FC8
14885 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:42:54 +1100 (EST)
14886 Received: from www.linuxchix.org ([127.0.0.1])
14887 by localhost (nest [127.0.0.1]) (amavisd-new, port 10024) with SMTP
14888 id 29756-10 for <courses@linuxchix.org>;
14889 Fri, 17 Dec 2004 23:42:54 +1100 (EST)
14890 Received: from proxy.syd (comcen.comcen.com.au [203.23.236.66])
14891 by www.linuxchix.org (Postfix) with ESMTP id 477E9272FC1
14892 for <courses@linuxchix.org>; Fri, 17 Dec 2004 23:42:54 +1100 (EST)
14893 Received: from [127.0.0.1] (202-172-117-64.cpe.nsw-1.comcen.com.au
14895 by proxy.syd (8.11.6/8.11.6) with ESMTP id iBHCgxI68563;
14896 Fri, 17 Dec 2004 23:42:59 +1100 (EST)
14897 (envelope-from suzo@spin.net.au)
14898 Message-ID: <41C2D44C.2050804@spin.net.au>
14899 Date: Fri, 17 Dec 2004 23:42:52 +1100
14900 From: Sue Stones <suzo@spin.net.au>
14901 User-Agent: Mozilla Thunderbird 0.9 (Windows/20041103)
14902 X-Accept-Language: en-us, en
14904 To: nameBasic One <basic_meo@hotmail.com>
14905 References: <BAY101-F938731A3F4DB6A85EA9FD8DAF0@phx.gbl>
14906 In-Reply-To: <BAY101-F938731A3F4DB6A85EA9FD8DAF0@phx.gbl>
14907 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
14908 Content-Transfer-Encoding: 7bit
14909 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at linuxchix.org
14910 Cc: courses@linuxchix.org
14911 Subject: [Courses] Re: contolling parallel port with perl
14912 X-BeenThere: courses@linuxchix.org
14913 X-Mailman-Version: 2.1.5
14915 List-Id: List for courses run by LinuxChix volunteers <courses.linuxchix.org>
14916 List-Unsubscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14917 <mailto:courses-request@linuxchix.org?subject=unsubscribe>
14918 List-Archive: <http://linuxchix.org/pipermail/courses>
14919 List-Post: <mailto:courses@linuxchix.org>
14920 List-Help: <mailto:courses-request@linuxchix.org?subject=help>
14921 List-Subscribe: <http://mailman.linuxchix.org/mailman/listinfo/courses>,
14922 <mailto:courses-request@linuxchix.org?subject=subscribe>
14923 X-List-Received-Date: Fri, 17 Dec 2004 12:42:54 -0000
14924 Content-Length: 994
14930 > do anybody have an idea how to control parralell port with perl command?
14932 > the basic thing I want to do is send a command to device in parallel por
14933 > and be able to read the respond...
14936 G'day nameBasic One,
14938 I personally can't help you, and in fact would like to point out that
14939 this is not the appropriate Linuxchix list for this kind of question.
14940 There are a number of others that would most likely be able to help you.
14942 You could repost the question on any of the following lists and I think
14943 you ar likely to get some help on any of them. They are
14945 Newchix for people who are new to linux
14946 Techtalk for technical questions
14947 Programming for programming specific discussions
14949 Are you a memeber of any of these lists? If not, I would highly
14950 recomend them. I suggest you repost your question to your pick of these
14951 lists, where I think that you will get some help. (I think my
14952 prefeerenc would be programming since it concerns perl commands).