100% found this document useful (3 votes)
22 views

Programming ERLANG Software for a Concurrent World 1st Edition Joe Armstrong pdf download

The document provides information about the book 'Programming Erlang: Software for a Concurrent World' by Joe Armstrong, which focuses on writing concurrent applications using Erlang. It includes details on various programming concepts and techniques relevant to Erlang, as well as links to additional resources and related books. The content emphasizes the importance of concurrency in programming to model real-world interactions effectively.

Uploaded by

czeckquass3o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
22 views

Programming ERLANG Software for a Concurrent World 1st Edition Joe Armstrong pdf download

The document provides information about the book 'Programming Erlang: Software for a Concurrent World' by Joe Armstrong, which focuses on writing concurrent applications using Erlang. It includes details on various programming concepts and techniques relevant to Erlang, as well as links to additional resources and related books. The content emphasizes the importance of concurrency in programming to model real-world interactions effectively.

Uploaded by

czeckquass3o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Programming ERLANG Software for a Concurrent

World 1st Edition Joe Armstrong download

https://ebookname.com/product/programming-erlang-software-for-a-
concurrent-world-1st-edition-joe-armstrong/

Get Instant Ebook Downloads – Browse at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Erlang programming 1st ed Edition Francesco Cesarini

https://ebookname.com/product/erlang-programming-1st-ed-edition-
francesco-cesarini/

Programming Elixir Functional Concurrent Pragmatic Fun


1st Edition Dave Thomas

https://ebookname.com/product/programming-elixir-functional-
concurrent-pragmatic-fun-1st-edition-dave-thomas/

Concurrent and Real Time Programming in Ada 3rd Edition


Alan Burns

https://ebookname.com/product/concurrent-and-real-time-
programming-in-ada-3rd-edition-alan-burns/

Research Methods in Sociolinguistics A Practical Guide


1st Edition Janet Holmes

https://ebookname.com/product/research-methods-in-
sociolinguistics-a-practical-guide-1st-edition-janet-holmes/
Gustav Klimt Best of 2nd ed Edition Jane Rogoyska

https://ebookname.com/product/gustav-klimt-best-of-2nd-ed-
edition-jane-rogoyska/

The Chemistry Of Radical Polymerization 2nd Edition


Graeme Moad

https://ebookname.com/product/the-chemistry-of-radical-
polymerization-2nd-edition-graeme-moad/

Infrared Spectra of Rubbers Plastics and Thermoplastic


Elastomers 4th Edition Yvonne Davies

https://ebookname.com/product/infrared-spectra-of-rubbers-
plastics-and-thermoplastic-elastomers-4th-edition-yvonne-davies/

Zora Neale Hurston and American Literary Culture 1st


Edition M. Genevieve West

https://ebookname.com/product/zora-neale-hurston-and-american-
literary-culture-1st-edition-m-genevieve-west/

Hitler s Crusade Bolshevism and the Myth of the


International Jewish Conspiracy Waddington

https://ebookname.com/product/hitler-s-crusade-bolshevism-and-
the-myth-of-the-international-jewish-conspiracy-waddington/
Consumer behavior buying having and being Ninth
Edition. Edition Michael R. Solomon

https://ebookname.com/product/consumer-behavior-buying-having-
and-being-ninth-edition-edition-michael-r-solomon/
The world is parallel.
If we want to write programs that behave as other objects behave in
the real world, then these programs will have a concurrent structure.
Use a language that was designed for writing concurrent applications,
and development becomes a lot easier.
Erlang programs model how we think and interact.
Joe Armstrong
Programming Erlang
Software for a Concurrent World

Joe Armstrong

The Pragmatic Bookshelf


Raleigh, North Carolina Dallas, Texas
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g
device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.

Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at

http://www.pragmaticprogrammer.com

Copyright © 2007 armstrongonsoftware.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmit-


ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher.

Printed in the United States of America.

ISBN-10: 1-9343560-0-X
ISBN-13: 978-1-934356-00-5
Printed on acid-free paper with 50% recycled, 15% post-consumer content.
P1.1 printing, July, 2007
Version: 2007-7-17
Contents
1 Begin 12
1.1 Road Map . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.2 Begin Again . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.3 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . 17

2 Getting Started 18
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Installing Erlang . . . . . . . . . . . . . . . . . . . . . . 21
2.3 The Code in This Book . . . . . . . . . . . . . . . . . . . 23
2.4 Starting the Shell . . . . . . . . . . . . . . . . . . . . . . 24
2.5 Simple Integer Arithmetic . . . . . . . . . . . . . . . . . 25
2.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.7 Floating-Point Numbers . . . . . . . . . . . . . . . . . . 32
2.8 Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.9 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.10 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.11 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.12 Pattern Matching Again . . . . . . . . . . . . . . . . . . 41

3 Sequential Programming 43
3.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.2 Back to Shopping . . . . . . . . . . . . . . . . . . . . . . 49
3.3 Functions with the Same Name and Different Arity . . 52
3.4 Funs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.5 Simple List Processing . . . . . . . . . . . . . . . . . . . 58
3.6 List Comprehensions . . . . . . . . . . . . . . . . . . . . 61
3.7 Arithmetic Expressions . . . . . . . . . . . . . . . . . . 64
3.8 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.9 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.10 case and if Expressions . . . . . . . . . . . . . . . . . . 72
3.11 Building Lists in Natural Order . . . . . . . . . . . . . . 73
3.12 Accumulators . . . . . . . . . . . . . . . . . . . . . . . . 74
CONTENTS 6

4 Exceptions 76
4.1 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.2 Raising an Exception . . . . . . . . . . . . . . . . . . . . 77
4.3 try...catch . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.4 catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.5 Improving Error Messages . . . . . . . . . . . . . . . . . 82
4.6 Programming Style with try...catch . . . . . . . . . . . . 82
4.7 Catching Every Possible Exception . . . . . . . . . . . . 83
4.8 Old- and New-Style Exception Handling . . . . . . . . . 84
4.9 Stack Traces . . . . . . . . . . . . . . . . . . . . . . . . . 84

5 Advanced Sequential Programming 86


5.1 BIFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.2 Binaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.3 The Bit Syntax . . . . . . . . . . . . . . . . . . . . . . . 89
5.4 Miscellaneous Short Topics . . . . . . . . . . . . . . . . 98

6 Compiling and Running Your Program 118


6.1 Starting and Stopping the Erlang Shell . . . . . . . . . 118
6.2 Modifying the Development Environment . . . . . . . . 119
6.3 Different Ways to Run Your Program . . . . . . . . . . . 122
6.4 Automating Compilation with Makefiles . . . . . . . . . 127
6.5 Command Editing in the Erlang Shell . . . . . . . . . . 130
6.6 Getting Out of Trouble . . . . . . . . . . . . . . . . . . . 131
6.7 When Things Go Wrong . . . . . . . . . . . . . . . . . . 131
6.8 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.9 Tweaking the Environment . . . . . . . . . . . . . . . . 135
6.10 The Crash Dump . . . . . . . . . . . . . . . . . . . . . . 136

7 Concurrency 137

8 Concurrent Programming 141


8.1 The Concurrency Primitives . . . . . . . . . . . . . . . . 142
8.2 A Simple Example . . . . . . . . . . . . . . . . . . . . . 143
8.3 Client-Server—An Introduction . . . . . . . . . . . . . . 144
8.4 How Long Does It Take to Create a Process? . . . . . . 148
8.5 Receive with a Timeout . . . . . . . . . . . . . . . . . . 150
8.6 Selective Receive . . . . . . . . . . . . . . . . . . . . . . 153
8.7 Registered Processes . . . . . . . . . . . . . . . . . . . . 154
8.8 How Do We Write a Concurrent Program? . . . . . . . . 156
8.9 A Word About Tail Recursion . . . . . . . . . . . . . . . 156
8.10 Spawning with MFAs . . . . . . . . . . . . . . . . . . . . 157
8.11 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
CONTENTS 7

9 Errors in Concurrent Programs 159


9.1 Linking Processes . . . . . . . . . . . . . . . . . . . . . . 159
9.2 An on_exit Handler . . . . . . . . . . . . . . . . . . . . . 160
9.3 Remote Handling of Errors . . . . . . . . . . . . . . . . 162
9.4 The Details of Error Handling . . . . . . . . . . . . . . . 162
9.5 Error Handling Primitives . . . . . . . . . . . . . . . . . 170
9.6 Sets of Linked Processes . . . . . . . . . . . . . . . . . . 172
9.7 Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
9.8 A Keep-Alive Process . . . . . . . . . . . . . . . . . . . . 173

10 Distributed Programming 175


10.1 The Name Server . . . . . . . . . . . . . . . . . . . . . . 177
10.2 The Distribution Primitives . . . . . . . . . . . . . . . . 182
10.3 Libraries for Distributed Programming . . . . . . . . . 185
10.4 The Cookie Protection System . . . . . . . . . . . . . . . 186
10.5 Socket-Based Distribution . . . . . . . . . . . . . . . . . 187

11 IRC Lite 191


11.1 Message Sequence Diagrams . . . . . . . . . . . . . . . 193
11.2 The User Interface . . . . . . . . . . . . . . . . . . . . . 194
11.3 Client-Side Software . . . . . . . . . . . . . . . . . . . . 195
11.4 Server-Side Software . . . . . . . . . . . . . . . . . . . . 199
11.5 Running the Application . . . . . . . . . . . . . . . . . . 203
11.6 The Chat Program Source Code . . . . . . . . . . . . . . 204
11.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

12 Interfacing Techniques 212


12.1 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12.2 Interfacing an External C Program . . . . . . . . . . . . 214
12.3 open_port . . . . . . . . . . . . . . . . . . . . . . . . . . 220
12.4 Linked-in Drivers . . . . . . . . . . . . . . . . . . . . . . 221
12.5 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

13 Programming with Files 226


13.1 Organization of the Libraries . . . . . . . . . . . . . . . 226
13.2 The Different Ways of Reading a File . . . . . . . . . . . 227
13.3 The Different Ways of Writing to a File . . . . . . . . . . 235
13.4 Directory Operations . . . . . . . . . . . . . . . . . . . . 239
13.5 Finding Information About a File . . . . . . . . . . . . . 240
13.6 Copying and Deleting Files . . . . . . . . . . . . . . . . 241
13.7 Bits and Pieces . . . . . . . . . . . . . . . . . . . . . . . 241
13.8 A Find Utility . . . . . . . . . . . . . . . . . . . . . . . . 242
CONTENTS 8

14 Programming with Sockets 245


14.1 Using TCP . . . . . . . . . . . . . . . . . . . . . . . . . . 246
14.2 Control Issues . . . . . . . . . . . . . . . . . . . . . . . . 255
14.3 Where Did That Connection Come From? . . . . . . . . 258
14.4 Error Handling with Sockets . . . . . . . . . . . . . . . 259
14.5 UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
14.6 Broadcasting to Multiple Machines . . . . . . . . . . . 263
14.7 A SHOUTcast Server . . . . . . . . . . . . . . . . . . . . 265
14.8 Digging Deeper . . . . . . . . . . . . . . . . . . . . . . . 272

15 ETS and DETS: Large Data Storage Mechanisms 273


15.1 Basic Operations on Tables . . . . . . . . . . . . . . . . 274
15.2 Types of Table . . . . . . . . . . . . . . . . . . . . . . . . 275
15.3 ETS Table Efficiency Considerations . . . . . . . . . . . 276
15.4 Creating an ETS Table . . . . . . . . . . . . . . . . . . . 277
15.5 Example Programs with ETS . . . . . . . . . . . . . . . 279
15.6 DETS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
15.7 What Haven’t We Talked About? . . . . . . . . . . . . . 287
15.8 Code Listings . . . . . . . . . . . . . . . . . . . . . . . . 288

16 OTP Introduction 291


16.1 The Road to the Generic Server . . . . . . . . . . . . . . 292
16.2 Getting Started with gen_server . . . . . . . . . . . . . 301
16.3 The gen_server Callback Structure . . . . . . . . . . . . 305
16.4 Code and Templates . . . . . . . . . . . . . . . . . . . . 309
16.5 Digging Deeper . . . . . . . . . . . . . . . . . . . . . . . 312

17 Mnesia: The Erlang Database 313


17.1 Database Queries . . . . . . . . . . . . . . . . . . . . . . 313
17.2 Adding and Removing Data in the Database . . . . . . 317
17.3 Mnesia Transactions . . . . . . . . . . . . . . . . . . . . 319
17.4 Storing Complex Data in Tables . . . . . . . . . . . . . 323
17.5 Table Types and Location . . . . . . . . . . . . . . . . . 325
17.6 Creating the Initial Database . . . . . . . . . . . . . . . 328
17.7 The Table Viewer . . . . . . . . . . . . . . . . . . . . . . 329
17.8 Digging Deeper . . . . . . . . . . . . . . . . . . . . . . . 329
17.9 Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
CONTENTS 9

18 Making a System with OTP 335


18.1 Generic Event Handling . . . . . . . . . . . . . . . . . . 336
18.2 The Error Logger . . . . . . . . . . . . . . . . . . . . . . 339
18.3 Alarm Management . . . . . . . . . . . . . . . . . . . . . 346
18.4 The Application Servers . . . . . . . . . . . . . . . . . . 348
18.5 The Supervision Tree . . . . . . . . . . . . . . . . . . . . 351
18.6 Starting the System . . . . . . . . . . . . . . . . . . . . 354
18.7 The Application . . . . . . . . . . . . . . . . . . . . . . . 358
18.8 File System Organization . . . . . . . . . . . . . . . . . 360
18.9 The Application Monitor . . . . . . . . . . . . . . . . . . 361
18.10 Digging Deeper . . . . . . . . . . . . . . . . . . . . . . . 361
18.11 How Did We Make That Prime? . . . . . . . . . . . . . . 363
19 Multicore Prelude 365
20 Programming Multicore CPUs 367
20.1 How to Make Programs Run Efficiently on a Multicore CPU 368
20.2 Parallelizing Sequential Code . . . . . . . . . . . . . . . 372
20.3 Small Messages, Big Computations . . . . . . . . . . . 375
20.4 mapreduce and Indexing Our Disk . . . . . . . . . . . . 379
20.5 Growing Into the Future . . . . . . . . . . . . . . . . . . 389
A Documenting Our Program 390
A.1 Erlang Type Notation . . . . . . . . . . . . . . . . . . . . 391
A.2 Tools That Use Types . . . . . . . . . . . . . . . . . . . . 394
B Erlang on Microsoft Windows 396
B.1 Erlang . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
B.2 Fetch and Install MinGW . . . . . . . . . . . . . . . . . 396
B.3 Fetch and Install MSYS . . . . . . . . . . . . . . . . . . 397
B.4 Install the MSYS Developer Toolkit (Optional) . . . . . 397
B.5 Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
C Resources 399
C.1 Online Documentation . . . . . . . . . . . . . . . . . . . 399
C.2 Books and Theses . . . . . . . . . . . . . . . . . . . . . 400
C.3 Link Collections . . . . . . . . . . . . . . . . . . . . . . . 400
C.4 Blogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
C.5 Forums, Online Communities, and Social Sites . . . . 401
C.6 Conferences . . . . . . . . . . . . . . . . . . . . . . . . . 401
C.7 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
C.8 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . 402
CONTENTS 10

D A Socket Application 403


D.1 An Example . . . . . . . . . . . . . . . . . . . . . . . . . 403
D.2 How lib_chan Works . . . . . . . . . . . . . . . . . . . . 406
D.3 The lib_chan Code . . . . . . . . . . . . . . . . . . . . . 409

E Miscellaneous 419
E.1 Analysis and Profiling Tools . . . . . . . . . . . . . . . . 419
E.2 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 422
E.3 Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
E.4 Dynamic Code Loading . . . . . . . . . . . . . . . . . . . 435

F Module and Function Reference 439


F.1 Module: application . . . . . . . . . . . . . . . . . . . . . 439
F.2 Module: base64 . . . . . . . . . . . . . . . . . . . . . . . 440
F.3 Module: beam_lib . . . . . . . . . . . . . . . . . . . . . . 441
F.4 Module: c . . . . . . . . . . . . . . . . . . . . . . . . . . 441
F.5 Module: calendar . . . . . . . . . . . . . . . . . . . . . . 443
F.6 Module: code . . . . . . . . . . . . . . . . . . . . . . . . 444
F.7 Module: dets . . . . . . . . . . . . . . . . . . . . . . . . . 445
F.8 Module: dict . . . . . . . . . . . . . . . . . . . . . . . . . 448
F.9 Module: digraph . . . . . . . . . . . . . . . . . . . . . . . 449
F.10 Module: digraph_utils . . . . . . . . . . . . . . . . . . . 450
F.11 Module: disk_log . . . . . . . . . . . . . . . . . . . . . . 451
F.12 Module: epp . . . . . . . . . . . . . . . . . . . . . . . . . 452
F.13 Module: erl_eval . . . . . . . . . . . . . . . . . . . . . . . 453
F.14 Module: erl_parse . . . . . . . . . . . . . . . . . . . . . . 453
F.15 Module: erl_pp . . . . . . . . . . . . . . . . . . . . . . . 454
F.16 Module: erl_scan . . . . . . . . . . . . . . . . . . . . . . 454
F.17 Module: erl_tar . . . . . . . . . . . . . . . . . . . . . . . 454
F.18 Module: erlang . . . . . . . . . . . . . . . . . . . . . . . 455
F.19 Module: error_handler . . . . . . . . . . . . . . . . . . . 464
F.20 Module: error_logger . . . . . . . . . . . . . . . . . . . . 464
F.21 Module: ets . . . . . . . . . . . . . . . . . . . . . . . . . 465
F.22 Module: file . . . . . . . . . . . . . . . . . . . . . . . . . 468
F.23 Module: file_sorter . . . . . . . . . . . . . . . . . . . . . 470
F.24 Module: filelib . . . . . . . . . . . . . . . . . . . . . . . . 471
F.25 Module: filename . . . . . . . . . . . . . . . . . . . . . . 471
F.26 Module: gb_sets . . . . . . . . . . . . . . . . . . . . . . . 472
F.27 Module: gb_trees . . . . . . . . . . . . . . . . . . . . . . 474
F.28 Module: gen_event . . . . . . . . . . . . . . . . . . . . . 475
F.29 Module: gen_fsm . . . . . . . . . . . . . . . . . . . . . . 476
CONTENTS 11

F.30 Module: gen_sctp . . . . . . . . . . . . . . . . . . . . . . 477


F.31 Module: gen_server . . . . . . . . . . . . . . . . . . . . . 478
F.32 Module: gen_tcp . . . . . . . . . . . . . . . . . . . . . . . 478
F.33 Module: gen_udp . . . . . . . . . . . . . . . . . . . . . . 479
F.34 Module: global . . . . . . . . . . . . . . . . . . . . . . . . 479
F.35 Module: inet . . . . . . . . . . . . . . . . . . . . . . . . . 480
F.36 Module: init . . . . . . . . . . . . . . . . . . . . . . . . . 481
F.37 Module: io . . . . . . . . . . . . . . . . . . . . . . . . . . 481
F.38 Module: io_lib . . . . . . . . . . . . . . . . . . . . . . . . 482
F.39 Module: lib . . . . . . . . . . . . . . . . . . . . . . . . . . 483
F.40 Module: lists . . . . . . . . . . . . . . . . . . . . . . . . . 483
F.41 Module: math . . . . . . . . . . . . . . . . . . . . . . . . 487
F.42 Module: ms_transform . . . . . . . . . . . . . . . . . . . 487
F.43 Module: net_adm . . . . . . . . . . . . . . . . . . . . . . 487
F.44 Module: net_kernel . . . . . . . . . . . . . . . . . . . . . 488
F.45 Module: os . . . . . . . . . . . . . . . . . . . . . . . . . . 488
F.46 Module: proc_lib . . . . . . . . . . . . . . . . . . . . . . 489
F.47 Module: qlc . . . . . . . . . . . . . . . . . . . . . . . . . 489
F.48 Module: queue . . . . . . . . . . . . . . . . . . . . . . . 490
F.49 Module: random . . . . . . . . . . . . . . . . . . . . . . 491
F.50 Module: regexp . . . . . . . . . . . . . . . . . . . . . . . 492
F.51 Module: rpc . . . . . . . . . . . . . . . . . . . . . . . . . 492
F.52 Module: seq_trace . . . . . . . . . . . . . . . . . . . . . . 494
F.53 Module: sets . . . . . . . . . . . . . . . . . . . . . . . . . 494
F.54 Module: shell . . . . . . . . . . . . . . . . . . . . . . . . 495
F.55 Module: slave . . . . . . . . . . . . . . . . . . . . . . . . 495
F.56 Module: sofs . . . . . . . . . . . . . . . . . . . . . . . . . 496
F.57 Module: string . . . . . . . . . . . . . . . . . . . . . . . . 500
F.58 Module: supervisor . . . . . . . . . . . . . . . . . . . . . 501
F.59 Module: sys . . . . . . . . . . . . . . . . . . . . . . . . . 501
F.60 Module: timer . . . . . . . . . . . . . . . . . . . . . . . . 502
F.61 Module: win32reg . . . . . . . . . . . . . . . . . . . . . . 503
F.62 Module: zip . . . . . . . . . . . . . . . . . . . . . . . . . 504
F.63 Module: zlib . . . . . . . . . . . . . . . . . . . . . . . . . 504

Index 507
Chapter 1

Begin
Oh no! Not another programming language! Do I have to learn yet another
one? Aren’t there enough already?
I can understand your reaction. There are loads of programming lan-
guages, so why should you learn another?
Here are five reasons why you should learn Erlang:
• You want to write programs that run faster when you run them on
a multicore computer.
• You want to write fault-tolerant applications that can be modified
without taking them out of service.
• You’ve heard about “functional programming” and you’re wonder-
ing whether the techniques really work.
• You want to use a language that has been battle tested in real
large-scale industrial products that has great libraries and an
active user community.
• You don’t want to wear your fingers out by typing lots of lines of
code.
Can we do these things? In Section 20.3, Running SMP Erlang, on
page 376, we’ll look at some programs that have linear speed-ups when
we run them on a thirty-two-core computer. In Chapter 18, Making a
System with OTP, we’ll look at how to make highly reliable systems that
have been in round-the-clock operation for years. In Section 16.1, The
Road to the Generic Server, on page 292, we’ll talk about techniques for
writing servers where the software can be upgraded without taking the
server out of service.
R OAD M AP 13

In many places we’ll be extolling the virtues of functional programming.


Functional programming forbids code with side effects. Side effects and
concurrency don’t mix. You can have sequential code with side effects,
or you can have code and concurrency that is free from side effects.
You have to choose. There is no middle way.
Erlang is a language where concurrency belongs to the programming
language and not the operating system. Erlang makes parallel program-
ming easy by modeling the world as sets of parallel processes that can
interact only by exchanging messages. In the Erlang world, there are
parallel processes but no locks, no synchronized methods, and no pos-
sibility of shared memory corruption, since there is no shared memory.
Erlang programs can be made from thousands to millions of extremely
lightweight processes that can run on a single processor, can run on a
multicore processor, or can run on a network of processors.

1.1 Road Map


• Chapter 2, Getting Started, on page 18 is a quick “jump in and
swim around” chapter.
• Chapter 3, Sequential Programming, on page 43 is the first of two
chapters on sequential programming. It introduces the ideas of
pattern matching and of nondestructive assignments.
• Chapter 4, Exceptions, on page 76 is about exception handling. No
program is error free. This chapter is about detecting and handling
errors in sequential Erlang programs.
• Chapter 5, Advanced Sequential Programming, on page 86 is the
second chapter on sequential Erlang programming. It takes up
some advanced topics and fills in the remaining details of sequen-
tial programming.
• Chapter 6, Compiling and Running Your Program, on page 118
talks about the different ways of compiling and running your pro-
gram.
• In Chapter 7, Concurrency, on page 137, we change gears. This
is a nontechnical chapter. What are the ideas behind our way of
programming? How do we view the world?
• Chapter 8, Concurrent Programming, on page 141 is about concur-
rency. How do we create parallel processes in Erlang? How do pro-
cesses communicate? How fast can we create parallel processes?
R OAD M AP 14

• Chapter 9, Errors in Concurrent Programs, on page 159 talks about


errors in parallel programs. What happens when a process fails?
How can we detect process failure, and what can we do about it?
• Chapter 10, Distributed Programming, on page 175 takes up dis-
tributed programming. Here we’ll write several small distributed
programs and show how to run them on a cluster of Erlang nodes
or on free-standing hosts using a form of socket-based distribu-
tion.
• Chapter 11, IRC Lite, on page 191 is a pure application chapter.
We tie together the themes of concurrency and socket-based distri-
bution with our first nontrivial application: a mini IRC-like client
and server program.
• Chapter 12, Interfacing Techniques, on page 212 is all about inter-
facing Erlang to foreign-language code.
• Chapter 13, Programming with Files, on page 226 has numerous
examples of programming with files.
• Chapter 14, Programming with Sockets, on page 245 shows you
how to program with sockets. We’ll look at how to build sequential
and parallel servers in Erlang. We finish this chapter with the sec-
ond sizable application: a SHOUTcast server. This is a streaming
media server, which can be used to stream MP3 data using the
SHOUTcast protocol.
• Chapter 15, ETS and DETS: Large Data Storage Mechanisms, on
page 273 describes the low-level modules ets and dets. ets is a
module for very fast, destructive, in-memory hash table opera-
tions, and dets is designed for low-level disk storage.
• Chapter 16, OTP Introduction, on page 291 is an introduction to
OTP. OTP is a set of Erlang libraries and operating procedures
for building industrial-scale applications in Erlang. This chap-
ter introduces the idea of a behavior (a central concept in OTP).
Using behaviors, we can concentrate on the functional behavior
of a component, while allowing the behavior framework to solve
the nonfunctional aspects of the problem. The framework might,
for example, take care of making the application fault tolerant or
scalable, whereas the behavioral callback concentrates on the spe-
cific aspects of the problem. The chapter starts with a general dis-
cussion on how to build your own behaviors and then moves to
describing the gen_server behavior that is part of the Erlang stan-
dard libraries.
R OAD M AP 15

• Chapter 17, Mnesia: The Erlang Database, on page 313 talks about
the Erlang database management system (DBMS) Mnesia. Mnesia
is an integrated DBMS with extremely fast, soft, real-time
response times. It can be configured to replicate its data over sev-
eral physically separated nodes to provide fault-tolerant operation.
• Chapter 18, Making a System with OTP, on page 335 is the second
of the OTP chapters. It deals with the practical aspects of sewing
together an OTP application. Real applications have a lot of small
messy details. They must be started and stopped in a consistent
manner. If they crash or if subcomponents crash, they must be
restarted. We need error logs so that if they do crash, we can figure
out what happened after the event. This chapter has all the nitty-
gritty details of making a fully blown OTP application.
• Chapter 19, Multicore Prelude, on page 365 is a short introduction
to why Erlang is suited for programming multicore computers. We
talk in general terms about shared memory and message passing
concurrency and why we strongly believe that languages with no
mutable state and concurrency are ideally suited to programming
multicore computers.
• Chapter 20, Programming Multicore CPUs, on page 367 is about
programming multicore computers. We talk about the techniques
for ensuring that an Erlang program will run efficiently on multi-
core computers. We introduce a number of abstractions for speed-
ing up sequential programs on multicore computers. Finally we
perform some measurements and develop our third major pro-
gram, a full-text search engine. To write this, we first implement
a function called mapreduce—this is a higher-order function for
parallelizing a computation over a set of processing elements.
• Appendix A, on page 390, describes the type system used to doc-
ument Erlang functions.
• Appendix B, on page 396, describes how to set up Erlang on the
Windows operating system (and how to configure emacs on all
operating systems).
• Appendix C, on page 399, has a catalog of Erlang resources.
• Appendix D, on page 403, describes lib_chan, which is a library for
programming socket-based distribution.
B EGIN A GAIN 16

• Appendix E, on page 419, looks at techniques for analyzing, pro-


filing, debugging, and tracing your code.
• Appendix F, on page 439, has one-line summaries of the most
used modules in the Erlang standard libraries.

1.2 Begin Again


Once upon a time a programmer came across a book describing a funny
programming language. It had an unfamiliar syntax, equal didn’t mean
equals, and variables weren’t allowed to vary. Worse, it wasn’t even
object-oriented. The programs were, well, different....
Not only were the programs different, but the whole approach to pro-
gramming was different. The author kept on and on about concurrency
and distribution and fault tolerance and about a method of programming
called concurrency-oriented programming—whatever that might mean.
But some of the examples looked like fun. That evening the programmer
looked at the example chat program. It was pretty small and easy to
understand, even if the syntax was a bit strange. Surely it couldn’t be
that easy.
The basic program was simple, and with a few more lines of code, file
sharing and encrypted conversations became possible. The programmer
started typing....

What’s This All About?


It’s about concurrency. It’s about distribution. It’s about fault toler-
ance. It’s about functional programming. It’s about programming a dis-
tributed concurrent system without locks and mutexes but using only
pure message passing. It’s about speeding up your programs on multi-
core CPUs. It’s about writing distributed applications that allow people
to interact with each other. It’s about design methods and behaviors
for writing fault-tolerant and distributed systems. It’s about modeling
concurrency and mapping those models onto computer programs, a
process I call concurrency-oriented programming.
I had fun writing this book. I hope you have fun reading it.
Now go read the book, write some code, and have fun.
A CKNOWLEDGMENTS 17

1.3 Acknowledgments
Many people have helped in the preparation of this book, and I’d like to
thank them all here.
First, Dave Thomas, my editor: Dave has been teaching me to write
and subjecting me to a barrage of never-ending questions. Why this?
Why that? When I started the book, Dave said my writing style was like
“standing on a rock preaching.” He said, “I want you to talk to people,
not preach.” The book is better for it. Thanks, Dave.
Next, I’ve had a little committee of language experts at my back. They
helped me decide what to leave out. They also helped me clarify some
of the bits that are difficult to explain. Thanks here (in no particular
order) to Björn Gustavsson, Robert Virding, Kostis Sagonas, Kenneth
Lundin, Richard Carlsson, and Ulf Wiger.
Thanks also to Claes Vikström who provided valuable advice on Mnesia,
to Rickard Green on SMP Erlang, and to Hans Nilsson for the stemming
algorithm used in the text-indexing program.
Sean Hinde and Ulf Wiger helped me understand how to use various
OTP internals, and Serge Aleynikov explained active sockets to me so
that I could understand.
Helen Taylor (my wife) has proofread several chapters and provided
hundreds of cups of tea at appropriate moments. What’s more, she put
up with my rather obsessive behavior for the last seven months. Thanks
also to Thomas and Claire; and thanks to Bach and Handel, Zorro and
Daisy, and Doris, who have helped me stay sane, have purred when
stroked, and have gotten me to the right addresses.
Finally, to all the readers of the beta book who filled in errata requests:
I have cursed you and praised you. When the first beta went out, I was
unprepared for the entire book to be read in two days and for you to
shred every page with your comments. But the process has resulted in
a much better book than I had imagined. When (as happened several
times) dozens of people said, “I don’t understand this page,” then I was
forced to think again and rewrite the material concerned. Thanks for
your help, everybody.

Joe Armstrong
May 2007
Chapter 2

Getting Started
2.1 Overview
As with every learning experience, you’ll pass through a number of
stages on your way to Erlang mastery. Let’s look at the stages we cover
in this book and the things you’ll experience along the way.

Stage 1: I’m Not Sure...


As a beginner, you’ll learn how to start the system, run commands in
the shell, compile simple programs, and become familiar with Erlang.
(Erlang is a small language, so this won’t take you long.)
Let’s break this down into smaller chunks. As a beginner, you’ll do the
following:
• Make sure you have a working Erlang system on your computer.
• Learn to start and stop the Erlang shell.
• Discover how to enter expressions into the shell, evaluate them,
and understand the results.
• See how to create and modify programs using your favorite text
editor.
• Experiment with compiling and running your programs in the
shell.

Stage 2: I’m Comfortable with Erlang


By now you’ll have a working knowledge of the language. If you run
into language problems, you’ll have the background to make sense of
Chapter 5, Advanced Sequential Programming, on page 86.
O VERVIEW 19

At this stage you’ll be familiar with Erlang, so we’ll move on to more


interesting topics:
• You’ll pick up more advanced uses of the shell. The shell can do a
lot more than we let on when you were first learning it. (For exam-
ple, you can recall and edit previous expressions. This is covered
in Section 6.5, Command Editing in the Erlang Shell, on page 130.)
• You’ll start learning the libraries (called modules in Erlang). Most
of the programs I write can be written using five modules: lists, io,
file, dict, and gen_tcp; therefore, we’ll be using these modules a lot
throughout the book.
• As your programs get bigger, you’ll need to learn how to automate
compiling and running them. The tool of choice for this is make.
We’ll see how to control the process by writing a makefile. This is
covered in Section 6.4, Automating Compilation with Makefiles, on
page 127.
• The bigger world of Erlang programming uses an extensive library
collection called OTP.1 As you gain experience with Erlang, you’ll
find that knowing OTP will save you lots of time. After all, why
reinvent the wheel if someone has already written the functional-
ity you need? We’ll learn the major OTP behaviors, in particular
gen_server. This is covered in Section 16.2, Getting Started with
gen_server, on page 301.
• One of the main uses of Erlang is writing distributed programs,
so now is the time to start experimenting. You can start with the
examples in Chapter 10, Distributed Programming, on page 175,
and you can extend them in any way you want.

Stage 2.5: I May Learn Some Optional Stuff


You don’t have to read every chapter in this book the first time through.
Unlike most of the languages you have probably met before, Erlang is
a concurrent programming language—this makes it particularly suited
for writing distributed programs and for programming modern multi-
core and SMP2 computers. Most Erlang programs will just run faster
when run on a multicore or SMP machine.
Erlang programming involves using a programming paradigm that I call
concurrency-oriented programming (COP).

1. Open Telecom Platform.


2. Symmetric multiprocessing.
O VERVIEW 20

When you use COP, you break down problems and identify the natural
concurrency in their solutions. This is an essential first step in writing
any concurrent program.

Stage 3: I’m an Erlang Master


By now you’ve mastered the language and can write some useful dis-
tributed programs. But to achieve true mastery, you need to learn even
more:
• Mnesia. The Erlang distribution comes complete with a built-in
fast, replicated database called Mnesia. It was originally designed
for telecom applications where performance and fault tolerance
are essential. Today it is used for a wide range of nontelecom appli-
cations.
• Interfacing to code written in other programming languages, and
using linked-in drivers. This is covered in Section 12.4, Linked-in
Drivers, on page 221.
• Full use of the OTP behaviors-building supervision trees, start
scripts, and so on. This is covered in Chapter 18, Making a System
with OTP, on page 335.
• How to run and optimize your programs for a multicore computer.
This is covered in Chapter 20, Programming Multicore CPUs, on
page 367.

The Most Important Lesson


There’s one rule you need to remember throughout this book: program-
ming is fun. And I personally think programming distributed applica-
tions such as chat programs or instant messaging applications is a
lot more fun than programming conventional sequential applications.
What you can do on one computer is limited, but what you can do
with networks of computers becomes unlimited. Erlang provides an
ideal environment for experimenting with networked applications and
for building production-quality systems.
To help you get started with this, I’ve mixed some real-world applica-
tions in among the technical chapters. You should be able to take these
applications as starting points for your own experiments. Take them,
modify them, and deploy them in ways that I hadn’t imagined, and I’ll
be very happy.
I NSTALLING E RLANG 21

2.2 Installing Erlang


Before you can do anything, you have to make sure you have a func-
tioning version of Erlang on your system. Go to a command prompt,
and type erl:
$ erl
Erlang (BEAM) emulator version 5.5.2 [source] ... [kernel-poll:false]

Eshell V5.5.2 (abort with ^G)


1>

On a Windows system, the command erl works only if you have installed
Erlang and changed the PATH environment variable to refer to the pro-
gram. Assuming you’ve installed the program in the standard way,
you’ll invoke Erlang through the Start > All Programs > Erlang OTP
menu. In Appendix B, on page 396, I’ll describe how I’ve rigged Erlang
to run with MinGW and MSYS.
Note: I’ll show the banner (the bit that says “Erlang (BEAM) ... (abort
with ∧G)”) only occasionally. This information is useful only if you want
to report a bug. I’m just showing it here so you won’t get worried if you
see it and wonder what it is. I’ll leave it out in most of the examples
unless it’s particularly relevant.
If you see the shell banner, then Erlang is installed on your system.
Exit from it (press Ctrl+G, followed by the letter Q, and then hit Enter
or Return).3 Now you can skip ahead to Section 2.3, The Code in This
Book, on page 23.
If instead you get an error saying erl is an unknown command, you’ll
need to install Erlang on your box. And that means you’ll need to make
a decision—do you want to use a prebuilt binary distribution, use a
packaged distribution (on OS X), build Erlang from the sources, or use
the Comprehensive Erlang Archive Network (CEAN)?

Binary Distributions
Binary distributions of Erlang are available for Windows and for Linux-
based operating systems. The instructions for installing a binary sys-
tem are highly system dependent. So, we’ll go through these system by
system.

3. Or give the command q() in the shell.


I NSTALLING E RLANG 22

Windows
You’ll find a list of the releases at http://www.erlang.org/download.html.
Choose the entry for the latest version, and click the link for the Win-
dows binary—this points to a Windows executable. Click the link, and
follow the instructions. This is a standard Windows install, so you
shouldn’t have any problems.

Linux
Binary packages exist for Debian-based systems. On a Debian-based
system, issue the following command:
> apt-get install erlang

Installing on Mac OS X
As a Mac user, you can install a prebuilt version of Erlang using the
MacPorts system, or you can build Erlang from source. Using MacPorts
is marginally easier, and it will handle updates over time. However,
MacPorts can also be somewhat behind the times when it comes to
Erlang releases. During the initial writing up this book, for example,
the MacPorts version of Erlang was two releases behind the then cur-
rent version. For this reason, I recommend you just bite the bullet and
install Erlang from source, as described in the next section. To do this,
you’ll need to make sure you have the developer tools installed (they’re
on the DVD of software that came with your machine).

Building Erlang from Source


The alternative to a binary installation is to build Erlang from the
sources. There is no particular advantage in doing this for Windows
systems since each new release comes complete with Windows binaries
and all the sources. But for Mac and Linux platforms, there can be
some delay between the release of a new Erlang distribution and the
availability of a binary installation package. For any Unix-like OS, the
installation instructions are the same:
1. Fetch the latest Erlang sources.4 The source will be in a file with
a name such as otp_src_R11B-4.tar.gz (this file contains the fourth
maintenance release of version 11 of Erlang).

4. From http://www.erlang.org/download.html .
T HE C ODE IN T HIS B OOK 23

2. Unpack, configure, make, and install as follows:


$ tar -xzf otp_src_R11B-4.tar.gz
$ cd otp_src_R11B-4
$ ./configure
$ make
$ sudo make install

Note: You can use the command ./configure - -help to review the available
configuration options before building the system.

Use CEAN
The Comprehensive Erlang Archive Network (CEAN) is an attempt to
gather all the major Erlang applications in one place with a common
installer. The advantage of using CEAN is that it manages not only
the basic Erlang system but a large number of packages written in
Erlang. This means that as well as being able to keep your basic Erlang
installation up-to-date, you’ll be able to maintain your packages as well.
CEAN has precompiled binaries for a large number of operating systems
and processor architectures. To install a system using CEAN, go to
http://cean.process-one.net/download/, and follow the instructions. (Note
that some readers have reported that CEAN might not install the Erlang
compiler. If this happens to you, then start the Erlang shell and give the
command cean:install(compiler). This will install the compiler.)

2.3 The Code in This Book


Most of the code snippets we show come from full-length, running
examples, which you can download.5 To help you find your way, if a
code listing in this book can be found in the download, there’ll be a bar
above the snippet (just like the one here):
Download shop1.erl

-module(shop1).
-export([total/1]).

total([{What, N}|T]) -> shop:cost(What) * N + total(T);


total([]) -> 0.

This bar contains the path to the code within the download. If you’re
reading the PDF version of this book and your PDF viewer supports
hyperlinks, you can click the bar, and the code should appear in a
browser window.

5. From http://pragmaticprogrammer.com/titles/jaerlang/code.html.
S TAR TING THE S HELL 24

2.4 Starting the Shell


Now let’s get started. We can interact with Erlang using an interactive
tool called the shell. Once we’ve started the shell, we can type expres-
sions, and the shell will display their values.
If you’ve installed Erlang on your system (as described in Section 2.2,
Installing Erlang, on page 21), then the Erlang shell, erl, will also be
installed. To run it, open a conventional operating system command
shell (cmd on Windows or a shell such as bash on Unix-based systems).
At the command prompt, start the Erlang shell by typing erl:
Ê $ erl
Erlang (BEAM) emulator version 5.5.1 [source] [async-threads:0] [hipe]

Eshell V5.5.1 (abort with ^G)


Ë 1> % I'm going to enter some expressions in the shell ..
Ì 1> 20 + 30.
Í 50
Î 2>

Let’s look at what we just did:


Ê This is the Unix command to start the Erlang shell. The shell
responds with a banner telling you which version of Erlang you
are running.
Ë The shell printed the prompt 1>, and then we typed a comment.
The percent (%) character indicates the start of a comment. All
the text from the percent sign to the end of line is treated as a
comment and is ignored by the shell and the Erlang compiler.
Ì The shell repeated the prompt 1> since we hadn’t entered a com-
plete command. At this point we entered the expression 20 + 30,
followed by a period and a carriage return. (Beginners often for-
get to enter the period. Without it, Erlang won’t know that we’ve
finished our expression, and we won’t see the result displayed.)
Í The shell evaluated the expression and printed the result (50, in
this case).
Î The shell printed out another prompt, this time for command
number 2 (because the command number increases each time a
new command is entered).
Have you tried running the shell on your system? If not, please stop and
try it now. If you just read the text without typing in the commands, you
might think that you understand what is happening, but you will not
S IMPLE I NTEGER A RITHMETIC 25

have transferred this knowledge from your brain to your fingertips—


programming is not a spectator sport. Just like any form of athletics,
you have to practice a lot.
Enter the expressions in the examples exactly as they appear in the
text, and then try experimenting with the examples and changing them
a bit. If they don’t work, stop and ask yourself what went wrong. Even
an experienced Erlang programmer will spend a lot of time interacting
with the shell.
As you get more experienced, you’ll learn that the shell is a really pow-
erful tool. Previous shell commands can be recalled (with Ctrl+P and
Ctrl+N) and edited (with emacs-like editing commands). This is covered
in Section 6.5, Command Editing in the Erlang Shell, on page 130. Best
of all, when you start writing distributed programs, you will find that
you can attach a shell to a running Erlang system on a different Erlang
node in a cluster or even make an secure shell (ssh) connection directly
to an Erlang system running on a remote computer. Using this, you can
interact with any program on any node in a system of Erlang nodes.
Warning: You can’t type everything you read in this book into the shell.
In particular, you can’t type the code that’s listed in the Erlang program
files into the shell. The syntactic forms in an .erl file are not expressions
and are not understood by the shell. The shell can evaluate only Erlang
expressions and doesn’t understand anything else. In particular, you
can’t type module annotations into the shell; these are things that start
with a hyphen (such as -module, -export, and so on).
The remainder of this chapter is in the form of a number of short dia-
logues with the Erlang shell. A lot of the time I won’t explain all the
details of what is going on, since this would interrupt the flow of the
text. In Section 5.4, Miscellaneous Short Topics, on page 98, I’ll fill in
the details.

2.5 Simple Integer Arithmetic


Let’s evaluate some arithmetic expressions:
1> 2 + 3 * 4.
14
2> (2 + 3) * 4.
20

Important: You’ll see that this dialogue starts at command number 1


(that is the shell printed, 1>). This means we have started a new Erlang
Another Random Document on
Scribd Without Any Related Topics
hatching the eggs while I am with you, and we will stay near enough
for her to see how fast you learn.”
You can imagine how excited the young Robins were then. They
talked so much that day that not one of them took a nap, and if
their mother had not insisted upon it, they would not have quieted
down at sunset.
Early the next morning their parents helped them to the ground.
First they tumbled, fluttered, and sprawled to the porch roof below
the nest. Then when they had rested, they tumbled, fluttered, and
sprawled to the tops of the sweetbriar bushes underneath. There
they clung until after breakfast, while their father hunted for them
and their mother sat on the eggs above. If they had not been taught
to mind, it would have been much harder. As it was, when their
parents said, “Flutter your wings! Get ready! Fly!” they did the very
best they could at once. And that is exactly the way children must
do if they wish to grow strong and help themselves.
There never were such plump, cheerful, and obedient little Robins as
these. Their father had them stay in the lower branches of the fir
tree, within sight of the nest, and the mother watched them while
he was hunting, and called down comforting things to them. When
they had tumbles in trying to fly, she would say: “Never mind! Pick
yourselves up! Robins must tumble before they can fly. After awhile,
when I have finished hatching these eggs, you can come right up to
this window ledge and see the babies.”
Then the little Robins would try harder than ever, for they were
already proud of the babies to be hatched, since they had helped
keep the eggs warm.
Sometimes Silvertip would stroll around the corner of the house, and
Mrs. Robin would be so scared that she could hardly scream “Cat!”
Yet she always managed to do it in some way, and all the other
Robins would help her. Then the Lady, who was almost always
writing or sewing at the sitting-room window, within sight of the
nest, would drop her work and run out the nearest door, pick up
Silvertip, and carry him inside. There he would stand, with his nose
pressed against the screen and his tail switching angrily.
The Lady seemed to understand Robins. When they only cried
“Trouble!” she did not move, knowing it was something she could
not help, but when they cried, “Cat! Cat!” she always hurried out.
Sometimes, though, it was the Gentleman who came, and
sometimes the Little Boy. Mrs. Robin often said that she was sure
she could never raise children so well in any other place as here, in
spite of Silvertip’s being around.
Every day the young Robins were larger and stronger, and their tail-
feathers were better grown. When at last the joyful time came for
the two babies to chip the shell, every one of the four children
managed to get up to the window ledge to see them. It was a hard
trip, and they had to try and try again, and rest between times. They
were not all there at once, but oh, it was a happy, happy time!
The mother told the babies how their big brothers and sisters had
helped hatch them, and the father told the mother how beautifully
she had managed everything. Then the mother told him how
faithfully he had worked, and they both told the older children how
proud they were of them. Everybody said lovely things to everybody
else, and the best part of it was that all these lovely things were
true.
The babies were too little to talk much, but they stretched their
necks up lovingly and sleepily to all the family, and acted as though
they really understood how many people had been loving and
working for them, even before they were hatched.
THE SPARROWS INSIDE THE EAVES

O NE does not like to say such things, but the English Sparrows
were very disagreeable people. And they are very disagreeable
people. Also, they always have been, and probably always will be,
very disagreeable people. They were the first birds to make trouble
among neighbors anywhere around the big house. If it had not been
that the Gentleman who lived there was so very tender-hearted,
their nests would probably have been poked down with poles long
before the eggs could have been laid in them. When Boys came
around with little rifles and ugly looking bags slung over their
shoulders, they were always ordered away and told that the
Gentleman would have no shooting near his house.
It is not strange then that the woodbine was full of Sparrows’ nests,
and that many of the evergreens also bore them in their top
branches. One had even been tucked in behind a conductor pipe,
and their owners hunted and argued and fussed all over the place.
There was just one way in which the English Sparrows were not
cared for like other birds around the big house. Silvertip was allowed
to eat all that he could catch. And you may be very sure that no
Robin ever called “Cat!” when he was ready to spring upon a
Sparrow.
“It may be wrong,” said one Robin mother, “but I cannot do it. I
remember too well how they have robbed my nests and quarrelled
with my friends. I say that they must care for their own children.
And if they do not—well, so much the better for Silvertip!”
You see that the birds were not angry at Silvertip for trying to eat
them. It was all to be expected, as they knew very well. It was not
pleasant, but it had to be, just as Worms and Flies had to expect to
be eaten, unless they were clever enough to keep out of the way of
birds. Only the quickest and strongest could live, so of course all the
young ones tried hard to become quick and strong.
When Miss Sparrow, from the nest behind the conductor pipe, was
old enough to marry, she had many lovers, and that was quite
natural. She was a plump and trim-looking bird, and pretty, too, if
one came close enough to her. Her feathers were gray and brown,
with a little white and black in places. Her bill was black, and her
feet were brown. She was very careful to keep clean, and although
she had to hunt food in the mud of the street, she bathed often in
fine dust and kept her wings and tail well up. Her lovers were
dressed in the same colors, but with more decided markings.
Her parents were very clever to think of building where they did;
and because they had such a large nest and so near the eaves of
the house, they were much looked up to by the other Sparrows.
They were very proud of their home, and especially on days when
the water running down the pipe made a sweet guggle-guggle-
guggling sound. Sparrows like noise, you know, and this always
amused the children and kept them quiet on rainy days.
All the young Sparrows who were not already in love, and a few who
were, began to court Miss Sparrow as soon as it was known that she
cared to marry. This was partly on her own account, and partly
because of her distinguished family.
Some birds would have waited for their suitors to speak first about
marriage. Miss Sparrow did not. The Sparrows are not very well
bred. “Of course I am going to marry,” she said. “I am only waiting
to make up my mind whom I will choose.”
They flocked around her as she fed in the dust of the road, all
talking at once in their harsh voices. When a team passed by, and
that was not often, they flew or hopped aside at the last minute.
When they settled down again there was always a squabble to see
who should be next to Miss Sparrow. Her lovers fought with each
other over choice seeds, but they let Miss Sparrow have everything
she wished. She always seemed very cross when her lovers were
around (as well as most of the time when they were not), and often
scolded and pecked at them. Sometimes one who was not brave,
and would not stand pain, flew away and began courting somebody
else.
After a while she had driven away so many that only two were left.
She flew at these, striking first one and then the other, until, brave
as they were, one went away. Then she turned to the suitor who
was left with a sweet smile. “I will marry you,” she said.
His wings were lame from her fighting him, his head smarted where
she had picked at it, and two or three small feathers were missing
from his breast. Miss Sparrow was certainly a strong bird, and he
knew that anybody who wanted her would have to stand just what
he had stood. He would have preferred to court as the Goldfinches
and Wrens do, by singing to their sweethearts, but that could not
be. In the first place, he could not sing, and in the second place she
would not have taken him until she had beaten him anyway. It
would have been more fun for him to fight some of the other birds
and let the winner have her, yet that could not be done either. If he
wanted to marry, he had to marry an English Sparrow, and if he
wanted to marry an English Sparrow he had to go about it in her
way. It would have been just the same if he had courted her sister
or her cousin.
The truth is that, although the Sparrow husbands swagger and brag
a great deal and act as though they owned everything in sight, there
is not one whose wife does not order him around. Miss Sparrow
would not have taken him if she had not made sure that she could
whip him.
“What do I need of a husband,” she said, “unless he will mind me?
And when I feel crosser than usual I want somebody always near
and at home, where I can treat him as I choose. That is what I care
for in a home.”
“Now,” she said, “if you are to be my husband, I will show you
where we are to build.”
Mr. Sparrow flew meekly along after her. You would be meek with
lame wings, a sore head, and three feathers off from your breast.
She led the way to the front west porch, where the syringa shoots
made a little hedge around it and a tall fir tree made good perching
places beside it.
“Where are we going to build?” asked Mr. Sparrow. He saw plenty of
good window ledges and places which would do for Robins and
Phœbes and other birds who plaster their nests. Yet he did not see a
single corner or big crack where a Sparrow’s nest could be made to
hold together.
“I will show you,” answered Mrs. Sparrow. She perched on the top of
a porch column and looked up at a small round hole nearly over her
head. It was the place where a conductor pipe had once run through
the cornice. Now the pipe had been taken away and the opening
was left. She gave an upward spring and flutter and went straight up
through the hole. “Come up!” she cried in the most good-natured
way. “Come up! This is the best place I ever saw. Our nest will be all
hidden, and no large bird or Squirrel can possibly get in. The rain
can never fall on it, and on cold days we shall be warm and snug.”
She did not ask him what he thought of it, and he did not expect her
to. So he just said, “It is a most unusual place.”
“That is what I think,” she replied. “Very unusual, and I would not
build in the woodbine like some Sparrows. No, indeed! One who has
been brought up in style beside a water-pipe, as I was, could never
come down to woodbine. It should not be expected.”
“I’m sure it was not, my dear,” said her husband.
“Very well,” said she. “Since you like this place so much, we may as
well call it settled and keep still about it until we are ready to build.”
Mr. Sparrow had not said that he liked it, yet he knew better than to
tell her so. If he did, she might leave him even now for one of her
other lovers. He really dreaded getting out through that hole, and let
her go while he watched her. She went head first, clinging to the
rough edges of the hole with both feet, let go with one, hung and
twisted around until she was headed right, then dropped and flew
away. Mr. Sparrow did the same, but he did not like it.
After a while they began nest-building, and all the straws, sticks, and
feathers had to be dragged up through the little round doorway to
the nest. Mrs. Sparrow did most of the arranging, while her husband
flew in and out more than a hundred times a day. She was a worker.
Any bird will tell you that. Still, you know, there are different ways of
working. Some of the people who do the most work make the least
fuss. Mrs. Sparrow was not one of these. When she did a thing, she
wanted everybody to know it, and since her building-place was
hidden she talked all the more to Mr. Sparrow.
“I am going to have a large nest,” she said. “So bring plenty of stuff.
Bring good things, too,” she added. “You have brought two straws
already that were really dirty, and this last stick isn’t fit to use. I will
push it back into a corner.”
Mr. Sparrow would have liked to tell her what hard work his was,
and ask her to use things he brought, even if they were not quite
what she wanted. He was too wise for this, however, so he flew out
and pitched into another Sparrow who was getting straws for his
wife. He tried to steal his straw, and they fought back and forth until
their wives came to see what was the matter and began fighting
also. When they stopped at last, the straw had been carried away by
a Robin, so neither had it. But they had had a lovely, loud, rough
fight, and Sparrows like that even better than straw, so they all felt
good-natured again.
Twice Mrs. Sparrow decided to move her nest a little this way or a
little that, and such a litter as she made when doing it! Some of the
best sticks fell down through the doorway, and the Lady swept them
off the porch. Then Mrs. Sparrow scolded her. She was not afraid of
a Lady. “She might have left them there,” she said. “I would have
had my husband pick them up soon. Yesterday she had the Maid put
some of her own horrid chairs and tables out here while they were
cleaning, and I never touched them.”
Mr. Sparrow flew up with a fine Turkey feather. “It came from the
Lady’s duster,” he said. “I think it will give quite an air to your nest.”
“Excellent!” cried his wife. “Just wait until I get ready for it.” He
clung patiently by one foot to the doorway. When that was tired he
changed to the other. When that was tired he perched on the top of
the column. He was very hungry, and he saw some grain dropped
from a passing wagon.
“Hurry up, my dear!” he called. “It is past my dinner-time already.”
“Wait until supper then,” cried his wife. “As if I hadn’t enough to do
without thinking about your dinner! Don’t let go of it or it will be
blown away.”
Then Mr. Sparrow lost his temper. He stuck that feather into a crack
near by, and flew softly away to eat some grain. He thought he
might be back in time to carry in the feather and his wife never
know where he had been. Unfortunately, he got to talking and did
not hear his wife call him.
“Mr. Sparrow!” said she. “Mr. Sparrow! I am ready for that feather.”
When he did not answer, she put her head out of the doorway.
There was the Turkey feather stuck into a crack, and in the road
beyond was her husband eating happily with several of his friends.
She looked very angry and opened her bill to speak. Then she
changed her mind and flew quietly off the other way. She went
straight to the Horse-block, where another old suitor was, the one
who had come so near winning her. “Mr. Sparrow has disobeyed
me,” she said, “and is actually eating his dinner when he should be
waiting by the nest to help me. I believe that I ought to have
married you, but better late than never. Come now.”
This was how it happened that when Mr. Sparrow’s stomach was
quite full, and he suddenly remembered his work, he flew back and
found the Turkey feather gone. In the eaves overhead he heard Mrs.
Sparrow telling somebody else what to do. He tried to force his way
up there. Every time he was shoved back, and not very gently either.
“You might better look for another home,” said Mrs. Sparrow’s voice.
“I have found another husband, one who will help me as I wish.
Good-by.”
That was the ending of Mr. Sparrow’s first marriage. It was a very
sad affair, and the birds talked of nothing else for a long time
afterward. Some said that it served him exactly right, because he
married to get into a fine family, when there were dozens of Sparrow
daughters much prettier and nicer than the one he chose. There
may have been something in this, for certainly if Mrs. Sparrow had
not been so sure of finding another to take his place, she would not
have turned him out in the way she did. It is said, however, that her
second husband had a hard life of it.
A RAINY DAY ON THE LAWN

W HEN the sun rose, that morning late in April, he tried and
tried to look at the big house and see what was happening. All
he could see was a thick gray cloud veil stretched between him and
the earth, and, shine as hard as he might, not a single sunbeam
went through that veil.
When the Blackbirds awakened, they found a drizzling rain falling,
and hurried on their waterproofs to get ready for a wet time.
Blackbirds are always handsome, yet they never look better than
when it rains. They coat their feathers with oil from the pockets
under their tails, as indeed all birds do, and then they fly to the high
branches of some tall and swaying tree and talk and talk and talk
and talk. They do not get into little groups and face each other, but
scatter themselves around and face the wind. This is most sensible,
for if one of them were to turn his back to the wind, it would rumple
up his feathers and give the raindrops a chance to get down to his
skin. When they speak, or at least when they have anything really
important to say, they ruffle their own feathers and stand on tip-toe,
but they ruffle them carefully and face the wind all the time.
When the Robins opened their round eyes, they chirped cheerfully to
each other and put on their waterproofs. “Good weather for us,”
they said. “It will make fine mud for plastering our new nests, and it
will bring out the Worms.”
The English Sparrows, Goldfinches, and other seed-eaters were not
made happy by the rain. With them it was only something to be
borne patiently and without complaining. The Hummingbirds found
fewer fresh blossoms open on cloudy days, and so had to fly farther
and work harder for their food. The Pewees and other fly-catchers
oiled their feathers and kept steadily at work.
“O MOTHER, IT IS RAINING!”

The birds had not awakened so early as usual, because it was


darker. They had hardly got well started on their breakfast before a
sleepy little face appeared at the window of the big house and a
sleepy little voice called out: “O Mother, it is raining! I didn’t want it
to rain.”
“Foolish! Foolish! Foolish!” chirped the Robins on the lawn. “Boys
would know better than to say such things if they were birds.”
“Boys are a bother, anyway,” said an English Sparrow, as he
spattered in the edge of a puddle. “I wish they had never been
hatched.”
“Ker-eeeee!” said a Blackbird above his head. “I suppose they may
be of some use in the world. I notice that the Gentleman and the
Lady seem to think a great deal of this one, and they are a very
good sort of people.”
“I’d like them better if they didn’t keep a Cat,” said his brother.
“Their Cat is the greatest climber I ever saw. He came almost to the
top of this maple after me yesterday, and I have seen him go clear
to the eaves of the big house on the woodbine.”
“That is because the Sparrows live there,” said Mr. Wren. “He went
to see their children. Silvertip says that he is very fond of children—
they are so much more tender than their parents.” Mr. Wren could
laugh about this because his own children were always safely
housed. Besides, you know, he had reason to dislike Sparrows.
“I would not stay here,” said a Sparrow who had just come up, “if
the people here were not of the right sort. They have mountain ash
trees and sweetbrier bushes where birds find good feeding. And in
the winter that Boy throws out bread crumbs and wheat for us.”
“Humph!” said the Oldest Blackbird. “There is no need of talking so
much about it. You can always tell what sort of people live in a place
by seeing if they have a bird-house. If they have, and it is a sensible
one, where a bird could live comfortably, they are all right.”
After that the birds worked more and talked less, for the Oldest
Blackbird, while he was often grumpy and sometimes cross, was
really a very sensible bird, and what he had said was true. The
Robins went here and there over the lawn in quick, short runs,
pausing once in a while with their heads bent forward and then
pulling up choice Worms to eat. Some of their mouthfuls were half
as long as they, but that was not rude in Robins. What they insist on
in bringing up their children is that mouthfuls should not be too
broad, and that they should not stop swallowing until all the Worm is
out of sight.
The Blackbirds hunted in a more dignified way. They never ran after
food, or indeed after anything else. “If walking is not fast enough,”
the Blackbird mothers say, “then fly, but do not run.” They walked in
parties over the lawn and waggled their heads at each step. When
they found Grubs they did not appear greedy, yet never a Grub
escaped.
“There are two ways of hurrying,” they often said. “One is the jerky
way and the other is our way, of being sure and steady. Of course
our way is the better. You will see that we do just as much and make
less fuss.”
Silvertip came to the edge of the porch and looked around. He was
licking his lips, and every bird on the lawn was happy to see that, for
it meant that he had just finished his breakfast. His eyes gleamed
and his tail waved stiffly as he saw the fat Robins so near. He even
crouched down and took four short steps, quivering his body and
trying his muscles. Then he remembered how wet the grass was and
turned back with a long sigh. After all, his stomach was full and he
could afford to wait until the grass was dry. The Robins would be
there then, and if they kept on eating Worms at this rate, they would
be growing plump and juicy all the time. He began to lick himself all
over, as every truly tidy Cat does after eating. By the time he had
finished the tip of his tail he was sleepy, so he went into the kitchen
and dozed by the fire.
The front door opened with a bang, and the Little Boy stood there,
shouting and waving a piece of red paper with a string tied to it.
“See my kite!” he cried. “Whee-ee-ee!”
Five birds who had been feeding near flew off in wild alarm. “Now
why did he do that?” asked one, after they had settled down
elsewhere. Nobody answered. None but Little Boys understand these
things, and even they do not always tell.
The Lady came to the door behind him and helped him start away.
He proudly carried a small new umbrella, and the precious kite
fluttered out behind him. When he was outside the gate, he peeped
through it and called back: “Good-by, Mother! I’m going to school to
learn everyfing. I’ll be a good Boy. Good-by!” Then he ran down the
walk with the umbrella held back over his shoulder and the rain
falling squarely in his face. All that the birds could see of the Little
Boy then was his fat legs bobbing along below the umbrella.
“There!” said all the birds together. “There! Silvertip is asleep and
the Little Boy has gone to school. Now we can take comfort.”

When the morning was nearly past, and the birds felt so safe that
they had grown almost careless, Silvertip wakened and felt hungry.
He walked slowly out of the kitchen door and looked at the grass.
The sun was now shining, and it was no longer sparkling with tiny
drops. He crept down the steps and around to a place under a big
spruce tree, the lower branches of which lay along the ground. A fat
Robin was hunting near by.
Silvertip watched her hungrily, and if you were a Cat you might have
done exactly the same thing. So you must not blame Silvertip. He
was creeping, creeping, creeping nearer, and never looking away
from her, when the Little Boy came tramping across the grass. He
had come in by the gate of the driveway, and was walking straight
toward Silvertip, who neither saw nor heard him.
Then the Little Boy saw what was happening, and dropped his bright
paper chain on the grass beside him. “G’way!” he cried, waving his
umbrella. “G’way! Don’t you try to eat any birds ’round here. My
father doesn’t ’low it. G’way! G’way! Else I’ll tell my mother that you
are a bad Cat.”
Silvertip fled under the porch, the Robin flew up onto the snowball
bush, and all around the birds sang the praises of the good Little
Boy with the umbrella. But the Little Boy didn’t know this. He stood
by the porch and dangled his pretty paper chain until Silvertip
forgave him and came out to play. Then they ran together into the
house, and the birds heard him shouting, “Mother! Mother! Where
are you? I want to give Silvertip some cream. He is so very hungry
that he most had to eat up a Robin, only I wouldn’t let him.”
THE PERSISTENT PHŒBE

IT is not often that a Phœbe will nest anywhere except near


running water, and nobody but the Phœbes themselves will ever
know why this pair chose to build under a porch of the big house.
When they came there on their wedding trip the other birds
supposed that they were only visiting, and it was not until a Catbird
heard them discussing different porches that any one really believed
they might come there to live.
Mrs. Phœbe was eager to begin at once, and could not pass a soft
bit of moss or an unusually good blade of grass without stopping to
look it over and think how she could weave it in. “I see no use in
waiting,” said she. “I know just as much about building now as I
shall after a while, and I should like a home of my own. It makes my
bill fairly tingle to see all these fine grasses and mosses waiting to
be used. And the worst of it is,” she added, “that if we wait, some
other bird may get them instead.”
Mr. Phœbe wanted to think it over a little longer. He was older than
his wife and had been married before. “Phœbe!” he would exclaim.
“Wait a day. You know we are building by a house to please you,
now wait one more day to please me.”
That, you see, was quite right and perfectly fair, for it is not fair for
one person to decide everything in a family, and it was right for the
wife to wait as long as she could. She could not, of course, wait
many days, for there were eggs to be laid, and when it was time for
them, the nest had to be ready. Mr. Phœbe knew this and wasted no
time.
“We cannot build on a rock,” said he, “because there are no rocks
here, and we cannot build under a bridge because there is no bridge
here. My other wife and I lived under a bridge.” Then he stood silent
for a long time and looked down at his black feet. When he spoke of
his first wife he always seemed sad. The second Mrs. Phœbe had
not liked this at first, but he was so good and kind to her, and let her
have her own way so much more than some husbands would, that
she had begun to feel happier about it.
There is reason to think that she chose an unusual nesting-place just
to see how far she could coax him out of his old ways. Perhaps, too,
she thought that there would be less in such a place to remind him
of his first wife. Another thing which had made her come to feel
differently was remembering that if he died or left her she would
marry again. Then, you know, she might want to think and talk
about her first husband.
She was very proud of him, and watched him as he stood thinking.
His upper feathers were deep brown, his under ones a dingy white,
and the outer edges of some of his tail-feathers were light colored.
His most beautiful features were his black bill and feet and the crest
which he could raise on the top of his head. Mrs. Phœbe had the
same coloring as her husband, yet she always insisted that he was
the better looking of the two, while he insisted, as a good and wise
husband should, that she was by far the handsomer.
Now Mr. Phœbe was speaking. “We have decided to build on this
house,” said he, “and under a porch. Still, there are four large ones
and we must find out which is the best. You feed on the shady side
and I will feed on the sunny side of the house. Then we shall see
how much these people use their porches.”
“I’ll do it,” answered his wife, “but isn’t it a pity that there are people
living in this house? It would be so much pleasanter if it were
empty.”
Mrs. Phœbe perched on a maple branch on the shady side and
watched two porches. She thought she would like the front one the
better, and had already chosen her window ledge, when she noticed
a pair of English Sparrows dragging straws and feathers toward it
and disappearing inside the cornice. “Not there,” she said firmly, as
she clutched the branch even more tightly with her pretty black feet.
“I will not have quarrelsome neighbors, and I could never bring our
children up to be good if the young Sparrows were always near,
showing them how to be naughty.” Then she darted after a Fly,
caught and swallowed him, and was back on her perch.
“I wonder how the back one would do?” she said. “There are no
steps leading to it, and those sweetbrier bushes all around it would
keep Boys from climbing onto the railing.”
She flew near and saw the Maid kneading bread by one window. A
door stood open into the big kitchen, and through two other
windows she could look into a pleasant dining-room. “I wouldn’t
mind that,” she said. “If I have plenty to eat myself, I would just as
soon see other people eating. We like different things anyway. I dare
say those people never tasted an insect in their lives and do not
even know the flavor of a choice Fly.” Then she swallowed a careless
Bug who had mistaken her for an English Sparrow and flown when
he should have stayed hidden. Mrs. Phœbe was much interested in
the nest, but not so much as to let an insect escape. Oh, never so
much as that!
Mr. Phœbe watched the back porch on his side. Some Robins were
building on a window-ledge there, which he thought exceeding
imprudent. But then he was not surprised, for everybody knows how
careless Robins are. That is why so many of them have to leave their
nests—because they are built where no nest should be. Mr. Phœbe
could tell at a glance that no bird should build there. Woodbine
climbed over the pillars and fell in a thick curtain from the cornice,
and beside the door stood a saucerful of milk. “That means a Cat,”
said he, “a Cat who stays on this porch most of the time and always
comes here when he is hungry. And when he tires of milk he will
climb up that woodbine and finish with young Robin. Or, perhaps,”
he added, “I should say that he will finish a young Robin.”
The front porch on his side was sunshiny and quiet, but there was
the woodbine again, and with the Cat so near. He next looked at the
portico over the front door. Under the roof of this was a queer shiny,
thin thing with a loop of black thread hanging down in it. He tried to
get the thread, but only hit and hurt his bill against the shiny, thin
stuff. Then he remembered seeing a bright light in it the night before
when he had been awakened by a bad dream. “That will never do,”
he said. “It is not good for children to sleep with a light near. One
would want to be catching insects there, too,” he added, “when he
should be sleeping. There must be many drawn by the light.”
So it ended in the couple building under the dining-room porch on
the shelf-like top of a column. Mrs. Phœbe chose this instead of a
window-ledge because from here she could look into the window
while brooding her eggs. “You may laugh at me all you choose,” said
she to her husband, “for I did wish the house empty. Since it cannot
be, however, I might as well see what the people in it do.”
“I was not laughing, my dear,” answered her husband meekly (you
remember that he had been married before). “I was only smiling
with pleasure at our fine nest. You have so much taste in arranging
grasses!”
That was the way in which the Phœbes began housekeeping. It was
not always easy, sitting on the nest day after day as Mrs. Phœbe had
to, with only a chance now and then to stretch her tired legs. She
was even glad that people lived in the house. “It gives me
something to think about,” said she, “although I do get much out of
patience with them sometimes. Much they know about bringing up
children! That Boy of theirs eats only three times a day. How can
they ever hope to raise him unless he eats more? Now, I expect to
feed my children all the time, and that is the way to do.” Here she
darted away to catch a Fly who came blundering along.
“It’s a good thing for that Fly that I got him,” she said, smilingly. “It
saved him from being caught in the Spider’s web over there, and I
am sure it is much pleasanter to be swallowed whole by a polite
Phœbe than to be nibbled at by a horrid Spider.”
Mr. Phœbe sometimes brought her a dainty morsel, but he spent
much of his time by the hydrant. “There is not much chance to
bathe,” he said, as he wallowed around in the little pool beside it,
“but it is something to smell water. You know we Phœbes like to fly
in and out of ponds and rivers, even when we cannot stop for a real
bath.” His favorite perch was on the top of a tall pole covered with
cinnamon vine, in the flower garden. Here he would sit for a whole
morning at a time, darting off now and then for an insect, but
always returning to the same place and position. He did not even
face the other way for a change.
The little Phœbes were hatched much like other birds, and were
about as good and about as naughty as children usually are. Mrs.
Phœbe was positive that they were remarkable in every way. Mr.
Phœbe, having raised other broods, did not think them quite so
wonderful, although he admitted that there was not another nestling
on the place to compare with them. “Still,” as he would modestly
remark, “we must remember that we are the only Phœbes here, and
that it is not fair to compare them with the young of other birds. You
could not expect our neighbors’ children to be as bright as they.”
Unfortunately there were only two little Phœbes, so each parent
could give all his time to one. The mother cared for the son and the
father for the daughter. When it was time for them to learn to catch
their own Flies, these children did not want to do so. The father
made his daughter learn, in spite of the fuss she made. He gave her
his old perch on the cinnamon-vine pole, and told her that she must
try to catch every insect that flew past. This was after she had been
out of the nest several days, and had learned to use her feet and
wings.
“If you do not,” he said, “I shall not feed you anything.” When she
pouted her bill, he paid no attention to it, and she soon stopped.
There is no use in pouting, you know, unless somebody is looking at
you and wishing that you wouldn’t. Perhaps it was because he had
brought up children before that Mr. Phœbe was so wise.
Mrs. Phœbe meant to be very firm also, but when her son
whimpered and said that he couldn’t, he knew he couldn’t, catch a
single one, and that he was sure he would tumble to the ground if
he tried it, she always felt sorry for him and said: “Perhaps you can
to-morrow.” Then she would catch food for him again.
This is how it happened that, day after day, a plump and strong
young Phœbe sat on a branch of the syringa bush and let his tired
mother feed him. At last his father quite lost patience and interfered.
“My dear,” he said to his wife, “I will be with our son to-day, and you
may have a rest.”
“You are very kind,” she replied, “but he is so used to having me that
I think I might better——”
“I said,” interrupted her husband, “that I would be with our son to-
day. I advise you to fly away with our daughter and show her
something of the world.” Mrs. Phœbe did not often hear him speak
in that tone of voice. When he did, she always agreed with him.
As soon as father and son were alone, the father said: “Now you are
going to catch Flies before sunset. You have let your poor mother
nearly work her feathers off for you. (Of course, feathers do not
come off so, but this was his way of speaking.) She is very tired, and
you are not to act like this again. There comes a Fly. Catch him!”
The young Phœbe made a wild dash, missed his Fly, and came back
to the syringa bush whimpering. “I knew I couldn’t,” he said. “I tried
as hard as I could, but he flew away.”
“Yes,” said his father. “You tried once, just once. You may have to try
a hundred times before you catch one, but that is no reason why
you should not try. Go for that Mosquito.”
The son went, and missed him, of course. This time he knew better
than to talk about it. He just flew back to his perch and looked
miserable.
“I think you got a little nearer to this one,” said his father. “Go for
that Fly!”
The young Phœbe was kept darting here and there so often that he
had no time to be sulky. Indeed, if people have to keep moving quite
fast, they soon forget to want to be sulky. At last he was surprised
by his father’s tucking a very delicious Bluebottle down his throat.
“Just for a lunch,” he explained. “Now try for that one.”
The son made a sudden lurch and flight, and actually caught him. It
was a much smaller Fly than the one which his father had fed him,
but it tasted better. He swallowed it as slowly as he could, so as to
feel it going down as long as possible. Then he began to be happier.
“Watch me catch that Mosquito,” he said. And when he missed him,
as he did, he made no fuss at all—only said: “I’ll get the next one!”
When he missed that he simply said: “Well, I’ll get the next one,
anyhow!”
And he did.
All day long he darted and failed or darted and succeeded, and more
and more often he caught the insect instead of missing him.
When the long shadows on the lawn showed that sunset was near,
his mother and sister came back. His mother had a delicious morsel
for him to eat. “Open your bill very wide,” she said, “you poor, tired,
hungry child.”
He did open his bill, because a Phœbe can always eat a little more
anyway, but he did not open it until he had said: “Why, I’m not much
tired, and I am not really hungry at all. You just ought to see me
catch Flies!”
You can imagine how surprised his mother was. And in the tall fir
tree near by he heard a Blackbird say something in a hoarse voice
about a persistent Phœbe. But that didn’t make much difference,
because, you see, he didn’t know what “persistent” meant, and if he
had known he could not have told whether the Blackbird was talking
about him or about his father. Could you have told, if you had been a
Phœbe?
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like