Developing Web Applications with Apache MySQL memcached and Perl 1 Original Edition Patrick Galbraith download pdf
Developing Web Applications with Apache MySQL memcached and Perl 1 Original Edition Patrick Galbraith download pdf
com
https://ebookname.com/product/developing-web-applications-
with-apache-mysql-memcached-and-perl-1-original-edition-
patrick-galbraith/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/beginning-php-apache-mysql-web-
development-michael-k-glass/
ebookname.com
Sams Teach Yourself PHP MySQL and Apache All in One Julie
C. Meloni
https://ebookname.com/product/sams-teach-yourself-php-mysql-and-
apache-all-in-one-julie-c-meloni/
ebookname.com
https://ebookname.com/product/web-graphics-perl-tk-best-of-the-perl-
journal-1st-edition-jon-orwant-ph-d/
ebookname.com
https://ebookname.com/product/voices-postgraduate-perspectives-on-
inter-disciplinarity-1st-edition-kathryn-vincent/
ebookname.com
Introduction to Probability and Statistics for Engineers
and Scientists Third Edition Sheldon M. Ross
https://ebookname.com/product/introduction-to-probability-and-
statistics-for-engineers-and-scientists-third-edition-sheldon-m-ross/
ebookname.com
https://ebookname.com/product/which-contract-choosing-the-appropriate-
building-contract-sixth-edition-lupton/
ebookname.com
https://ebookname.com/product/1-001-best-slow-cooker-recipes-the-only-
slow-cooker-cookbook-you-ll-ever-need-sue-spitler/
ebookname.com
https://ebookname.com/product/recursive-macroeconomic-theory-third-
edition-lars-ljungqvist/
ebookname.com
Tarascon Pediatric Emergency Pocketbook 7th Edition
Mahajan
https://ebookname.com/product/tarascon-pediatric-emergency-
pocketbook-7th-edition-mahajan/
ebookname.com
Developing Web Applications with Perl,
memcached, MySQL® and Apache
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 1: LAMMP, Now with an Extra M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 3: Advanced MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Chapter 4: Perl Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Chapter 5: Object-Oriented Perl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Chapter 6: MySQL and Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Chapter 7: Simple Database Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Chapter 8: memcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Chapter 9: libmemcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Chapter 10: Memcached Functions for MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Chapter 11: Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Chapter 12: Contact List Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Chapter 13: mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Chapter 14: Using mod_perl Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Chapter 15: More mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Chapter 16: Perl and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
Chapter 17: Search Engine Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Appendix A: Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Appendix B: Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Developing Web Applications with Perl,
memcached, MySQL® and Apache
Patrick Galbraith
ISBN: 978-0-470-41464-4
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be
addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)
748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought.
Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or
Web site is referred to in this work as a citation and/or a potential source of further information does not mean that
the author or the publisher endorses the information the organization or Web site may provide or recommendations
it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or
disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
To my wonderful wife, Ruth, whom I have known for 27 years and who has stood by me while writing
this book, even when I couldn’t give her the time she deserved. Also, to my dear friend Krishna,
who gave me inspiration every day.
Credits
Acquisitions Editor Vice President and Executive Group
Jenny Watson Publisher
Richard Swadley
Project Editor
Maureen Spears Vice President and Executive Publisher
Barry Pruett
Technical Editor
John Bokma Associate Publisher
Jim Minatel
Production Editor
Rebecca Coleman Project Coordinator, Cover
Lynsey Stanford
Copy Editor
Sara E. Wilson Proofreader
Corina Copp, Word One
Editorial Manager
Mary Beth Wakefield Indexer
Robert Swanson
Production Manager
Tim Tate
About the Author
Patrick Galbraith lives up in the sticks of southwestern New Hampshire near Mt. Monadnock with
his wife, Ruth. Since 1993, he has been using and developing open source software. He has worked on
various open source projects, including MySQL, Federated storage engine, Memcached Functions for
MySQL, Drizzle, and Slashcode, and is the maintainer of DBD::mysql. He has worked at a number of
companies throughout his career, including MySQL AB, Classmates.com, OSDN/Slashdot. He currently
works for Lycos. He is also part owner of a wireless broadband company, Radius North, which provides
Internet service to underserved rural areas of New Hampshire. His web site, which comes by way of a
5.8GHz Alvarion access unit up in a pine tree, is http://patg.net.
One weekend in 1993, I had the chance to go on a getaway to San Diego. Instead, I opted to stay home
and download, onto 26 floppies, Slackware Linux, which I promptly installed onto my Packard Bell 386.
I could never get the built-in video card to work with X, so I ended up buying a separate video card and
had to edit my XConfig file to get it to work. How much more interesting this was to do than editing a
config.sys and an autoexec.bat! From then on, I was hooked. I worked at Siemens Ultrasound Group in
Issaquah, Washington, at the time. An engineer there named Debra, when asked what was a good thing
to learn, said something I’ll never forget: ‘‘Learn Perl.’’ Debra — you were right!
I always wanted to be a C++ graphics programmer. That didn’t happen because of this thing called the
World Wide Web. I remember Ray Jones and Randy Bentson of Celestial Software showing me a program
called Mosaic, which allowed you to view text over the Internet. Images would be launched using XV.
Everywhere I worked, I had to write programs that ran on the Web, which required me to write CGI in
Perl. So much for my goal of being a C++ programmer — but I consider this a great trade for a great
career. (I did eventually get to write C++ for MySQL!)
I would first like to thank my editor, Maureen Spears, who is not only a great editor, but also a friend.
She gave me much-needed encouragement throughout the writing of this book.
A special thanks goes to John Bokma for his meticulous attention to detail and great knowledge of
Perl — particularly with regard to Perl programming style and convention that I didn’t realize had
changed over the last several years. I was somewhat set in my ways!
Thank you to Jenny Watson, who gave me the opportunity to write this book in the first place!
Thanks to Monty Widenius for creating MySQL and for being a mentor as well as a good friend, and
thanks, Monty, for looking over Chapters 1, 2, and 3! Thanks also to Brian Aker for being another great
mentor and friend, as well as being a software-producing machine with a scrolling page full of open
source software projects that he’s created, including Drizzle and libmemcached. Thanks to Sheeri Kritzer
for her encouragement and for listening to me — she finished her book not too long before I finished
mine, so she understood completely what I was going through.
I’d like to thank my friend, Wes Moran, head of design for Sourceforge, for providing the nice, clean,
simple HTML design I used for many of the examples in this book.
Thanks to Eric Day for his excellent input and review of chapters pertaining to Gearman.
A special thanks to Joaquı́n Ruiz of Gear 6, who provided a lot of input on Chapter 1, as well as Jeff
Freund of Clickability and Edwin Desouza and Jimmy Guerrero of Sun, who put me in touch with others
and were great sources of memcached information.
I would like to thank my current colleagues at Lycos, and former colleagues at Grazr and MySQL, as
well as the team members of Drizzle, for their part in my professional development, which gave me the
ability to write this book. Thanks also to anyone I forgot to mention!
Acknowledgments
Finally, I would like to thank the entire Open Source community. My life would not be the same without
open source software.
There’s a verse in an ancient book, the Bhagavad Gita, that aptly describes how people like Monty
Widenius, Linus Torvalds, Larry Wall, Brian Aker and other leaders within the Open Source community
inspire the rest of us:
‘‘Whatever action a great man performs, common men follow. And whatever standards he sets by exemplary acts,
all the world pursues.’’
x
Contents
Foreword xxv
Introduction xxvii
xii
Contents
Perl Data Types 165
Scalars 165
Arrays 167
Hashes 167
File Handles 168
Type Globs 168
Subroutines 168
Variable Usage 168
References 169
Scalar Usage 173
Array Usage and Iteration 174
Working with Hashes 179
Writing to Files 184
STDOUT and STDERR 184
File Handles to Processes 185
Subroutines 186
Variable Scope 189
Packages 192
Perl Modules 193
Writing a Perl Module 194
@ISA array 197
Documenting Your Module 197
Making Your Module Installable 201
Testing 201
Adding a MANIFEST file 204
CPAN 205
Regex One-Liners 206
Storing Regular Expressions in Variables 207
Regex Optimizations 208
Perl 6 Tidbits 208
Summary 210
xiii
Exploring the Variety of Random
Documents with Different Content
Trade and industry, I say, owed their establishment to the
ambition of princes, who supported and favoured the plan in the
beginning, principally with a view to enrich themselves, and thereby
to become formidable to their neighbours. But they did not discover,
until experience taught them, that the wealth they drew from such
fountains was but the overflowing of the spring; and that an
opulent, bold, and spirited people, having the fund of the prince’s
wealth in their own hands, have it also in their own power, when it
becomes strongly their inclination, to shake off his authority. The
consequence of this change has been the introduction of a more
mild, and a more regular plan of administration. The money
gatherers are become more useful to princes, than the great lords;
and those who are fertile in expedients for establishing public credit,
and for drawing money from the coffers of the rich, by the
imposition of taxes, have been preferred to the most wise and most
learned counsellors.
As this system is new, no wonder if it has produced phenomena
both new and surprizing. Formerly, the power of Princes was
employed to destroy liberty, and to establish arbitrary subordination;
but in our days, we have seen those who have best comprehended
the true principles of the new plan of politics, arbitrarily limiting the
power of the higher classes, and thereby applying their authority
towards the extension of public liberty, by extinguishing every
subordination, other than that due to the established laws.
The fundamental maxim of some of the greatest ministers, has
been to restrain the power of the great lords. The natural inference
that people drew from such a step, was, that the minister thereby
intended to make every thing depend on the prince’s will only. This I
do not deny. But what use have we seen made of this new
acquisition of power? Those who look into events with a political
eye, may perceive several acts of the most arbitrary authority
exercised by some late European sovereigns, with no other view
than to establish public liberty upon a more extensive bottom. And
although the prerogative of some princes be increased considerably
beyond the bounds of the antient constitution, even to such a
degree as perhaps justly to deserve the name of usurpation; yet the
consequences resulting from the revolution, cannot every where be
said, upon the whole, to have impaired what I call public liberty. I
should be at no loss to prove this assertion from matters of fact, and
by examples, did I think it proper: it seems better to prove it from
reason.
When once a state begins to subsist by the consequences of
industry, there is less danger to be apprehended from the power of
the sovereign. The mechanism of his administration becomes more
complex, and, as was observed in the introduction to the first book,
he finds himself so bound up by the laws of his political oeconomy,
that every transgression of them runs him into new difficulties.
I only speak of governments which are conducted systematically,
constitutionally, and by general laws; and when I mention princes, I
mean their councils. The principles I am enquiring into, regard the
cool administration of their government; it belongs to another
branch of politics, to contrive bulwarks against their passions, vices
and weaknesses, as men.
I say, therefore, that from the time states have begun to be
supported by the consequences of industry, the plan of
administration has become more moderate; has been changing and
refining by degrees; and every change, as has been often observed,
must be accompanied with inconveniencies.
It is of governments as of machines, the more they are simple,
the more they are solid and lasting; the more they are artfully
composed, the more they become useful; but the more apt they are
to be out of order.
The Lacedemonian form may be compared to the wedge, the
most solid and compact of all the mechanical powers. Those of
modern states to watches, which are continually going wrong;
sometimes the spring is found too weak, at other times too strong
for the machine: and when the wheels are not made according to a
determined proportion, by the able hands of a Graham, or a Julien le
Roy, they do not tally well with one another; then the machine
stops, and if it be forced, some part gives way; and the workman’s
hand becomes necessary to set it right.
CHAP. XIV.
Security, Ease and Happiness, no inseparable Concomitants of Trade and Industry.
CHAP. XV.
A general View of the Principles to be attended to by a Statesman, who resolves to
establish Trade and Industry upon a lasting footing.
The two preceding chapters I have introduced purposely to serve
as a relaxation to the mind, like a farce between the acts of a
serious opera. I now return to the place where I broke off my
subject, at the end of the twelfth chapter.
It is a great assistance to memory, now and then to assemble our
ideas, after certain intervals, in going through an extensive subject.
No part of it can be treated of with distinctness, without banishing
combinations; and no part of it can be applied to practice, or
adapted to any plan, without attending to combinations almost
infinite.
For this reason nothing can appear more inconsistent than the
spirit which runs through some parts of this book, if compared with
that which prevailed in the first. There luxury was looked on with a
favourable eye, and every augmentation of superfluity was
considered as a method of advancing population. We were then
employed in drawing mankind, as it were, out of a state of idleness,
in order to increase their numbers, and engage them to cultivate the
earth. We had no occasion to divide them into societies having
separate interests, because the principles we treated of were
common to all. We therefore considered the industrious, who are the
providers, and the luxurious, who are the consumers, as children of
the same family, and as being under the care of the same father.
We are now engaged in a more complex operation; we represent
different societies animated with a different spirit; some given to
industry and frugality, others to dissipation and luxury. This creates
separate interests among nations, and every one must be supposed
under the government of a statesman, who is wholly taken up in
advancing the good of those he governs, though at the expence of
other societies which lie round him.
This presents a new idea, and gives birth to new principles. The
general society of mankind treated of in the first book, is here in a
manner divided into two. The industrious providers are supposed to
live in one country, the luxurious consumers in another. The
principles of the first book remain here in full vigour. Luxury still
tends as much as ever to the advancement of industry; the
statesman’s business is only to remove the seat of it from his own
country. When that can be accomplished without detriment to
industry at home, he has an opportunity of joining all the
advantages of antient simplicity, to the wealth and power which
attend upon the luxury of modern states. He may preserve his
people in sobriety, and moderation as to every expence, as to every
consumption, and make them enjoy, at the same time, riches and
superiority over all their neighbours.
Such would be the state of trading nations, were they only
employed in supplying the wants or extravagant consumption of
strangers; and did they not insensibly adopt the very manners with
which they strive to inspire others.
As often, therefore, as we suppose a people applying themselves
to the advancement of foreign trade, we must simplify our ideas, by
dismissing all political combinations of other circumstances; that is to
say, we must suppose the spirit universal, and then point out the
principles which influence the success of it.
We must encourage oeconomy, frugality, and a simplicity of
manners, discourage the consumption of every thing that can be
sold out of the country, and excite a taste for superfluity in
neighbouring nations. When such a system can no more be
supported to its full extent, by the scale of foreign demand
becoming positively lighter; then in order to set the balance even
again, without taking any thing out of the heavy scale, and to
preserve and give bread to those who have enriched the state, an
additional home consumption, proportioned to the deficiency of
foreign demand, must be encouraged. For were the same simplicity
of manners still kept up, the infallible consequence would be a
forced restitution of the balance, by the distress, misery, and at last
extinction of the supernumerary workmen.
I must therefore, upon such occasions, consider the introduction
of luxury, or superfluous consumption, as a rational and moral
consequence of the deficiency of foreign trade.
I am, however, far from thinking that the luxury of every modern
state, is only in proportion to such failure; and I readily admit, that
many examples may be produced where the progress of luxury, and
the domestic competitions with strangers who come to market, have
been the cause both of the decline and extinction of their foreign
trade; but as my business is chiefly to point out principles, and to
shew their effects, it is sufficient to observe, that in proportion as
foreign trade declines, either a proportional augmentation upon
home consumption must take place, or a number of the industrious,
proportioned to the diminution of former consumption, must
decrease. By the first, what I call a natural restitution of the balance
is brought about, from the principles above deduced; by the second,
what I call a forced one.
Here then is an example, where the introduction of luxury may be
a rational and prudent step of administration; and as long as the
progress of it is not accelerated from any other principle, but that of
preserving the industrious, by giving them employment, the same
spirit, under the direction of an able statesman, will soon throw
industry into a new channel, better calculated for reviving foreign
trade, and for promoting the public good, by substituting the call of
foreigners in place of that of domestic luxury.
I hope, from what I have said, the political effects of luxury, or the
consumption of superfluity, are sufficiently understood. These I have
hitherto considered as advantageous only to those classes who are
made to subsist by them; I reserve for another occasion the pointing
out how they influence the imposition of taxes, and how the abuse
of consumption in the rich may affect the prosperity of a state.
So soon as all foreign trade comes to a stop, without a scheme for
recalling it, and that domestic consumption has filled up its place in
consuming the work, and giving bread to the industrious, we find
ourselves obliged to reason again upon the principles of the first
book. The statesman has once more both the producers and the
consumers under his care. The consumers can live without
employment, the producers cannot. The first seldom have occasion
for the statesman’s protection; the last constantly stand in need of
it. There is a perpetual fluctuation in the balance between these two
classes, from which a multitude of new principles arise; and these
render the administration of government infinitely more difficult, and
require superior talents in the person who is at the helm. I shall here
only point out the most striking effects of the fluctuation and
overturn of this new balance, which in the subsequent chapters shall
be more fully illustrated.
1mo. In proportion as the consumers become extravagant, the
producers become wealthy; and when the former become
bankrupts, the latter fill their place.
2do. As the former become frugal and oeconomical, the latter
languish; when those begin to hoard, and to adopt a simple life,
these are extinguished: all extremes are vicious.
3tio. If the produce of industry consumed in a country, surpass the
income of those who do not work, the balance due by the
consumers must be paid to the suppliers by a proportional alienation
of their funds. This vibration of the balance, gives a very correct idea
of what is meant by relative profit and loss. The nation here loses
nothing by the change produced.
4to. When, on the other hand, the annual produce of industry
consumed in a country, does not amount to the value of the income
of those who do not work, the balance of income saved, must either
be locked up in chests, made into plate, lent to foreigners, or fairly
exported as the price of foreign consumption.
5to. The scales stand even when there is no balance on either
side; that is, when the domestic consumption is just equivalent to
the annual income of the funds. I do not pretend to decide at
present whether this exact equilibrium marks the state of perfection
in a country where there is no foreign trade, (of which we are now
treating) or whether it be better to have small vibrations between
the two scales; but I think I may say, that all subversions of the
balance on either side cannot fail to be hurtful, and therefore should
be prevented.
Let this suffice at present, upon a subject which shall be more
fully treated of afterwards. Let us now fix our attention upon the
interests of a people entirely taken up in the prosecution of foreign
trade. So long as this spirit prevails, I say, it is the duty of a
statesman to encourage frugality, sobriety, and an application to
labour in his own people, and to excite in foreign nations a taste for
superfluities as much as possible.
While a people are occupied in the prosecution of foreign trade,
the mutual relations between the individuals of the state, will not be
so intimate as when the producers and consumers live in the same
society; such trade implies, and even necessarily creates a chain of
foreign dependencies; which work the same effect, as when the
mutual dependence subsisted among the citizens. Now the use of
dependencies, I have said, is to form a band of society, capable of
making the necessitous subsist out of the superfluities of the rich,
and to keep mankind in peace and harmony with one another.
Trade, therefore, and foreign communications, form a new kind of
society among nations; and consequently render the occupation of a
statesman more complex. He must, as before, be attentive to
provide food, other necessaries and employment for all his people;
but as the foreign connections make these very circumstances
depend upon the entertaining a good correspondence with
neighbouring nations, he must acquire a proper knowledge of their
domestic situation, so as to reconcile, as much as may be, the
interests of both parties, by engaging the strangers to furnish
articles of the first necessity, when the precious metals cannot be
procured; and to accept, in return, the most consumable
superfluities which industry can invent. And, last of all, he must
inspire his own people with a spirit of emulation in the exercise of
frugality, temperance, oeconomy, and an application to labour and
ingenuity. If this spirit of emulation is not kept up, another will take
place; for emulation is inseparable from the nature of man; and if
the citizens are not made to vie with one another, in the practice of
moderation, the wealth they must acquire, will soon make them vie
with strangers, in luxury and dissipation.
While a spirit of moderation prevails in a trading nation, it may
rest assured, that in as far as it excels the nations with whom it
corresponds in this particular, so far will it increase the proportion of
its wealth, power, and superiority, over them. These are lawful
pursuits among men, when purchased by success in so laudable an
emulation.
If it be said, that superfluity, intemperance, prodigality, and
idleness, qualities diametrically opposite to the former, corrupt the
human mind, and lead to violence and injustice; is it not very wisely
calculated by the Author of all things, that a sober people, living
under a good government, should by industry and moderation,
necessarily acquire wealth, which is the best means of warding off
the violence of those with whom they are bound in the great society
of mankind? And is it not also most wisely ordained, that in
proportion as a people contract vicious habits, which may lead to
excess and injustice, the very consequence of their dissipation
(poverty) should deprive them of the power of doing harm? But such
reflections seem rather to be too great a refinement on my subject,
and exceed the bounds of political oeconomy.
When we treat of a virtuous people applying to trade and industry,
let us consider their interest only, in preserving those sentiments;
and examine the political evil of their falling off from them. When we
treat of a luxurious nation, where the not-working part is given to
excesses in all kinds of consumption, and the working part to labour
and ingenuity, in order to supply them, let us examine the
consequences of such a spirit, with respect to foreign trade: and if
we find, that a luxurious turn in the rich is prejudicial thereto, let us
try to discover the methods of engaging the inhabitants to correct
their manners from a motive of self-interest. These things premised,
I shall now give a short sketch of the general principles upon
which a system of foreign trade may be established and preserved
as long as possible, and of the methods by which it may be again
recovered, when, from the natural advantages and superior ability of
administration in rival nations, (not from vices at home) a people
have lost for a time every advantage they used to draw from their
foreign commerce.
The first general principle is to employ, as usefully as possible, a
certain number of the society, in producing objects of the first
necessity, always more than sufficient to supply the inhabitants; and
to contrive means of enabling every one of the free hands to procure
subsistence for himself, by the exercise of some species of industry.
These first objects compassed, I consider the people as
abundantly provided with what is purely necessary; and also with a
surplus prepared for an additional number of free hands, so soon as
a demand can be procured for their labour. In the mean time, the
surplus will be an article of exportation; but no sooner will demand
come from abroad, for a greater quantity of manufactures than
formerly, than such demand will have the effect of gradually
multiplying the inhabitants up to the proportion of the surplus above
mentioned, provided the statesman be all along careful to employ
these additional numbers, which an useful multiplication must
produce, in supplying the additional demand: then with the
equivalent they receive from strangers, they will at the same time
enrich the country, and purchase for themselves that part of the
national productions which had been permitted to be exported, only
for want of a demand for it at home.
He must, at the same time, continue to give proper
encouragement to the advancement of agriculture, that there may
be constantly found a surplus of subsistence (for without a surplus
there can never be enough) this must be allowed to go abroad, and
ought to be considered as the provision of those industrious hands
which are yet unborn.
He must cut off all foreign competition, beyond a certain standard,
for that quantity of subsistence which is necessary for home
consumption; and, by premiums upon exportation, he must
discharge the farmers of any superfluous load, which may remain
upon their hands when prices fall too low. This important matter
shall be explained at large in another place, when we come to treat
of the policy of grain.
If natural causes should produce a rise in the price of subsistence,
which cannot be brought down by extending agriculture, he must
then lay the whole community under contribution, in order to
indemnify those who work for strangers, for the advance upon the
price of their food; or he must indemnify the strangers in another
way, for the advance in the price of manufactures.
He must consider the manufactures of superfluity, as worked up
for the use of strangers, and discourage all domestic competition for
them, by every possible means.
He must do what he can, constantly to proportion the supply to
the demand made for them; and when the first necessarily comes to
exceed the latter, in spight of all his care, he must then consider
what remains over the demand, as a superfluity of the strangers;
and for the support of the equal balance between work and demand,
he must promote the sale of them even within the country, under
certain restrictions, until the hands employed in such branches
where a redundancy is found, can be more usefully set to work in
another way.
He must consider the advancement of the common good as a
direct object of private interest to every individual, and by a
disinterested administration of the public money, he must plainly
make it appear that it is so.
From this principle flows the authority, vested in all governments,
to load the community with taxes, in order to advance the prosperity
of the state. And this object can be nowise better obtained than by
applying the amount of them to the keeping an even balance
between work and demand. Upon this the health of a trading state
principally depends.
If the failure of foreign demand be found to proceed from the
superior natural advantages of other countries, he must double his
diligence to promote luxury among his neighbours; he must support
simplicity at home; he must increase his bounties upon exportation;
and his expence in relieving manufactures, when the price of their
industry falls below the expence of their subsistence.
While these operations are conducted with coolness and
perseverance, while the allurements of the wealth acquired do not
frustrate the execution, the statesman may depend upon seeing
foreigners return to his ports, so soon as their own dissipation, and
want of frugality, come to compensate the advantages which nature
had given them over their frugal and industrious neighbours.
If this plan be pursued, foreign trade will increase in proportion to
the number of inhabitants; and domestic luxury will serve only as an
instrument in the hands of the statesman to increase demand when
the home supply becomes too great for foreign consumption. In
other words, the rich citizens will be engaged to consume what is
superfluous, in order to keep the balance even in favour of the
industrious, and in favour of the nation.
The whole purport of this plan is to point out the operation of
three very easy principles.
The first, That in a country entirely taken up with the object of
promoting foreign trade, no competition should be allowed to come
from abroad for articles of the first necessity, and principally for
food, so as to raise prices beyond a certain standard.
The second, That no domestic competition should be allowed
upon articles of superfluity, so as to raise prices beyond a certain
standard.
The third, That when these standards cannot be preserved, and
that from natural causes, prices get above them, public money must
be thrown into the scale to bring prices to the level of those of
exportation.
The greater the extent of foreign trade in any nation, the lower
these standards must be kept; the less the extent of it, the higher
they may be allowed to rise. Consequently,
Were no man in a nation employed in producing the necessaries of
life, but every man in supplying articles of foreign consumption, the
prices of necessaries might be allowed to fall as low as possible.
There would be no occasion for a standard in favour of those who
live by producing them.
Were no man in the state employed in supplying strangers, the
prices of superfluities might be allowed to rise as high as possible,
and a standard would also become useless, as the sole design of it is
to favour exportation.
But as neither of these suppositions can ever take place, and as in
every nation there is a part employed in producing, and a part in
consuming, and that it is only the surplus of industry which can be
exported; a standard is necessary for the support of the reciprocal
interests of both parties at home; and the public money must be
made to operate only upon the price of the surplus of industry so as
to make it exportable, even in cases where the national prices upon
home consumption have got up beyond the standard. Let me set
this matter in another light, the better to communicate an idea
which I think a little obscure.
Were food and other necessaries the pure gift of nature in any
country, I should have laid it down as a principle to discourage all
foreign competition for them, either below or above any certain
standard; because in this case the lower the price the better, since
no inconveniency could result from thence to any industrious person.
But when the production of these is in itself a manufacture, or an
object of industry, a certain standard must be kept up in favour of
those who live by producing them.
On the other hand, as to the manufactures of superfluity, domestic
competition should be discouraged, beyond a certain standard, in
order that prices may not rise above those offered by foreigners; but
it might be encouraged below the standard, in order to promote
consumption and give bread to manufacturers. But were there no
foreign demand at all, there would be no occasion for any standard,
and the nation’s wealth would thereby only circulate in greater or
less rapidity in proportion as prices would rise or fall. The study of
the balance between work and demand, would then become a
principal object of attention in the statesman, not with a view to
enrich the state, but in order to preserve every member of it in
health and vigour. On the other hand, the object of a standard
regards foreign trade, and the acquisition of new wealth, at the
expence of other nations. The rich, therefore, at home must not be
allowed to increase their consumption of superfluities beyond the
proportion of the constant supply; because these being intended for
strangers, the only way of preventing them from supplying
themselves, is to prevent prices from getting up beyond the
standard, at which strangers can produce them.
Farther, were every one of the society in the same pursuit of
industry, there would be no occasion for the public to be laid under
contribution for advancing the general welfare; but as there is a part
employed in enriching the state, by the sale of their work to
strangers, and a part employed in making these riches circulate at
home, by the consumption of superfluities, I think it is a good
expedient to throw a part of domestic circulation into the public
coffers; that when the consequences of private wealth come
necessarily to raise prices, a statesman may be enabled to defray
the expence of bounties upon that part which can be exported, and
thereby enable the nation to continue to supply foreigners at the
same price as formerly.
The farther these principles can be carried into execution, the
longer a state will flourish; and the longer she will support her
superiority. When foreign demand begins to fail, so as not to be
recalled, either industry must decline, or domestic luxury must
begin. The consequences of both may be easily guessed at, and the
principles which influence them shall be particularly examined in the
following chapter.
CHAP. XVI.
Illustration of some Principles laid down in the former Chapter, relative to the
advancement and support of foreign Trade.