0% found this document useful (0 votes)
15 views60 pages

Perl Cookbook 2nd Edition Tom Christiansen Download PDF

The document provides links to download various programming cookbooks, including the Perl Cookbook 2nd Edition by Tom Christiansen and Nathan Torkington. It includes details such as ISBN numbers, file format, and publication year for the Perl Cookbook. Additionally, it mentions other related resources and titles available for download on ebookultra.com.

Uploaded by

obpzmhul6263
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
0% found this document useful (0 votes)
15 views60 pages

Perl Cookbook 2nd Edition Tom Christiansen Download PDF

The document provides links to download various programming cookbooks, including the Perl Cookbook 2nd Edition by Tom Christiansen and Nathan Torkington. It includes details such as ISBN numbers, file format, and publication year for the Perl Cookbook. Additionally, it mentions other related resources and titles available for download on ebookultra.com.

Uploaded by

obpzmhul6263
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/ 60

Perl Cookbook 2nd Edition Tom Christiansen download

pdf

https://ebookultra.com/download/perl-cookbook-2nd-edition-tom-
christiansen/

Visit ebookultra.com today to download the complete set of


ebook or textbook!
We believe these products will be a great fit for you. Click
the link to download now, or visit ebookultra.com
to discover even more!

PostGIS Cookbook 2nd edition Edition Corti

https://ebookultra.com/download/postgis-cookbook-2nd-edition-edition-
corti/

Arduino Cookbook 2nd edition Edition Margolis

https://ebookultra.com/download/arduino-cookbook-2nd-edition-edition-
margolis/

Ruby Cookbook 2nd Edition Lucas Carlson

https://ebookultra.com/download/ruby-cookbook-2nd-edition-lucas-
carlson/

PrimeFaces Cookbook 2nd Edition Mert Caliskan

https://ebookultra.com/download/primefaces-cookbook-2nd-edition-mert-
caliskan/
ElasticSearch Cookbook 2nd Edition Alberto Paro

https://ebookultra.com/download/elasticsearch-cookbook-2nd-edition-
alberto-paro/

Language Universals 1st Edition Morten H. Christiansen

https://ebookultra.com/download/language-universals-1st-edition-
morten-h-christiansen/

Digital Photography An introduction 2nd Edition Tom Ang

https://ebookultra.com/download/digital-photography-an-
introduction-2nd-edition-tom-ang/

Ethics The Key Thinkers 2nd Edition Tom Angier

https://ebookultra.com/download/ethics-the-key-thinkers-2nd-edition-
tom-angier/

Beginning Perl 3rd Edition James Lee

https://ebookultra.com/download/beginning-perl-3rd-edition-james-lee/
Perl Cookbook 2nd Edition Tom Christiansen Digital
Instant Download
Author(s): Tom Christiansen; Nathan Torkington
ISBN(s): 9780596003135, 0596003137
Edition: 2
File Details: PDF, 3.23 MB
Year: 2003
Language: english
Perl Cookbook ™
Other Perl resources from O’Reilly
Related titles Programming Perl Perl 6 Essentials
Learning Perl Learning Perl Objects,
CGI Programming with Perl References & Modules
Computer Science & Perl Mastering Regular
Programming Expressions
Web, Graphics & Perl/Tk Practical mod_perl
Games, Diversions & Perl Perl Pocket Reference
Culture Perl in a Nutshell
Programming Web Services Perl Graphics Programming
with Perl

Perl Books perl.oreilly.com is a complete catalog of O’Reilly’s books on Perl


Resource Center and related technologies, including sample chapters and code
examples.

Perl.com is the central web site for the Perl community. It is the
perfect starting place for finding out everything there is to know
about Perl.

Conferences O’Reilly & Associates brings diverse innovators together to nur-


ture the ideas that spark revolutionary industries. We specialize
in documenting the latest tools and systems, translating the in-
novator’s knowledge into useful skills for those in the trenches.
Visit conferences.oreilly.com for our upcoming events.

Safari Bookshelf (safari.oreilly.com) is the premier online refer-


ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today with a free trial.
SECOND EDITION

Perl Cookbook

Tom Christiansen and Nathan Torkington

Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo


Perl Cookbook, Second Edition
by Tom Christiansen and Nathan Torkington

Copyright © 2003, 1998 O’Reilly Media, Inc. All rights reserved.


Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly Media, Inc. books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (safari.oreilly.com). For more information, con-
tact our corporate/institutional sales department: (800) 998-9938 or [email protected].

Editor: Linda Mui


Production Editor: Genevieve d’Entremont
Cover Designer: Edie Freedman
Interior Designer: David Futato

Printing History:
August 1998: First Edition.
August 2003: Second Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarks of O’Reilly Media, Inc. The Cookbook series designations, Perl Cookbook, the image
of a bighorn sheep, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the
designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of
a trademark claim, the designations have been printed in caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher and authors
assume no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.

ISBN: 0-596-00313-7
ISBN13: 978-0-596-00313-5
[M] [12/06]
Table of Contents

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

1. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Accessing Substrings 7
1.2 Establishing a Default Value 10
1.3 Exchanging Values Without Using Temporary Variables 12
1.4 Converting Between Characters and Values 13
1.5 Using Named Unicode Characters 15
1.6 Processing a String One Character at a Time 17
1.7 Reversing a String by Word or Character 19
1.8 Treating Unicode Combined Characters as Single Characters 21
1.9 Canonicalizing Strings with Unicode Combined Characters 22
1.10 Treating a Unicode String as Octets 24
1.11 Expanding and Compressing Tabs 25
1.12 Expanding Variables in User Input 27
1.13 Controlling Case 29
1.14 Properly Capitalizing a Title or Headline 31
1.15 Interpolating Functions and Expressions Within Strings 33
1.16 Indenting Here Documents 35
1.17 Reformatting Paragraphs 39
1.18 Escaping Characters 41
1.19 Trimming Blanks from the Ends of a String 43
1.20 Parsing Comma-Separated Data 44
1.21 Constant Variables 48
1.22 Soundex Matching 50

v
1.23 Program: fixstyle 52
1.24 Program: psgrep 55

2. Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.1 Checking Whether a String Is a Valid Number 60
2.2 Rounding Floating-Point Numbers 63
2.3 Comparing Floating-Point Numbers 67
2.4 Operating on a Series of Integers 68
2.5 Working with Roman Numerals 70
2.6 Generating Random Numbers 71
2.7 Generating Repeatable Random Number Sequences 72
2.8 Making Numbers Even More Random 73
2.9 Generating Biased Random Numbers 74
2.10 Doing Trigonometry in Degrees, Not Radians 76
2.11 Calculating More Trigonometric Functions 77
2.12 Taking Logarithms 78
2.13 Multiplying Matrices 80
2.14 Using Complex Numbers 82
2.15 Converting Binary, Octal, and Hexadecimal Numbers 83
2.16 Putting Commas in Numbers 84
2.17 Printing Correct Plurals 85
2.18 Program: Calculating Prime Factors 87

3. Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90


3.1 Finding Today’s Date 92
3.2 Converting DMYHMS to Epoch Seconds 94
3.3 Converting Epoch Seconds to DMYHMS 95
3.4 Adding to or Subtracting from a Date 96
3.5 Difference of Two Dates 97
3.6 Day in a Week/Month/Year or Week Number 99
3.7 Parsing Dates and Times from Strings 100
3.8 Printing a Date 101
3.9 High-Resolution Timers 103
3.10 Short Sleeps 105
3.11 Program: hopdelta 106

4. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.1 Specifying a List in Your Program 111
4.2 Printing a List with Commas 113

vi | Table of Contents
4.3 Changing Array Size 115
4.4 Implementing a Sparse Array 117
4.5 Iterating Over an Array 119
4.6 Iterating Over an Array by Reference 122
4.7 Extracting Unique Elements from a List 124
4.8 Finding Elements in One Array but Not Another 126
4.9 Computing Union, Intersection, or Difference of Unique Lists 128
4.10 Appending One Array to Another 130
4.11 Reversing an Array 131
4.12 Processing Multiple Elements of an Array 132
4.13 Finding the First List Element That Passes a Test 134
4.14 Finding All Elements in an Array Matching Certain Criteria 136
4.15 Sorting an Array Numerically 138
4.16 Sorting a List by Computable Field 139
4.17 Implementing a Circular List 143
4.18 Randomizing an Array 144
4.19 Program: words 144
4.20 Program: permute 146

5. Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
5.1 Adding an Element to a Hash 152
5.2 Testing for the Presence of a Key in a Hash 153
5.3 Creating a Hash with Immutable Keys or Values 155
5.4 Deleting from a Hash 156
5.5 Traversing a Hash 157
5.6 Printing a Hash 160
5.7 Retrieving from a Hash in Insertion Order 161
5.8 Hashes with Multiple Values per Key 162
5.9 Inverting a Hash 164
5.10 Sorting a Hash 166
5.11 Merging Hashes 167
5.12 Finding Common or Different Keys in Two Hashes 169
5.13 Hashing References 170
5.14 Presizing a Hash 171
5.15 Finding the Most Common Anything 172
5.16 Representing Relationships Between Data 173
5.17 Program: dutree 174

Table of Contents | vii


6. Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
6.1 Copying and Substituting Simultaneously 185
6.2 Matching Letters 186
6.3 Matching Words 188
6.4 Commenting Regular Expressions 189
6.5 Finding the Nth Occurrence of a Match 192
6.6 Matching Within Multiple Lines 195
6.7 Reading Records with a Separator 198
6.8 Extracting a Range of Lines 199
6.9 Matching Shell Globs as Regular Expressions 202
6.10 Speeding Up Interpolated Matches 203
6.11 Testing for a Valid Pattern 205
6.12 Honoring Locale Settings in Regular Expressions 207
6.13 Approximate Matching 209
6.14 Matching from Where the Last Pattern Left Off 210
6.15 Greedy and Non-Greedy Matches 212
6.16 Detecting Doubled Words 215
6.17 Matching Nested Patterns 218
6.18 Expressing AND, OR, and NOT in a Single Pattern 220
6.19 Matching a Valid Mail Address 224
6.20 Matching Abbreviations 226
6.21 Program: urlify 228
6.22 Program: tcgrep 229
6.23 Regular Expression Grab Bag 235

7. File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239


7.1 Opening a File 247
7.2 Opening Files with Unusual Filenames 251
7.3 Expanding Tildes in Filenames 253
7.4 Making Perl Report Filenames in Error Messages 254
7.5 Storing Filehandles into Variables 255
7.6 Writing a Subroutine That Takes Filehandles as Built-ins Do 258
7.7 Caching Open Output Filehandles 259
7.8 Printing to Many Filehandles Simultaneously 260
7.9 Opening and Closing File Descriptors by Number 262
7.10 Copying Filehandles 263
7.11 Creating Temporary Files 265
7.12 Storing a File Inside Your Program Text 266
7.13 Storing Multiple Files in the DATA Area 268

viii | Table of Contents


7.14 Writing a Unix-Style Filter Program 270
7.15 Modifying a File in Place with a Temporary File 274
7.16 Modifying a File in Place with the -i Switch 276
7.17 Modifying a File in Place Without a Temporary File 277
7.18 Locking a File 279
7.19 Flushing Output 281
7.20 Doing Non-Blocking I/O 284
7.21 Determining the Number of Unread Bytes 285
7.22 Reading from Many Filehandles Without Blocking 287
7.23 Reading an Entire Line Without Blocking 289
7.24 Program: netlock 291
7.25 Program: lockarea 294

8. File Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300


8.1 Reading Lines with Continuation Characters 306
8.2 Counting Lines (or Paragraphs or Records) in a File 307
8.3 Processing Every Word in a File 309
8.4 Reading a File Backward by Line or Paragraph 310
8.5 Trailing a Growing File 312
8.6 Picking a Random Line from a File 314
8.7 Randomizing All Lines 315
8.8 Reading a Particular Line in a File 316
8.9 Processing Variable-Length Text Fields 318
8.10 Removing the Last Line of a File 319
8.11 Processing Binary Files 320
8.12 Using Random-Access I/O 321
8.13 Updating a Random-Access File 322
8.14 Reading a String from a Binary File 324
8.15 Reading Fixed-Length Records 325
8.16 Reading Configuration Files 326
8.17 Testing a File for Trustworthiness 329
8.18 Treating a File as an Array 331
8.19 Setting the Default I/O Layers 333
8.20 Reading or Writing Unicode from a Filehandle 333
8.21 Converting Microsoft Text Files into Unicode 336
8.22 Comparing the Contents of Two Files 338
8.23 Pretending a String Is a File 339
8.24 Program: tailwtmp 340
8.25 Program: tctee 341

Table of Contents | ix
8.26 Program: laston 342
8.27 Program: Flat File Indexes 343

9. Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
9.1 Getting and Setting Timestamps 351
9.2 Deleting a File 353
9.3 Copying or Moving a File 354
9.4 Recognizing Two Names for the Same File 355
9.5 Processing All Files in a Directory 356
9.6 Globbing, or Getting a List of Filenames Matching a Pattern 358
9.7 Processing All Files in a Directory Recursively 359
9.8 Removing a Directory and Its Contents 362
9.9 Renaming Files 363
9.10 Splitting a Filename into Its Component Parts 365
9.11 Working with Symbolic File Permissions Instead of Octal Values 367
9.12 Program: symirror 369
9.13 Program: lst 370

10. Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373


10.1 Accessing Subroutine Arguments 374
10.2 Making Variables Private to a Function 376
10.3 Creating Persistent Private Variables 378
10.4 Determining Current Function Name 379
10.5 Passing Arrays and Hashes by Reference 381
10.6 Detecting Return Context 382
10.7 Passing by Named Parameter 383
10.8 Skipping Selected Return Values 385
10.9 Returning More Than One Array or Hash 386
10.10 Returning Failure 387
10.11 Prototyping Functions 388
10.12 Handling Exceptions 391
10.13 Saving Global Values 393
10.14 Redefining a Function 397
10.15 Trapping Undefined Function Calls with AUTOLOAD 399
10.16 Nesting Subroutines 400
10.17 Writing a Switch Statement 401
10.18 Program: Sorting Your Mail 404

x | Table of Contents
11. References and Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
11.1 Taking References to Arrays 413
11.2 Making Hashes of Arrays 415
11.3 Taking References to Hashes 416
11.4 Taking References to Functions 417
11.5 Taking References to Scalars 420
11.6 Creating Arrays of Scalar References 421
11.7 Using Closures Instead of Objects 423
11.8 Creating References to Methods 424
11.9 Constructing Records 425
11.10 Reading and Writing Hash Records to Text Files 428
11.11 Printing Data Structures 429
11.12 Copying Data Structures 431
11.13 Storing Data Structures to Disk 432
11.14 Transparently Persistent Data Structures 434
11.15 Coping with Circular Data Structures Using Weak References 435
11.16 Program: Outlines 438
11.17 Program: Binary Trees 441

12. Packages, Libraries, and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444


12.1 Defining a Module’s Interface 449
12.2 Trapping Errors in require or use 451
12.3 Delaying use Until Runtime 453
12.4 Making Variables Private to a Module 455
12.5 Making Functions Private to a Module 457
12.6 Determining the Caller’s Package 459
12.7 Automating Module Cleanup 461
12.8 Keeping Your Own Module Directory 463
12.9 Preparing a Module for Distribution 465
12.10 Speeding Module Loading with SelfLoader 467
12.11 Speeding Up Module Loading with Autoloader 468
12.12 Overriding Built-in Functions 469
12.13 Overriding a Built-in Function in All Packages 471
12.14 Reporting Errors and Warnings Like Built-ins 473
12.15 Customizing Warnings 475
12.16 Referring to Packages Indirectly 479
12.17 Using h2ph to Translate C #include Files 480
12.18 Using h2xs to Make a Module with C Code 483

Table of Contents | xi
12.19 Writing Extensions in C with Inline::C 486
12.20 Documenting Your Module with Pod 487
12.21 Building and Installing a CPAN Module 489
12.22 Example: Module Template 492
12.23 Program: Finding Versions and Descriptions of Installed Modules 493

13. Classes, Objects, and Ties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498


13.1 Constructing an Object 506
13.2 Destroying an Object 508
13.3 Managing Instance Data 510
13.4 Managing Class Data 513
13.5 Using Classes as Structs 515
13.6 Cloning Constructors 519
13.7 Copy Constructors 520
13.8 Invoking Methods Indirectly 522
13.9 Determining Subclass Membership 524
13.10 Writing an Inheritable Class 525
13.11 Accessing Overridden Methods 527
13.12 Generating Attribute Methods Using AUTOLOAD 529
13.13 Coping with Circular Data Structures Using Objects 532
13.14 Overloading Operators 535
13.15 Creating Magic Variables with tie 540

14. Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548


14.1 Making and Using a DBM File 550
14.2 Emptying a DBM File 552
14.3 Converting Between DBM Files 553
14.4 Merging DBM Files 555
14.5 Sorting Large DBM Files 556
14.6 Storing Complex Data in a DBM File 558
14.7 Persistent Data 559
14.8 Saving Query Results to Excel or CSV 561
14.9 Executing an SQL Command Using DBI 562
14.10 Escaping Quotes 565
14.11 Dealing with Database Errors 566
14.12 Repeating Queries Efficiently 568
14.13 Building Queries Programmatically 570
14.14 Finding the Number of Rows Returned by a Query 571
14.15 Using Transactions 572

xii | Table of Contents


14.16 Viewing Data One Page at a Time 574
14.17 Querying a CSV File with SQL 576
14.18 Using SQL Without a Database Server 577
14.19 Program: ggh—Grep Netscape Global History 579

15. Interactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583


15.1 Parsing Program Arguments 585
15.2 Testing Whether a Program Is Running Interactively 587
15.3 Clearing the Screen 588
15.4 Determining Terminal or Window Size 589
15.5 Changing Text Color 590
15.6 Reading Single Characters from the Keyboard 592
15.7 Ringing the Terminal Bell 593
15.8 Using POSIX termios 595
15.9 Checking for Waiting Input 597
15.10 Reading Passwords 597
15.11 Editing Input 599
15.12 Managing the Screen 600
15.13 Controlling Another Program with Expect 603
15.14 Creating Menus with Tk 605
15.15 Creating Dialog Boxes with Tk 607
15.16 Responding to Tk Resize Events 610
15.17 Removing the DOS Shell Window with Windows Perl/Tk 612
15.18 Graphing Data 613
15.19 Thumbnailing Images 614
15.20 Adding Text to an Image 615
15.21 Program: Small termcap Program 616
15.22 Program: tkshufflepod 618
15.23 Program: graphbox 620

16. Process Management and Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 622


16.1 Gathering Output from a Program 625
16.2 Running Another Program 626
16.3 Replacing the Current Program with a Different One 629
16.4 Reading or Writing to Another Program 630
16.5 Filtering Your Own Output 632
16.6 Preprocessing Input 634
16.7 Reading STDERR from a Program 635
16.8 Controlling Input and Output of Another Program 638

Table of Contents | xiii


16.9 Controlling the Input, Output, and Error of Another Program 639
16.10 Communicating Between Related Processes 641
16.11 Making a Process Look Like a File with Named Pipes 647
16.12 Sharing Variables in Different Processes 650
16.13 Listing Available Signals 652
16.14 Sending a Signal 653
16.15 Installing a Signal Handler 654
16.16 Temporarily Overriding a Signal Handler 655
16.17 Writing a Signal Handler 656
16.18 Catching Ctrl-C 659
16.19 Avoiding Zombie Processes 660
16.20 Blocking Signals 663
16.21 Timing Out an Operation 664
16.22 Turning Signals into Fatal Errors 665
16.23 Program: sigrand 666

17. Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672


17.1 Writing a TCP Client 674
17.2 Writing a TCP Server 676
17.3 Communicating over TCP 679
17.4 Setting Up a UDP Client 682
17.5 Setting Up a UDP Server 684
17.6 Using Unix Domain Sockets 686
17.7 Identifying the Other End of a Socket 688
17.8 Finding Your Own Name and Address 689
17.9 Closing a Socket After Forking 690
17.10 Writing Bidirectional Clients 692
17.11 Forking Servers 694
17.12 Pre-Forking Servers 695
17.13 Non-Forking Servers 698
17.14 Multitasking Server with Threads 701
17.15 Writing a Multitasking Server with POE 703
17.16 Writing a Multihomed Server 705
17.17 Making a Daemon Server 706
17.18 Restarting a Server on Demand 708
17.19 Managing Multiple Streams of Input 710
17.20 Program: backsniff 713
17.21 Program: fwdport 714

xiv | Table of Contents


18. Internet Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
18.1 Simple DNS Lookups 721
18.2 Being an FTP Client 724
18.3 Sending Mail 727
18.4 Reading and Posting Usenet News Messages 730
18.5 Reading Mail with POP3 732
18.6 Simulating Telnet from a Program 734
18.7 Pinging a Machine 736
18.8 Accessing an LDAP Server 738
18.9 Sending Attachments in Mail 741
18.10 Extracting Attachments from Mail 744
18.11 Writing an XML-RPC Server 746
18.12 Writing an XML-RPC Client 748
18.13 Writing a SOAP Server 749
18.14 Writing a SOAP Client 750
18.15 Program: rfrm 751
18.16 Program: expn and vrfy 753

19. CGI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756


19.1 Writing a CGI Script 760
19.2 Redirecting Error Messages 762
19.3 Fixing a 500 Server Error 763
19.4 Writing a Safe CGI Program 767
19.5 Executing Commands Without Shell Escapes 770
19.6 Formatting Lists and Tables with HTML Shortcuts 773
19.7 Redirecting to a Different Location 775
19.8 Debugging the Raw HTTP Exchange 777
19.9 Managing Cookies 779
19.10 Creating Sticky Widgets 781
19.11 Writing a Multiscreen CGI Script 783
19.12 Saving a Form to a File or Mail Pipe 785
19.13 Program: chemiserie 787

20. Web Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792


20.1 Fetching a URL from a Perl Script 793
20.2 Automating Form Submission 796
20.3 Extracting URLs 797
20.4 Converting ASCII to HTML 800
20.5 Converting HTML to ASCII 801

Table of Contents | xv
20.6 Extracting or Removing HTML Tags 802
20.7 Finding Stale Links 804
20.8 Finding Fresh Links 805
20.9 Using Templates to Generate HTML 807
20.10 Mirroring Web Pages 810
20.11 Creating a Robot 811
20.12 Parsing a Web Server Log File 812
20.13 Processing Server Logs 813
20.14 Using Cookies 816
20.15 Fetching Password-Protected Pages 817
20.16 Fetching https:// Web Pages 818
20.17 Resuming an HTTP GET 819
20.18 Parsing HTML 820
20.19 Extracting Table Data 823
20.20 Program: htmlsub 825
20.21 Program: hrefsub 827

21. mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829


21.1 Authenticating 834
21.2 Setting Cookies 835
21.3 Accessing Cookie Values 837
21.4 Redirecting the Browser 838
21.5 Interrogating Headers 838
21.6 Accessing Form Parameters 839
21.7 Receiving Uploaded Files 840
21.8 Speeding Up Database Access 842
21.9 Customizing Apache’s Logging 843
21.10 Transparently Storing Information in URLs 845
21.11 Communicating Between mod_perl and PHP 846
21.12 Migrating from CGI to mod_perl 847
21.13 Sharing Information Between Handlers 848
21.14 Reloading Changed Modules 849
21.15 Benchmarking a mod_perl Application 850
21.16 Templating with HTML::Mason 852
21.17 Templating with Template Toolkit 856

22. XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863


22.1 Parsing XML into Data Structures 871
22.2 Parsing XML into a DOM Tree 874

xvi | Table of Contents


22.3 Parsing XML into SAX Events 876
22.4 Making Simple Changes to Elements or Text 879
22.5 Validating XML 882
22.6 Finding Elements and Text Within an XML Document 885
22.7 Processing XML Stylesheet Transformations 887
22.8 Processing Files Larger Than Available Memory 890
22.9 Reading and Writing RSS Files 891
22.10 Writing XML 895

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897

Table of Contents | xvii


Random documents with unrelated
content Scribd suggests to you:
This nursing Sister, while she had to have the hospital mosquito-
proof, in deference to the doctor's opinion, sternly declined to have
any such abomination anywhere near her little bungalow, and so the
cool, fresh night air blew in through her great windows, and we had
an extensive view of the glorious hillsides, all clothed in emerald
green, and if a clammy white mist wrapped us close when we waked
in the early morning so that we could not see beyond our own
verandahs, the rolling away of that mist was a gorgeous sight, ever
to be remembered.
Needless to say, the doctor's house was carefully enclosed in
mosquito-proof wire, and I dined in an oppressive atmosphere that
nearly drove me distracted. The bungalow was set high on a hilltop,
in the middle of a garden that should one day be beautiful, but he
has of course cut down every native tree, and owing to the
mosquito-proof wire we got no benefit from the cool breeze that was
blowing outside. He took me to see the new native village he was
building, a place that left an impression of corrugated iron and hard-
baked clay. Trees, of course, and all vegetation were taboo, but I am
bound in justice to say that the old village, a place teeming with
inhabitants, drawn from all corners of West Africa, attracted by the
lust for gold, was just as bare and ugly, and a good deal more
unkempt.
He took me out, and pointed out to me the principal hill in the
centre of Prestea, on which are the mining manager's and other
officials' houses, and he pointed it out with pride.
“There's a nice clean hill for you.”
The sun glared down fiercely on corrugated-iron roofs, the soil of
the hill looked like a raw, red scar, and there was not so much as a
blade of grass to be seen. I did not wonder that the unfortunate
women of Prestea had gone home sick if they had been compelled
to live in such a place.
I said, “It's a horrible place. I never saw a beautiful place more
utterly spoiled.”
He looked at me with surprise, and his surprise was thoroughly
genuine. “Why, what's the matter? It's nice and clean.”
I pointed to the beautiful hills all round.
“Mosquitoes,” said he, with a little snort for my ignorance.
“But you want some shade?”
He shook his head doubtfully.
“You can't have trees. The boys would leave pots under them.
Breeding places for mosquitoes.”
He was my host, so I did not like to say all I felt.
“I'd rather die of fever than sunstroke any day,” was the way it
finally came out.
“My dear lady,” he said judicially, as one who was correcting a
long-standing error, “no one dies of fever in Africa.”
“Exactly what I always maintain,” said I; “you, with your ghastly
hills are arranging for them to die of sunstroke.”
But he only reiterated that they could not have the trees, because
the boys would leave pots and pans under them, and so turn them
into mosquito traps. Personally, I didn't arrive at the logic of that,
because it has never seemed to me to require trees for boys to leave
pots about. The theory was, I suppose, that they would not walk out
into the hot sun, while they might be tempted to do work and make
litter under shade-trees. And again I did not wonder that there were
no women save the nursing Sister in Prestea. To live on that hill and
keep one's health would have been next door to impossible.
“It doesn't matter,” said the doctor, “we don't want women in West
Africa. I keep my wife at home. It isn't a white man's country.”

But I'm bound to say that they very often arrange it shall not be a
white man's and emphatically not a white woman's country. It suits
somebody's plan that the country should have an evil reputation.
Goldfields, too, must never be judged in the same category as one
judges the ordinary settlements in a country. When I was a tiny child
I learned to discriminate, and to know that “diggers” must not be
judged by the rules that guide the conduct of ordinary men. The
population of a goldfield are a wild and reckless lot, and they lead
wild and utterly reckless lives, and die in places where other people
manage to live happily enough.
When the gold first “broke out” in Victoria, my father was Gold
Commissioner on the Buckland River, among the mountains in the
north-eastern district, and I have heard him tell how the men used
to die like flies of “colonial” fever, and the theory was that there was
some emanation from the dense vegetation that was all around
them. Nowadays the Buckland is one of the healthiest spots in a
very healthy country, and no one ever gets fever of any sort there.
Now I do not wish to say that West Africa is one of the healthiest
countries in the world, but I do say that men very very often work
their own undoing.
“You should see Tarkwa,” said a man to me, who was much of my
way of thinking, “when an alcoholic wave has passed over it!”
Eduaprim was another mine I went to see from Tarkwa. But it was
in direct contrast to Prestea, though it too was in the heart of the
forest country. No railway led to it; I had to go by hammock, and so
I got my first taste of forest travelling, and enjoyed it immensely.
It is a solitary mine about nine miles from Tarkwa, and I started
off early in the morning, and noticed as I went that the industry is,
for good or ill, clearing the forests of West Africa, opening up the
dark places, even as it did in my country over fifty years ago. Along
the hillsides we went to Eduaprim, past mines and clearings for
mining villages; sometimes the road was cut, a narrow track on the
side of the hill, with the land rising up on one side and falling sheer
on the other, sometimes a little river had to be bridged, and the road
went on tunnel-like through the forest that must disappear before
the furnaces, but at last I arrived at the top of the hill, and on it,
commanding a wonderful view over the surrounding country, stood a
bungalow, in a garden that looked over the tops of range upon
range of high hills. I saw a storm come sweeping across the country,
break and divide at the hilltop upon which I stood, and pass on,
veiling the green hills in mist, which rolled away from the hills
behind, leaving them smiling and washed and clean under a blue
sky. If for no other sight than that, that journey into the hills was
worth making.
The wife of the manager of the mine was a fellow-countrywoman
of mine. She liked West Africa, kept her health there, and felt
towards it very much as I did. No one likes great heat. The
unchanging temperature is rather difficult to bear for one
unaccustomed to it, but she thought it might be managed by a
woman interested in her work and her husband, and as for the other
discomforts—like me, she smiled at them. “The people who grumble
should live in Australia,” said she, “and do their own work, cooking,
washing, scrubbing. Do it for a week with the temperature averaging
100 degrees in the shade, and they wouldn't grumble at West Africa,
and wouldn't dream of being sick.” And yet this contented woman
must have led a very lonely life. Some wandering man connected
with the mines, or a stray Commissioner, would come to see her
occasionally, and the news of the world would come on men's heads
from Tarkwa. And, of course, I suppose there was always the mine,
which was her husband's livelihood. They took me into the bush
behind the bungalow and showed me a great mahogany tree they
had cut down, and then they showed me what I had seen many and
many a time in my life before, but never in Africa—men washing the
sand for gold. They were “dollying” it first, that is crushing the hard
stone in iron vessels and then washing it, and the “show,” I could
see for myself, was very good.
I lingered in Eduaprim; the charm of talking with a woman who
found joy in making a home in the wilderness was not to be lightly
foregone, and I only went when I remembered that it was the rainy
season, the roads were bad, and Tarkwa was away over those
forbidding hills.
And from Tarkwa I went up the line to Obuasi.
This railway line that runs from Sekondi to Kumasi, the capital of
Ashanti, is a wonderful specimen of its class. Every day sees some
improvement made, but, being a reasonable being, I cannot help
wondering what sort of engineers laid it out. It presents no
engineering difficulties, but it was extremely costly, and meanders
round and round like a corkscrew. They are engaged now in
straightening it, but still they say that when the guard wants a light
for his pipe all he has to do is to lean out of his van and get it from
the engine. It was laid through dense forest, but the forest is going
rapidly, the trees being used up for fuel. In the early days, too, these
trees were a menace, for again and again, when a fierce tornado
swept across the land, the line would be blocked by fallen trees, a
casualty that grows less and less frequent as the forest recedes.
When first the line was opened they tell me all passengers were
notified that they must bring food and bedding, as the company
could not guarantee their being taken to their destination. There is
also the story of the distracted but pious negro station-master, who
telegraphed to headquarters, “Train lost, but by God's help hope to
find it.” It is a single line of 168 miles, so I conclude his trust in the
Deity was not misplaced.
Obuasi, on the borders of Ashanti, is the great mine of West
Africa, a mine that pays, I think, something like 75 per cent, on its
original shares, and even at their present value pays 12 per cent. It
is enough to set everyone looking for gold in West Africa.
And like Prestea, Obuasi is the mine, and the mine only. There
are, I think, between eighty and one hundred white men, all, save
the few Government officials and storekeepers, in some way or
another connected with the mine, and the place at night looks like a
jewel set in the midst of the hills, for it is lighted by electricity. Every
comfort of civilisation seems to be here, save and except the white
woman, who is conspicuous by her absence. “We want no white
women,” seems to be the general opinion; an opinion, I deeply
regret to say, warranted by my experience of the average English
woman who goes to West Africa.

The place is all hill and valley, European bungalows built on the
hills, embowered generally in charming gardens such as one sees
seldom in the Colony, and the native villages—for there are about
five thousand black men on the books of the mine—in the valleys.
There are miles of little tramway railways too, handling about 35,000
tons a month, more, they tell me, than the Government railway
does, and the mine pays Government a royalty of £25,000 a year.
Obuasi is a fascinating, beautiful place; I should have liked to have
spent a month there, but it is not savagery. It is as civilised in many
ways as London itself. I stayed in the mining manager's bungalow,
and am very grateful to him for his hospitality, and the manager's
bungalow is a most palatial place, set on the top of a high hill in the
midst of a beautiful garden. Palm and mango and grape-fruit trees,
flamboyant, palms, dahlias, corallita, crotons, and roses, the most
beautiful roses in the world, red, white, yellow, pink, everywhere; a
perfect glory of roses is his garden, and the view from the verandah
is delightful. His wide and spacious rooms are panelled with the
most beautiful native woods, and looking at it with the eyes of a
passer-by, I could see nothing but interest in the life of the man who
had put in a year there. He will object strongly, I know, to my writing
in praise of anything West-African, and say what can I know about it
in a brief tour. True enough, what can I know? But at least I have
seen many lands, and I am capable of making comparisons.
Every man I met here pointed out to me the evils of life in Africa.
“You make the very worst of it,” said I, and proceeded to tell the
story of a bridge party in a Coast town that began at three o'clock
on Friday afternoon and ended up at ten o'clock on Monday
morning.
“And if those men have fever,” said I, feeling I had clinched my
argument, “they will set it down to the beastly climate.”
“So it is,” said my opponent emphatically; “we could always do
that sort of thing in Buluwayo.”
I thereby got the deepest respect for the climate of Buluwayo, and
a most doubtful estimate of the character of the pioneer Englishman.
Perhaps I look on these things with a woman's narrow outlook, but
I'm not a bit sorry for the men who cannot dissipate without paying
for it in Africa. I heartily wish them plenty of fever.
The manager took me on a trolley along one of these little lines,
right away into the hills. This was a new form of progression. A seat
for two people was fixed on a platform and pushed along the line,
uphill or on the flat, by three or four negroes, and fairly flew by its
own weight downhill. It was a delightful mode of progression, and as
we flew along, Xi my host, while pointing out the sights,
endeavoured to convert me, not to the faith that West Africa was
unfit for the white woman, that would have been impossible, but
that the mining industry was a very great one and most useful to the
Colony. And here he succeeded.

I admired the forests and regretted their going, but he showed me


the farms that had taken their place. Bananas and maize and
cassada, said he truly enough, were far more valuable to the people
than the great, dark forests they had cleared away—ten people
could live now where one had lived before; and so we rolled on till
we came to the Justice mine, where all the hillside seemed to be
worked, a mine that has been paying £10,000 a month for the last
three years. Truly, it is a wonderful place, that Obuasi mine with its
nine shafts, an industry in the heart of savage Africa. They pay
£11,000 a week in wages, and when I was thinking how closely in
touch it was with civilisation, the manager told me how the chiefs
had just raised a great agitation against the mine because it worked
on Friday, their sacred day. They complained that the snails were so
shocked at this act of sacrilege that they were actually leaving the
district. Now the snails in Ashanti are very important people,
boundaries are always calculated with reference to them, and if a
chief can prove that his men are in the habit of gathering snails over
a certain area, it is proof positive that he holds jurisdiction over that
land. That the snails should leave the district shocked would be a
national calamity. The African snail looks like an enormous whelk, he
haunts the Ashanti forest, and is at his best just at the
commencement of the rains, when he begins to grow fat and
succulent, but is not yet too gross and slimy. He is hunted for
assiduously, and all along the forest paths may be seen men, laden
with sticks on which are impaled snails drawn from their shells,
dried, and smoked. Luckily also these African snails appear to be
very sensible, and when it was put to them that the mines could not
possibly stop working on a Friday, but a small monetary tribute
would be paid to them regularly through the principal chief, they
amiably consented at once to stay and meet their final end, as a
self-respecting snail should, by impalement on a stick.
CHAPTER XXII—A NEW TRADING
CENTRE
The siege of Kumasi—The Governor in 1900—The rebellion—The
friendlies under the walls of the fort—The Ashanti warrior of ten
years ago and the trader of to-day—The chances of the people in the
fort—The retreat—The gallant men who conducted it—The men who
were left behind—The rescue—Kumasi of to-day—The trade that
comes to Kumasi as the trade of Britain came to London in the days
of Augustus—The Chief Commissioner—The men needed to rule
West Africa.

A
nd when I had been to Obuasi nothing remained but to go up
the line and see Kumasi and go as far beyond as the time at
my disposal would allow.
I wonder if English-speaking people have forgotten yet the siege
of Kumasi. For me, I shall never forget, and it stands out specially in
my mind because I know some of the actors, and now I have seen
the fort where the little tragedy took place; for, put it what way you
will, it was a tragedy, for though the principals escaped, some with
well-merited honour, the minor actors died, died like flies, and no
man knoweth even their names.
It was dark when I reached Kumasi and got out on to the platform
and was met by the kind cantonment magistrate, put into a
hammock, and carried up to the fort, and was there received by the
Chief Commissioner and his pretty bride, one of the two white
women who make Kumasi their home, I had seen many forts, old
forts along the Coast, but this fort was put up in 1896, and in 1900
its inmates were fighting for their lives. In it were shut up the
Governor, his wife, two or three unfortunate Basel missionary
women, a handful of troops, and all the other white people in the
place. Standing on the verandah overlooking the town to-day, with a
piano playing soft music and a dining-table within reach set out with
damask and cut-glass and flowers and silver, it is hard to believe that
those times are only ten years back. I have heard men talk of those
days, and they are reticent; there are always things it seems they
think they had better not tell, and I gather that the then Governor
was not very much beloved, and that no one put much faith in him.
The rebellion started somewhere to the north, and by the time it
reached Kumasi it was too late to fly, for it was a good eight days'
hard march to the Coast through dense forest. The nearest possible
safety outside that fort lay beyond the River Prah, at least three or
four days' march away. Every white man and many of the black who
were not Ashantis had taken refuge in the fort, which was crowded
to suffocation, and outside, in front of the fort, camped the
friendlies, safe to a certain extent under the white man's guns, but
dying slowly because the white man could not give what he had not
got himself—food; and here they died, died of disease and hunger
and wounds, and the reek of their dying poisoned the air so that the
white man, starving behind his high walls of cement, was like to
have his end accelerated by those who stood by him.
And out beyond, where the English town now stands, with broad
streets planted with palms and mangoes and ficus, were the
encampments of fierce Ashanti warriors, their cloths wound round
their middles, their hair brushed fiercely back from their foreheads,
their powder-flasks and bullet-bags slung across their shoulders, and
their long Danes in their hands, the locks carefully covered with a
shield of pigskin. The same man, very often the very same
individual, walks about the streets of Kumasi to-day, and if he wears
a tourist cap and a shirt, torn, ragged, and dirty, he is at least a
peaceful citizen, and ten years hence he will probably, like the
Creoles in Sierra Leone, be talking of “going home.” But it was
ghastly in the fort then. It was small and it was crowded to
suffocation. The nearest help was at Cape Coast, nigh on 200 miles
away, and between lay the dense forest that no man lightly dared.
The Ashanti too was the warrior of the Coast, and the difficulty was
even to get carriers who would help to move a force against him.
Shut up in the fort there they looked out and waited for help and
waited for death that ever seemed coming closer and closer.
Kumasi is set in a hollow, and round it, pressing in on every side,
was the great forest. Away to the south went the road to Cape
Coast, but it was but a track kept open with the greatest difficulty,
and hidden in the depths of the forest on either hand were these
same warriors. Truly the chances of the people in the fort seemed
small, small indeed. And day after day passed and there was no sign
of help. Provisions were getting low, ammunition was running short,
and from the Ashanti no mercy could be expected. It was war to the
death. Any man or woman who fell into their hands could expect
nothing but torture. I gather that his advisers would have had the
Governor start for the Coast at once on the outbreak of hostilities,
but he could not make up his mind, and lingered and lingered,
hoping for the help that did not, that could not come. No one has
ever had a word of praise for that Governor, though very gallantly
the men under him came out of it. Starvation and death stared them
all in the face; the gallant little garrison, heavily handicapped as it
was, could certainly hold out but little longer, and the penalty of
conquest was death—death, ghastly and horrible.
At last the Governor gave in and they started, a forlorn little
company, for the River Prah, which had generally set a bound to
Ashanti raids. The Governor's wife was carried in a hammock, but
the Basel missionary women, who had escaped with only the clothes
they stood up in, walked, for the hammock-boys were too weak to
carry them, and they had to tramp through mud and swamp. The
soldiers did their best to protect the forlorn company, the friendlies
crowded after, a tumultuous, disorderly crew fleeing before their
enemies, and those same enemies hung on their flanks, scrambled
through the forest, ruthlessly cut off any stragglers, and poured
volleys from their long Danes into the retreating company. Knowing
the forest, I wonder that one man ever escaped alive to tell the tale;
that the principal actors did, only shows that the Ashanti was not the
practised warrior the Coast had always counted him. Had those
Ashantis been the lean Pathan from the hills of northern India, not a
solitary man would have lived to tell the tale, and the retreat from
Kumasi would have taken its place with some of those pitiful stories
of the Afghan Border. But one thing the Ashanti is not, he is not a
good marksman. He blazes away with his long Dane, content to
make a terrific row without making quite sure that every bullet has
reached its billet. And so, thanks to the bad marksmanship of the
Ashantis, that little company got through.
But let no man think I am in any way disparaging the men who
fought here, who by their gallantry brought the Governor and his
wife through. Major Armitage and his comrades were brave men of
whom England may well be proud, men worthy to take their places
beside Blake and Hawkins and all the gallant Britons whose names
are inscribed on the roll of fame; they fought against desperate
odds, they were cruelly hampered by the helpless people under their
care, and they stuck beside them, though by so doing they risked
not only death, but death by ghastly torture. Some of them died,
some of them got through—they are with us still, young men, men
in the prime of life—and when we tell our children tales of the way
England won her colonies, we may well tell how that little company
left the fort of Kumasi, every man who was wise with cyanide of
potassium in his pocket, and fought his way down to the Prah.
But even though they went south they were not going to abandon
Kumasi, which had been won at the cost of so much blood, and in
that fort were left behind three white men and a company of native
soldiers. All in good time the relief must come, and till then they
must hold it.
A verandah hangs round the fort nowadays that the piping times
of peace have come, but still upstairs in the rooms above are the
platforms for the gun-carriages, and I climbed up on them and
walked along the verandahs and wondered how those men must
have felt who had looked out from the self-same place ten years
ago. If no help came, if waiting were unduly prolonged, they would
die, die like rats in a hole, and the men in their companies were
dying daily. They were faithful, those dark soldiers of the Empire, but
they were dying, dying of disease and hunger, and their officers
could not help them, for were they not slowly dying themselves?
Rumours there were of the relief force, but they were only rumours,
and the spectres of disease and starvation grew daily. Could they
hold out? Could they hold out? The tale has been told again and
again, and will probably be told yet again in English story, and at last
when they had well-nigh given up to despair they heard the sound
of English guns, so different from the explosions of the long Danes,
and presently there was the call of the bugles, and out into the open
trotted a little fox terrier, the advance guard of the men who had
come to save Kumasi.
And now the change. Kumasi has a train from the coast port of
Sekondi every day, it has a population that exceeds that of the
capital of the Gold Coast itself, every day the forest is receding and
in the streets are growing up great buildings that mark only the
beginning of a trade that is already making the wise wonder how it
was when wealth lay on the ground for the picking up, England, who
had it all within her grasp, was amiable enough to allow the greater
portion of this wonderful land to fall to the lot of the French and
Germans.
The forest used to close Kumasi in on every side. It is set in a
hollow, and the tall trees and luxuriant green in the days that I have
just spoken of threatened to overwhelm it. Now that sensation has
passed away. Whatever Kumasi may be in the future, to-day it is a
busy centre of life and trade. Where the fetish tree stood, the
ground beneath its branches soaked with human blood and strewn
with human bones, is now the centre of the town where the great
buildings of the merchant princes of West Africa are rising. They are
fine, but they are a blot on the landscape for all that. The nation
that prides itself on being the colonising nation of the earth never
makes any preparation for the expansion of its territory or the
growth of its trade, so here in this conquered country, bought at the
cost of so much sweat and blood, the authorities are allowing to go
up, in the very heart of the town buildings, very handsome buildings
without doubt, so close together that in a tropical land where fresh
air is life itself they are preparing to take toll of the health of the
unfortunates who will have to dwell and work there. But beyond that
one grave mistake Kumasi promises to be a very pretty place as well
as a very important one. Its wide, red roads, smooth and well-kept,
are planted with trees, mangoes and palms; its bungalows are set
well apart, surrounded by trees and shrubs and lawns, their red-
brown roofs and verandahs toning picturesquely with the prevailing
green.

Curious it is when one thinks of its history to see the white painted
sign-posts on which are recorded the names of the streets. There is
“Kingsway” for one, and “Stewart-avenue,” after the man who
deeply loved the country, for another, and there are at least two
great roads that lead away to the fruitful country in the north, roads
that push their way through the dense forest and must even compel
the admiration of our friends the Germans, those champion road-
makers. And down those roads comes all the wonderful trade of
Kumasi, not as the trade of London, of course, but as the trade of
London was, perhaps, when Augustus ruled at Rome. The trade of
the world comes to London nowadays, the trade of the back-country
came to London then, and so does the trade of all the country round
come to the Ashanti capital. Its streets are thronged with all manner
of peoples, dark, of course, for the ruling whites are but an
inconsiderable handful, and only the Chief Commissioner and one
missionary have been daring enough to bring their wives.
Ashanti is a conquered country, and it seems to me it has got just
the right sort of Government, a Government most exactly suited to
the requirements of the negro in his present state of advancement.
What a negro community requires is a benevolent despotism, but as
a rule the British Government, with its feeling for the rights of the
individual, does not see its way to give it such a Government. But
Ashanti was conquered at great cost, wherefore as yet England has
still to think of the rights of the white men who dwell there as
against the rights of the black man, and the result to me, an
onlooker, appears to be most satisfactory for both white and black.
Of course, such a Government requires to administrate not only
excellent men, not only honest and trustworthy men, but men who
have the interests of the country at heart, and who devote
themselves to it, and such men she has got in the Chief
Commissioner, Mr Fuller, and the subordinates chosen by him. Only
an onlooker am I, a woman, a passer-by, but as a passer-by I could
not but be struck by the difference between the feeling in the Gold
Coast Colony and the feeling in Ashanti. The whole tone of thought
was different. Everywhere on the Gold Coast men met me with the
question, “What did I think of this poisonous country? Wasn't it a
rotten place?” and they seemed bitterly disappointed if I did not
confirm their worst blame.
But in Ashanti it was different. The very clerks in the mercantile
houses had some good word to say for the country, and were
anxious that I should appreciate it and speak well of it, and this I
can but set down to the example and guidance of such men as the
Chief Commissioner and the men he chooses to serve under him.
Had the rest of West Africa always had such broad-minded, clever,
interested men at the head of affairs, I think we should have heard a
great deal less about its unhealthiness and a great deal more about
the productiveness of the country. Since I have seen German
methods I am more than thankful that I have been to Ashanti and
learned that my own country is quite equal to doing as well, if not
beating them at their own methods. The Ashanti himself, the
truculent warrior of ten years ago, has under the paternal and
sympathetic Government of this Chief Commissioner become a man
of peace. If he has not beaten his long Dane gun into a ploughshare
he has at least taken very kindly to trade and is pleased, nay eager
that the white man should dwell in his country. He stalks about
Kumasi in his brightly coloured, toga-like cloth still, very sure that he
is a man of great importance among the tribes, and his chiefs march
through the streets in chairs on men's heads, with tom-toms
beating, immense gaily coloured umbrellas twirling, their silken'
cloths a brilliant spot in the brilliant sunshine, their rich gold
ornaments marking them off from the common herd, and all their
people who are not Christian still give them unquestioned devotion.
But Kumasi, as I said, is the centre of a great trade, and the native
town, which is alongside but quite apart from the European town, is
packed with shops, shops that are really very much in the nature of
stalls, for there are no fronts to them, and the goods are exposed to
the street, where all manner of things that are attractive to the
native are set out.
And here one gathers what is attractive to the native. First and
foremost, perhaps, are the necessities of life, the things that the
white man has made absolute necessaries. First among them, I
think, would be kerosene and bread, so everywhere, in market-place
and shop, or even just outside a house, you may see ordinary wine
and whisky bottles full of kerosene, and rows and rows of loaves of
bread. Then there comes men's clothing—hideous shirts and uglier
trousers, tourist caps that are the last cry in hooliganism, and boots,
buttoned and shiny, that would make an angel weep. Alas! and alas!
The Ashanti in his native state, very sure of himself, has a certain
dignity about him even as must have had the old Roman. You might
not have liked the old Roman, probably you would not unless he
chose to make himself pleasant, but you could not but recognise the
fact that he was no nonentity, and so it is with the Ashanti till he
puts on European garments. Then how are the mighty fallen! for like
all negroes, in the garb of civilisation, he is commonplace when he is
not grotesque. What they are to wear I cannot say, but the better-
class among them seem to realise this, for I have often heard it said,
not only in Ashanti but in other parts of the Coast: “The Chief may
not wear European clothes.”
And beside clothes in the native shops are hurricane lanterns,
ordinary cheap kerosene lamps, and sewing machines which the
men work far more often then the women, accordions, mouth
harmoniums, and cotton goods in the strange and weird patterns
that Manchester thinks most likely to attract the native eye. I have
seen brooms and brushes and dustpans printed in brilliant purple on
a blue ground, and I have seen the outspread fingers of a great
hand in scarlet on a black ground. But mostly there is nothing of
very great interest in these shops, just European goods of the
commonest, cheapest description supplied apparently with the view
of educating the native eye in all that is ugliest and most
reprehensible in civilisation.
There are horses in Kumasi, for the forest and undergrowth have
been cleared away sufficiently to destroy the tsetse fly, and so most
evenings, when the heat of the day has passed, the Chief
Commissioner and his wife go for a ride, and on occasions many of
the soldiermen play polo and hold race-meetings, but as yet there is
no wheeled traffic in the streets. Most of the goods are carried on
men's heads, and the roadways are crowded. There are women with
loads on their heads and generally children on their backs, walking
as if the world belonged to them, though in truth they are little
better than their husbands' slaves. There are soldiers all in khaki,
with little green caps like condensed fezes, lor the place is a great
military camp and the black soldier swaggers through the street;
there are policemen in blue uniforms with red fezes, their feet bare
like those of the soldiers, and their legs bound in dark-blue putties;
and there are black men from all corners of West Africa. There are
the Kroo boys, those labourers of the Coast, with the dark-blue
freedom mark tattooed on their foreheads, never carrying anything
on their heads, but pushing and pulling heavily laden carts, in gangs
that vary from four to a dozen, and their clothing is the cast-off
clothing of the white man; there are Hausas and Wangaras, than
whom no man can carry heavier loads, and they wear not a flowing
cloth like the Ashanti, but a long, shirt-like garment not unlike the
smock of the country labourer. It is narrower and longer, but is
usually decorated with the same elaborate needlework about the
neck and shoulders; if their legs are not bare they wear Arab
trousers, full above and tight about their feet, and the flapping of
their heelless slippers makes a clack-clack as they walk. There are
Yorubas, dressed much the same, only with little caps like a child's
Dutch bonnet, and there are even men from the far north, with blue
turbans and the lower part of their faces veiled. Far beyond the
dense forest lies their home, away possibly in French territory, but
the trade is coming to this new city of the Batouri, and they wander
down with the cattle or horses. For all the cattle and horses come
down through the forest, driven hastily and fast because of the
deadly tsetse, and many must perish by the way. A herd of the
humped, long-horned cattle come wearily through the streets.
Whatever they may have been once, there is no spirit left in them
now, for they have come down that long road from the north; they
have fed sparely by the way, and they are destined for the feeding of
the population that are swarming into Kumasi to work the mines in
the south.
Three towns are here in Kumasi: the European quarter, the
Ashanti town, and the Mohammedan town or zonga. Here all the
carrying trade that is not done by Government is arranged for—by a
woman. Here the houses are small and unattractive, nondescript
native huts built by people who are only sojourners in the land,
come but to make money, ready to return to their own land in the
north the moment it is made. And they sit by the roadside with little
things to sell. Food-stuffs often, balls of kenki white as snow, yams
and cassada, which is the root of which we make tapioca, cobs of
Indian corn, and, of course, stink-fish that comes all the way from
the Coast and is highly prized as a food, and does not appear to
induce ptomaine poisoning in African stomachs. Some of these
dainties are set out on brass trays made in Birmingham; others on
wooden platters and on plates delicately woven in various patterns
of grass dyed in many colours. But most things they have they are
ready to sell, for the negro has great trading instincts, and that
trading instinct it is that has made him so easy to hold once he is
conquered.
Kumasi is peaceful enough now, and the only reminder of the bad
days of ten years back is the fort just above the native town, but it
looks down now across a smooth green lawn, on which are some
great, shady trees, where chiefs assembled whom I photographed.
One was a great fetish chief with gold ornaments upon his head and
upon his feet, and knowledge of enough magic, had this been the
fifteenth century instead of the twentieth, to drive the white man
and all his following back to the sea from whence he came; but it is
the twentieth, and he is wise enough to know it, and he flings all the
weight of his authority into the scales with the British raj. But at the
gate of the fort still stands a guard of black soldiers in all the glory
of scarlet and yellow which stands for gold, for the Chief
Commissioner lives here, and in a land where a chief is of such
importance it is necessary to keep up a certain amount of state, and
the Chief Commissioner ruling over this country and receiving
obeisance from the chiefs, clad in their gorgeous silken cloths, laden
with golden jewellery, men looked up to by their followers as half-
divine, must feel something like a Roman proconsul of old carrying
the eagles into savage lands, and yet allowing those savages as far
as possible to govern themselves by their own laws. Africa has
always been the unknown land, but now at last the light is being let
into dark places, the French have regenerated Dahomey, and the
railway comes to Kumasi. I sat on that verandah and thought of the
old days that were only ten years back, and learned much from the
Commissioner, and I felt that civilisation was coming by leaps and
bounds to Ashanti, and if it be true, as old tradition has it, that a
house to be firmly built must have a living man beneath its
foundation stone, then must the future of Kumasi be assured, for its
foundations were well and truly laid in rivers of human blood.
CHAPTER XXIII—IN THE HEART OF
THE RUBBER COUNTRY
Bound for Sunyani—The awe-inspiring-forest—The road through
the forest—The people upon that road—Ofinsu and an Ashanti house
—Rather a public bedroom—Potsikrom—A night of fear—Sandflies—
Attractive black babies—A great show at Bechem—A most important
person—The Hausa who went in fear of his life—Coronation night at
Tanosu—A teetotal party—The medical officer's views on trees—
Beyond the road—Sunyani.

I
talked to the Commissioner, and those talks with him made me
want to go somewhere out into the wilds. Kumasi was beginning
to look strangely civilised to me. It was a great trading-centre,
and presently it would be as well known, it seemed to me, as
Alexandria or Cairo, or at the other end of the Continent, Buluwayo.
I should like to have gone into the Northern Territories, but the rainy
season was upon us, and if that did not daunt me—and it would not
have done so—I had to consider the time. I ought to be back in
London. I had intended to be away for six months, and now it was
close on eight since I had come out of the mouth of the Mersey.
“Go to Sunyani,” said the Chief Commissioner, “and go on to
Odumase, where the rising began at the beginning of the century.
You will be the first white woman to go there, and I think you will
find it worth your while.”
So I interviewed the head of the transport service, and by his
kindness was supplied with seventeen carriers, and one hot day in
June started north.
They had doubts, these kind friends of mine, about my capabilities
as a traveller, at least they feared that something might happen to
me while I was in their country, and they told me that a medical
officer was starting north for Sunyam that day and would go with
me.
I looked up the medical officer and found him in the midst of
packages that he was taking with him beyond civilisation to last for a
year. He was most courteous, but it seemed to me that he felt the
presence of a woman a responsibility, and I was so sure of myself,
hated to be counted a nuisance, that when he said he had intended
to go only as far as Sansu that night, I expressed my intention of
going on to Ofinsu, and hinted that he might catch me up next
morning if he could.
So by myself I set out into the heart of the rubber country north
of Kumasi. I was fairly beyond civilisation now. Ten years ago this
country was in open rebellion against English rule, and even now
there are no European stores there; there is no bread, no kerosene,
no gin—those first necessities of an oncoming civilisation; it was
simply the wild heart of the rubber country, unchanged for hundreds
of years. It has been known, but it has not been lightly visited. It
has been a country to be shunned and talked of with bated breath
as “the land of darkness.” The desert might be dared, the surf might
be ventured, the black man might be defied, but the gloom of the
forest the white man feared and entered not except upon
compulsion. The Nile has given up its secrets, the Sahara yields to
cultivation, but still in Africa are there places where the all-
conquering white man is dwarfed, and one of them is the great
forest that lies north of the capital of Ashanti.
Here we know not the meaning of the word forest. England's
forests are delightful woods where the deer dwell in peace, where
the rabbits scutter through the fern and undergrowth, and where the
children may go for a summer's holiday; in Australia are trees close-
growing and tall; but in West Africa the forest has a life and being of
its own. It is not a thing of yesterday or of ten years back or of fifty
years. Those mighty trees that dwarf all other trees in the world
have taken hundreds of years to their growth. When a slight young
girl came to the throne of England, capturing a nation's chivalry by
her youth and innocence, the mahogany and kaku and odoum trees
were old and staid monarchs of the forest. When the first of the
Georges came over from Hanover, unwelcome, but the nation's last
hope, they were young and slim but already tall trees stretching up
their crowns to the brilliant sunlight that is above the gloom, and
now at last, when the fifth of that name reigns over them, at last is
their sanctuary invaded and the seclusion that is theirs shall be
theirs no longer. For already the axe is laid to their roots, and
through the awe-inspiring forest runs a narrow roadway kept clear
by what must be almost superhuman labour, and along that
roadway, the beginning of the end, the sign that marks the peaceful
conquest of the savage, that marks also the downfall of the forest
though it is not even whispered among the trees that scorn them
yet, flows a perpetual stream of traffic, men, women, and children.
Backwards and forwards from the north to Kumasi and the sea they
come, and they bear on their heads, going north, corrugated iron
and cotton goods, kerosene, and flour, and chairs, all the trifles that
the advance of civilisation makes absolute necessaries; and coming
down they bring all in their season, hides, and heavy cakes of
rubber, and sticks of dried snails, and all the other articles of native
produce that a certain peace has made marketable along the way or
in the markets of Kumasi.
The spell was upon me the moment I left the town. That road is
like nothing else in the world. The hammock and the carriers were
dwarfed by the great roots and buttresses of the trees to tiny,
crawling ants, and overhead was a narrow strip of blue sky where
the sunlight might be seen, but only at noon did that sunlight reach
the roadway below. We travelled in a shadow pleasant in that heat;
and on either side, close on either side, were the great trees.
Looking down the road I could see them straight as a die, tall pillars,
white and brown; ahead of me and close at hand the mighty
buttresses that supported those pillars rose up to the height of
perhaps ten men before the tree was fairly started, a tall trunk with
branches that began to spread, it seemed to me, hundreds of feet
above the ground. And between those tree-trunks was all manner of
undergrowth, and all were bound and matted together with thickly
growing creepers and vines. It was impossible to step an inch from
that cleared path. There would be no getting lost in the bush, for it
would be almost impossible for the unpractised hand to get into the
bush. There is nothing to be seen but the brown, winding roadway,
the dense green of the undergrowth, and the trunks of the trees tall
and straight as Nelson's column and brown or white against the
prevailing green. And there are all shades of green, from that so
pale that it is almost golden to that so dark it is almost black, but
never a flower breaks the monotony, the monotony that is not
monotony but dignity, and the flowers of an English spring or an
autumn in Australia would but cheapen the forest of the Gold Coast.
There must have been orchids, for sometimes as I passed their rich,
sensuous smell would come to my nostrils, but I only knew they
were there by my sense of smell just as sometimes I smelt a strong
smell of mice, and knew, though I could not see them, that
somewhere in the depths of the gloom were hidden away a great
colony of fruitarian bats that would not come out into the daylight.

When there was a village there was, of course, a clearing, and on


the first day I passed several villages until at last I came to Ofinsu,
where I had arranged to spend the night. Ofinsu is on the banks of a
river, and the road comes out of the forest and passes broadly
between two rows of mud-walled houses with steeply pitched, high-
thatched roofs, and my carriers raced along and stopped opposite a
small wooden door in a mud wall and rapped hard.
For the first time on my travels I had really excellent carriers. They
were Krepis from beyond the German border, slight, dark men with
slim wrists and ankles, and crosses cut as tribal marks on each
cheek, and they were cheerful, smiling, willing. When I remembered
my before-time tribulations I could hardly believe these were
actually carriers who were going along so steadily and well, who
were always up before me in the morning, and in as soon as I was
at night, who never lingered, never grumbled, never complained, but
were simply ideal servants such as I had never had before in my life
save perhaps for a day, as when I went to Palime from Ho, and such
as I shall count myself extremely lucky if I ever have again.
“We have got good carriers,” the transport officer had said,
“though you don't seem to believe it”; and he proved his words, for
never have I travelled more comfortably than I did on that one
hundred and sixty miles to Sunyani and back.
The knocking at the little door brought a black lady with a shaven
head and a blue cloth wrapped round her middle. She was a woman
past all beauty, and very little was left to the imagination, but she
threw open the door and indicated that we were to enter, and she
looked at me very curiously. Never before had a white woman come
to Ofinsu.
I entered, and this was my first introduction to an Ashanti house,
a house that seems to me singularly suited to the climate and
people. It is passing away, they tell me, and I for one am sorry.
We went into a courtyard open to the sky, and round it, raised at
least two feet from the ground, were the rooms, I suppose I must
call them, but though there was a roof overhead and walls on three
sides, walls without windows, the fourth side was open to the central
courtyard. When I entered the place was crowded; Hausas or
Wangaras—I never could tell one from the other—were settled down
on the platforms, and their loads—long bundles made up for
carrying on the head—were all over the place. I said nothing. I am
generally for the superiority of the white man and exact all the
deference that is my due, but clearly these people were here first,
and it seemed to me they had it by right, only how I was to bathe
and sleep in a house where everything was so public among such a
crowd I did not know.

But my hostess had other views. No sooner had I entered than


she began clearing out the former guests, and in less than a quarter
of an hour the place that had seemed so crowded was empty, swept
and garnished for my accommodation. My bed was put up on one
platform, my table and chair on another. “Get table quick and chair,
so can play cards,” Grant instructed my headman, and behind,
through a little door that may be seen in the picture, was a place
that answered for a kitchen, and a cup of tea was quickly produced
for my comfort. It was weird going to sleep there in the open, but it
was very, very delightful. I rigged up in the corner of one of the
rooms—I have no other names for them—with ground sheet and
rugs, a little shelter where I could have my bath in comfort, but I
undressed without a qualm and went to bed and slept the sleep of
the woman who has been in the open air the livelong day and who,
happily for herself, can indulge her taste and sleep in the open air all
night.
I took a picture of my open-air bedroom with my valuable
headman and two small children who belonged to the household I
had invaded in the foreground. But that was before I went to bed at
night. At earliest dawn, before the dawn in fact, my headman was at
my bedside wanting to pack up and start.
That night's lodging cost me one shilling and threepence. The
headman told me one shilling was enough, so I bestowed the extra
threepence as a dash on the shaven old woman who had done all
for me that my servants could not do, and she seemed so delighted
that I was left wondering what the Wan-garas who had given place
to me had paid.
Just as the sun was rising we crossed the Ofin River, and I found
there assembled the entire population of the village to look at the
strange sight—a perfectly courteous, polite people who never
crushed or crowded though they looked their fill. I can only hope I
was a success as a show, for certainly I attracted a great deal of
attention, but of course I had no means of knowing whether I came
up to expectations. It took some time to get my goods and followers
across the river in the crank canoe which is only used in the rainy
season, for usually the Ofin River can be waded, and while I waited
on the farther shore I looked with interest at the other people who
were waiting for their loads to be ferried across.
The men were Hausas or Wangaras, some wearing turbans, some
with shaven heads, and clad in long, straight, shirt-like garments,
while the women excited my deepest compassion. They may have
been the men's wives, I know not; but by whatever name they were
called they were slaves if ever I saw slaves. They had very little on
besides a dirty, earthen-coloured cloth hitched round their loins,
their dark faces were brutalised and depressed with that speechless
depression that hardly realises its own woes, and their dusty hair
that looked as if it had not been washed for years was generally
twisted into short, thick, dusty looking plaits that were pressed
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!

ebookultra.com

You might also like