Perl 6 Fundamentals: A Primer with Examples, Projects, and Case Studies Moritz Lenz 2024 scribd download
Perl 6 Fundamentals: A Primer with Examples, Projects, and Case Studies Moritz Lenz 2024 scribd download
com
https://textbookfull.com/product/perl-6-
fundamentals-a-primer-with-examples-projects-and-
case-studies-moritz-lenz/
https://textbookfull.com/product/parsing-with-perl-6-regexes-and-
grammars-a-recursive-descent-into-parsing-moritz-lenz/
textbookfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textbookfull.com
https://textbookfull.com/product/python-continuous-integration-and-
delivery-a-concise-guide-with-examples-lenz/
textbookfull.com
https://textbookfull.com/product/racial-disproportionality-and-
disparities-in-the-child-welfare-system-alan-j-dettlaff/
textbookfull.com
https://textbookfull.com/product/criminal-dismemberment-forensic-and-
investigative-analysis-1st-edition-sue-black/
textbookfull.com
https://textbookfull.com/product/the-dark-side-of-globalisation-leila-
simona-talani/
textbookfull.com
Rx.NET in Action with Examples in C# 1st Edition Tamir
Dresher
https://textbookfull.com/product/rx-net-in-action-with-examples-
in-c-1st-edition-tamir-dresher/
textbookfull.com
Perl 6
Fundamentals
A Primer with Examples, Projects,
and Case Studies
—
Moritz Lenz
Foreword by Larry Wall, creator of Perl
Perl 6 Fundamentals
A Primer with Examples,
Projects, and Case Studies
Moritz Lenz
Foreword by Larry Wall, creator of Perl
Perl 6 Fundamentals: A Primer with Examples, Projects, and Case Studies
Moritz Lenz
Fürth, Bayern, Germany
ISBN-13 (pbk): 978-1-4842-2898-2 ISBN-13 (electronic): 978-1-4842-2899-9
DOI 10.1007/978-1-4842-2899-9
Library of Congress Control Number: 2017948406
Copyright © 2017 by Moritz Lenz
This work is subject to copyright. All rights are reserved by the Publisher, whether the
whole or part of the material is concerned, specifically the rights of translation, reprinting,
reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any
other physical way, and transmission or information storage and retrieval, electronic
adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a
trademark symbol with every occurrence of a trademarked name, logo, or image we use the
names, logos, and images only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms,
even if they are not identified as such, is not to be taken as an expression of opinion as to
whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the
date of publication, neither the authors nor the editors nor the publisher can accept any
legal responsibility for any errors or omissions that may be made. The publisher makes
no warranty, express or implied, with respect to the material contained herein.
Cover image by Freepik (www.freepik.com)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Technical Reviewer: Massimo Nardone
Coordinating Editor: Mark Powers
Copy Editor: Brendan Frost
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit
http://www.apress.com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our
Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
9781484228982. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
Contents at a Glance
■
■Chapter 1: What Is Perl 6?�������������������������������������������������������������� 1
■
■Chapter 2: Running Rakudo Perl 6������������������������������������������������� 5
■
■Chapter 3: Formatting a Sudoku Puzzle����������������������������������������� 9
■
■Chapter 4: Datetime Conversion for the Command Line��������������� 23
■
■Chapter 5: Testing say( )��������������������������������������������������������������� 39
■
■Chapter 6: Silent-Cron, a Cron Wrapper��������������������������������������� 43
■
■Chapter 7: Stateful Silent-Cron����������������������������������������������������� 61
■
■Chapter 8: Review of the Perl 6 Basics���������������������������������������� 67
■
■Chapter 9: Parsing INI Files Using Regexes and Grammars��������� 73
■
■Chapter 10: A File and Directory Usage Graph����������������������������� 95
■
■Chapter 11: A Unicode Search Tool��������������������������������������������� 113
■
■Chapter 12: Plotting Using Inline::Python and Matplotlib������������ 119
■
■Chapter 13: What’s Next?����������������������������������������������������������� 135
Index���������������������������������������������������������������������������������������������� 139
iii
Contents
■
■Chapter 1: What Is Perl 6?�������������������������������������������������������������� 1
1.1 Perl 5, the Older Sister������������������������������������������������������������������� 1
1.2 Library Availability�������������������������������������������������������������������������� 2
1.3 Why Should I Use Perl 6?��������������������������������������������������������������� 2
1.4 Summary���������������������������������������������������������������������������������������� 3
■
■Chapter 2: Running Rakudo Perl 6������������������������������������������������� 5
2.1 Installers���������������������������������������������������������������������������������������� 5
2.2 Docker�������������������������������������������������������������������������������������������� 6
2.3 Building from Source��������������������������������������������������������������������� 7
2.4 Testing Your Rakudo Star Installation��������������������������������������������� 8
2.5 Documentation������������������������������������������������������������������������������� 8
2.6 Summary���������������������������������������������������������������������������������������� 8
■
■Chapter 3: Formatting a Sudoku Puzzle����������������������������������������� 9
3.1 Making the Sudoku Playable�������������������������������������������������������� 12
3.2 Shortcuts, Constants, and More Shortcuts����������������������������������� 16
v
■ Contents
6.6 Summary�������������������������������������������������������������������������������������� 59
vi
■ Contents
■
■Chapter 7: Stateful Silent-Cron����������������������������������������������������� 61
7.1 Persistent Storage������������������������������������������������������������������������ 61
7.2 Developing the Storage Back End������������������������������������������������ 62
7.3 Using the Storage Back End��������������������������������������������������������� 65
7.4 Room for Expansion��������������������������������������������������������������������� 66
7.5 Summary�������������������������������������������������������������������������������������� 66
■
■Chapter 8: Review of the Perl 6 Basics���������������������������������������� 67
8.1 Variables and Scoping������������������������������������������������������������������ 67
8.2 Subroutines���������������������������������������������������������������������������������� 67
8.3 Classes and Objects��������������������������������������������������������������������� 69
8.4 Concurrency��������������������������������������������������������������������������������� 71
8.5 Outlook����������������������������������������������������������������������������������������� 71
■
■Chapter 9: Parsing INI Files Using Regexes and Grammars��������� 73
9.1 Regex Basics�������������������������������������������������������������������������������� 74
9.1.1 Character Classes�������������������������������������������������������������������������������������� 75
9.1.2 Quantifiers�������������������������������������������������������������������������������������������������� 75
9.1.3 Alternatives������������������������������������������������������������������������������������������������ 76
vii
■ Contents
viii
■ Contents
12.7 Summary���������������������������������������������������������������������������������� 134
■
■Chapter 13: What’s Next?����������������������������������������������������������� 135
13.1 Scaling Your Code Base����������������������������������������������������������� 135
13.2 Packaging Your Application������������������������������������������������������ 136
13.2.1 Packaging as a Traditional Perl 6 Module����������������������������������������������� 136
13.2.2 Deploying with Docker��������������������������������������������������������������������������� 137
13.2.3 Windows Installers��������������������������������������������������������������������������������� 137
Index���������������������������������������������������������������������������������������������� 139
ix
About the Author
xi
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
About the Technical
Reviewer
xiii
Acknowledgments
They say it takes a village to raise a child. Similar things can be said about
writing a book. It is only possible through the effort of many people, often
unpaid volunteers who contribute just to see the project succeed, and out of
kindness of heart.
I am very grateful for the review by and feedback from Paul Cochrane,
Will Coleda, Elizabeth Mattijsen, Ryan Erwin, Claudio Ramirez, and Massimo
Nardone.
Special thanks go to Larry Wall for creating Perl, for the great foreword, and
for shaping the community to be friendly, welcoming, and a second home to me.
Finally, thanks go to my parents, for kindling my love both for books and for
engineering. And most importantly to my family: to Signe, my wife, for constant
support; and to my daughters Ida and Ronja for keeping me grounded in the real
world, and bringing joy to my life.
xv
Foreword
The reason I’m writing this (and perhaps why you’re reading it), is that people
just give me way too much credit. Yeah, sure, I invented Perl 30 years ago, and I
coded the first five versions all by myself, pretty much. But for the last 20 years,
the vast majority of the work has been done by other members of the industrious
Perl community, who get far too little credit. To be sure, I don’t mind getting
extra credit: I’m human enough to enjoy the undue adulation, and I understand
how communities want—and possibly even need—to have a figurehead who
represents the whole.
I will gladly take credit, however, for the idea that a computer language must
have a vibrant community in order to thrive. From the beginning, that was the
intent of Perl. It all comes down to linguistics: Perl was designed to work like a
natural language on many levels, not just the syntactic level. In particular, every
living language is symbiotic with the culture that conveys it forward into the
future. More generally, natural languages are responsive to context on every
level, and some of those levels are anthropological. People provide context to
Perl, which in turn is designed to respond productively to that context.
This may seem simple, but it’s a surprisingly tricky concept to bake into
a programming language and its culture. Just look at how many computer
languages fail at it. In most programming cultures, you are a slave to the
computer language. Rarely, if ever, do you get the feeling that the computer
language is there to work for you.
We’re trying to change all that. So when the Perl community, back in 2000,
decided to do a major redesign of Perl 5 to clean up the cruftier bits, we not only
wanted to fix things that we already knew were suboptimal, but we also wanted
to do a better job of responding to cultural change, because we simply don’t
know what we’ll want in the future. So we thought about how best to future-
proof a computer language; much of the current design is about maintaining
careful control of identity, mutability, dimensionality, typology, and extensibility
over time, so we could isolate changes to minimize collateral damage. Other
than worrying about that, my main contribution as language designer was to
unify the community’s contradictory desires into a coherent whole.
All that being said, it’s still all about the community: nearly all the
implementation work was done by others, and most of the features that ended
up in Perl 6 can be traced back through various revisions to the community’s
xvii
■ Foreword
xviii
■ Foreword
xix
CHAPTER 1
What Is Perl 6?
During this time, Perl 5 also evolved, and the two languages are different
enough that most Perl 5 developers don’t consider Perl 6 a natural upgrade path
anymore, to the point that Perl 6 does not try to obsolete Perl 5 (at least not more
than it tries to obsolete any other programming language :-), and the first stable
release of Perl 6 in 2015 does not indicate any lapse in support for Perl 5.
Perl 5 is developed by a separate community of enthusiasts, who keep an
eye on Perl 6 to find features worth adopting into Perl 5. So while the Perl 5 and
Perl 6 communities have a certain overlap and communicate with each other,
both thrive mostly independently.
1.2 Library Availability
Being a relatively young language, Perl 6 lacks the mature module ecosystem
that languages such as Perl 5 and Python provide.
To bridge this gap, interfaces exist that allow you to call into libraries
written in C, Python, Perl 5, and Ruby. The Perl 5 and Python interfaces are
sophisticated enough that you can write a Perl 6 class that subclasses a class
written in either language, and the other way around.
So if you like a particular Python library, for example, you can simply load it
into your Perl 6 program through the Inline::Python module.
2
Chapter 1 ■ What Is Perl 6?
There are more technical benefits that I could list, but more importantly,
the language is designed to be fun to use. An important aspect of that is good
error messages. Have you ever been annoyed at Python for typically giving just
SyntaxError: invalid syntax when something’s wrong? This error could
come from forgetting a closing parenthesis, for example. In this case, a Perl 6
compiler says
which actually tells you what’s wrong. But this is just the tip of the iceberg. The
compiler catches common mistakes and points out possible solutions, and
even suggests fixes for spelling mistakes. The Perl 6 community considers error
messages that are less than awesome, short LTA, to be worthy of bug reports, and
much effort is spent into raising the bar for error messages.
Finally, Perl 6 gives you the freedom to express your problem domain and
solution in different ways and with different programming paradigms. And if
the options provided by the core language are not enough, it is designed with
extensibility in mind, allowing you to introduce both new semantics for
object-oriented code and new syntax.
1.4 Summary
Perl 6 is a flexible programming language that offers many cool and convenient
features to both beginners and experts. It offers flexibility, type checking, and
powerful Unicode and text processing support.
3
CHAPTER 2
Before we start exploring Perl 6, you should have an environment where you
can run Perl 6 code. So you need to install Rakudo Perl 6, currently the only
actively developed Perl 6 compiler. Or even better, install Rakudo Star, which is a
distribution that includes Rakudo itself, a few useful modules, and a tool that can
help you install more modules.
Installing Rakudo itself gives you just the compiler. It follows a monthly
release cycle, so it allows you to keep up to date with the latest developments.
When you choose to install Rakudo Star, which is typically released every
three months, you get a more stable base for development, and some tools like a
debugger and a module installer. You can use the module installer to make use
of prepackaged software libraries that are included neither in Rakudo itself nor
in Rakudo Star.
The following sections discuss a few options for installing Rakudo Star.
Choose whatever works for you.
The examples in this book use Rakudo 2017.04.03 or Rakudo Star 2017.04
(which is built on top of Rakudo 2017.04.03) and should work with this or any
newer version of Rakudo, as long as it supports Perl 6 version 6.c.
■■Note The examples and source code used in this book can be accessed via the
Download Source Code button at https://www.apress.com/9781484228982.
2.1 Installers
You can download installers from https://rakudo.perl6.org/downloads/
star/ for Mac OS (.dmg) and Windows (.msi). After download, you can launch
them, and they walk you through the installation process.
Prebuilt Linux packages are available from https://github.com/nxadm/
rakudo-pkg/releases/ for Debian, Ubuntu, CentOS, and Fedora.
In both cases, use version 2017.04 to get the best compatibility with the
examples used in this book.
Note that Rakudo is not relocatable, which means you have to install to
a fixed location that was decided by the creator of the installer. Moving the
installation to a different directory is not possible.
On Windows, the installer (Figure 2-1) offers to add C:\rakudo\bin and
C:\rakudo\share\perl6\site\bin to your PATH environment. You should
choose that option, as it allows you to execute Rakudo perl6 (and programs that
the module installer installs on your behalf ) without specifying full paths.
Figure 2-1. The Rakudo Star installer consists of four easy screens
2.2 Docker
On platforms that support Docker, you can pull an existing Docker container
from the docker hub:
Then you can get an interactive Rakudo shell with this command:
But that alone won’t work for executing scripts, because the container has
its own separate file system. To make scripts available inside the container, you
need to tell Docker to make the current directory available to the container:
I put this line into my ∼/.bashrc file, so new shell instances have a p6d
command, short for “Perl 6 docker.”
As a short test to see if it works, you can run
If you go the Docker route, use the p6d alias instead of perl6 to run scripts.
$ wget https://rakudo.perl6.org/downloads/star/rakudo-star-2017.04.
tar.gz
$ tar xzf rakudo-star-2017.04.tar.gz
$ cd rakudo-star-2017.04/
$ perl Configure.pl --prefix=$HOME/opt/rakudo-star --gen-moar
$ make install
You should have about 2GB of RAM available for the last step; building a
compiler is a resource-intensive task.
You need to add paths to two directories to your PATH environment variable,
one for Rakudo itself and one for programs installed by the module installer:
PATH=$PATH:$HOME/opt/rakudo-star/bin/:$HOME/opt/rakudo-star/share/perl6/
site/bin
If you are a Bash user, you can put that line into your ∼/.bashrc file to make
it available in new Bash processes.
1
http://gcc.gnu.org/
7
Chapter 2 ■ Running Rakudo Perl 6
$ perl6 --version
This is Rakudo version 2017.04.2 built on MoarVM version 2017.04
implementing Perl 6.c.
If, against all odds, all of these approaches have failed to produce a usable
Rakudo installation, you should describe your problem to the friendly Perl
6 community, which can usually provide some help. https://perl6.org/
community/ describes ways to interact with the community.
2.5 Documentation
Rakudo itself has little documentation, since most of the interesting material is
about the Perl 6 language. But Rakudo does come with a summary of command-
line options that you can access by calling perl6 --help.
The official place for Perl 6 language documentation is at https://docs.
perl6.org/, which aims to provide both reference and tutorial-style material.
Other good resources are listed at https://perl6.org/resources/, many of
which are created and maintained by members of the Perl 6 community.
2.6 Summary
On most platforms, you can install Rakudo Star from prebuilt binary installers.
Where this doesn’t work, Docker images are available. Finally, Rakudo Star can
be built from its sources.
8
CHAPTER 3
00000007500008009400050060001000020000090005700600304000100002308000
0006063240000
# file sudoku.p6
use v6;
my $sudoku = '
000000075000080094000500600010000200000900057006003040
001000023080000006063240000';
for 0..8 -> $line-number {
say substr $sudoku, $line-number * 9, 9;
}
$ perl6 sudoku.p6
000000075
000080094
000500600
010000200
000900057
006003040
001000023
080000006
063240000
There’s not much magic in there, but let’s go through the code one line at a time.
The first line, starting with a #, is a comment that extends to the end of the line.
use v6;
This line is not strictly necessary, but good practice anyway. It declares
the Perl version you are using, here v6; in other words, any version of the Perl
6 language. We could be more specific and say use v6.c to require exactly the
version discussed here. If you ever accidentally run a Perl 6 program through
Perl 5, you’ll be glad you included this line, because it’ll tell you the following:
$ perl sudoku.p6
Perl v6.0.0 required--this is only v5.22.1, stopped at sudoku.p6 line 1.
BEGIN failed--compilation aborted at sudoku.p6 line 1.
10
Chapter 3 ■ Formatting a Sudoku Puzzle
my $sudoku = '00000007500...';
my declares a lexical variable. It is visible from the point of the declaration to the
end of the current scope, which means either to the end of the current block
delimited by curly braces, or to the end of the file if it’s outside any block, as it is
in this example.
Variables start with a sigil, here a $. Sigils are what gave Perl the reputation
of being line noise, but there is signal in the noise. The $ looks like an S, which
stands for scalar. If you know some math, you know that a scalar is just a single
value, as opposed to a vector or even a matrix.
This variable doesn’t start its life empty, because there’s an initialization
right next to it. The value it starts with is a string literal, as indicated by the
quotes.
Note that there is no need to declare the type of the variable beyond the
very vague “it’s a scalar” implied by the sigil. If we wanted, we could add a type
constraint:
The for loop has the general structure for ITERABLE BLOCK. Here the
iterable is a range,1 and the block is a pointy block. The block starts with ->,
which introduces a signature. The signature tells the compiler what arguments
the block expects, here a single scalar called $line-number.
1
https://docs.perl6.org/type/Range.html
11
Random documents with unrelated
content Scribd suggests to you:
metallo, anche prezioso, lavori in ceramica, porcellana, oggetti di
legno, schiavi ecc. ecc. I rivoli di tanta importazione scaturivano da
ogni parte del mondo allora conosciuto, dalla Grecia peninsulare,
dalla Tracia, dalla Macedonia, dal Tirreno, dal Jonio, dall’Egeo, dalle
coste dell’Asia Minore, dalle sponde del Mar Nero, da Cipro, Creta,
Cartagine, dalla Magna Grecia, in Etruria, in Caria, in Frigia, in
Paflagonia, in Fenicia, in Spagna, in Siria [37].
Tanto ben di Dio non era unicamente destinato al fabbisogno della
città, ma ne alimentava a sua volta lo scambio ed il commercio con
l’estero. I forestieri scendevano sia al Pireo che ad Atene a fare le
loro provviste e a fornirsi dei manufatti dell’industria paesana o
forestiera, accumulati nei magazzini della metropoli. «Tutti coloro»,
chiedeva Senofonte o, piuttosto, l’anonimo autore di un suggestivo
libretto su Le entrate di Atene; «tutti coloro i quali abbondano di
grano, di vino (specie se si tratta di qualità prelibate), di olio, di
bestiame, vogliono trar vantaggio o dalla loro sagacia o dal loro
danaro», «tutti quelli che han bisogno di vendere o di comperare
nella maggiore quantità e nel più breve tempo possibile, dove
potrebbero meglio profittare che in Atene?» [38]. In tal guisa, Atene, a
cui tutti i prodotti delle contrade mediterranee mettevano capo,
esportava, a sua volta, ovunque [39] pellicce, oggetti in cuoio, tessuti
di varie fogge, vasi di terra e di metallo, letti, strumenti musicali,
profumi, chincaglierie, oreficerie, argenterie, libri, tavole, statue,
oggetti artistici, marmo, piombo, e, fra i generi alimentari, vino, miele,
fichi e, specialmente olio [40], dal cui commercio, come dall’unica
derrata, che la politica finanziaria ateniese affrancasse da misure
proibitive, massimo era, qual’è ancor oggi, il guadagno.
D’altro lato, i mercanti ateniesi facevano direttamente, fuori
dell’Attica, un attivissimo scambio di merci, acquisite e caricate [41],
ed esportavano dall’Egeo vini e altri articoli in Sicilia, in Italia, nella
Colchide, nel Ponto [42], ecc.
A quanto ascendeva il valore del movimento commerciale di Atene?
Noi non abbiamo mezzo sicuro per determinarlo. Calcolando
sull’ammontare del dazio d’entrata e di uscita, a cui sottostavano le
merci che per terra o per mare entravano nell’Attica o ne uscivano —
la famosa cinquantesima, ossia un’imposta del 2% — quale ci è
riferito per un anno di grave crisi economica ateniese, ci troveremmo
di fronte a un movimento di 10-12 milioni [43]. Ma il commercio
ateniese doveva essere ben altra cosa negli anni normali o nei giorni
della gloria. Del resto esso non si svolgeva soltanto nell’Attica, ma
circolava per tutto il suo impero marittimo. Entro così vasta periferia,
noi non possiamo supporlo inferiore a 160-180.000.000 di lire-
oro [44], ossia, dato il valore del denaro nell’evo antico, rispetto a
quello moderno, pari circa a un miliardo di lire, il triplo di ciò a cui,
innanzi la Guerra europea, ascendesse il movimento commerciale
della Grecia contemporanea, che è pure uno dei più fiorenti del
Mediterraneo [45].
Or bene, tutta questa gigantesca e preziosa attività veniva d’un tratto
arrestata dalla guerra. Le navi, gli uomini erano tosto destinati a
scopi ben diversi, che non fossero più quelli del pacifico commercio;
i porti erano ingombri di vascelli militari e di materiale bellico; a tutte
le flotte era imposto il duro cómpito della così detta guerra di corsa.
Una tale e tanto grave conseguenza basterebbe da sola ad avvertire
delle proporzioni della crisi, che la guerra, col suo semplice
annunzio, veniva ogni volta a provocare.
Nè la guerra si combatteva soltanto con le armi. Il gioco delle armi si
intrecciava e si serrava con quello delle proibizioni commerciali. «I
nostri nemici», avvertiva acutamente l’autore de La Repubblica
ateniese, «non consentiranno mai che le merci vengano portate in
luoghi diversi da quelli in cui essi tengono il dominio del mare» [46].
La Guerra del Peloponneso — era ben noto in Atene — era stata
inaugurata da un decreto vietante ai Megaresi l’accesso nei mercati
dell’Attica e nei restanti porti della Confederazione [47]; più volte
Egina ed Atene s’erano a vicenda bandito il boicottaggio di
determinate mercanzie; in tempi lieti e tranquilli, Sparta, antico
Impero celeste, chiudeva l’ingresso agli stranieri [48], e quindi alle
loro importazioni. Ma la guerra induceva naturalmente a moltiplicare
all’infinito la frequenza e la gravità di tante rappresaglie e di così
gravi danni.
Quando Atene ebbe rotto le ostilità contro Filippo II, fu bandita la
pena di morte contro chiunque apprestasse armi o attrezzi navali al
nemico [49]. E, poichè le fabbriche ateniesi erano le fornitrici principali
del materiale di guerra a molti Stati greci, siffatta disposizione, che
deve aver avuto conseguenze crudeli, non deve certo essere
rimasta episodio isolato. Noi non abbiamo menzione di veri e propri
blocchi, parziali o totali, contro l’Attica: ma sappiamo che contro
Atene fu più volte condotta la così detta guerra di corsa e siamo
autorizzati a pensare che blocchi veri e propri non dovettero
certamente mancarne poichè ne troviamo enumerati parecchi a
danno di altri Paesi. Il blocco, del resto, non danneggia soltanto
coloro contro cui è dichiarato, ma ricade su quelli stessi che lo
dichiarano, specie se si tratta di Stati industriali e commerciali. Esso
equivale alla chiusura di una delle fonti dell’importazione e di taluno
dei mercati d’esportazione, cioè a dire al disseccamento di una parte
dell’agiatezza e della ricchezza di quei Paesi, che lo hanno per primi
volontariamente decretato [50].
Ma non si trattava di ripercussioni o di effetti limitati. La grande legge
della solidarietà universale abbraccia tutto il dominio e tutti i rami
della produzione. «I frammenti del gran tutto», parla un antico, «si
sorreggono come gli anelli di una catena; l’agricoltura ha bisogno
dell’arte del legnaiuolo e del fabbro ferraio; questi, dei tessitori e
degli architetti, e così, a chi ben guardi, tutto è solidale
nell’intrecciarsi dell’umano consorzio». Per tal guisa, il fiorire o il
decadere del commercio decideva in buona parte delle sorti
dell’agricoltura e di quelle dell’industria.
Il rapporto di queste due attività riesce nell’evo antico alquanto
diverso da quello che fra esse corre nell’evo moderno. Oggi il rapido
miglioramento degli strumenti di produzione, la tenuità del prezzo
delle merci sforzano i mercati più refrattari, determinano le correnti
d’importazione e di esportazione, eludono i talora contrastanti
rapporti diplomatici. Nel mondo antico, invece, delle sorti del
commercio, e quindi della esistenza delle varie industrie, decideva il
più delle volte la violenza delle armi.
Gli Stati, con le loro navi da guerra e con la loro autorità,
assicuravano le pericolose vie del transito, aprivano o chiudevano i
mercati ai più temuti concorrenti, s’accaparravano, in gran copia e a
buon mercato, l’approvvigionamento delle materie prime. L’industria
e il dominio della terra e del mare erano, assai più che nell’evo
contemporaneo, intimamente connessi fra loro, e la ricchezza e la
forza erano, assai più che non oggi, a un di presso, un unico
fenomeno sotto denominazioni differenti. «Non vi è città», spiega
ancora l’autore de La Repubblica ateniese, «che non abbia bisogno
di importare o di esportare; il che non potrebbe, se non
assoggettandosi alle disposizioni di coloro che tengono il dominio
delle acque» [51]. «Se taluna ha eccesso di legname per navi, dove
lo venderà se non consente chi impera sul mare? Se tal’altra
abbonda di ferro, di bronzo, di cotone, dove li scambierà se a
costoro non si renderà benevisa?» [52]. Ma se tale circostanza
faceva ad ogni popolo gradito il pensiero della guerra, ogni disastro
bellico segnava necessariamente la fine d’ogni prosperità industriale.
A sua volta la rovina del commercio e dell’industria si ripercoteva
malauguratamente su quello che abbiamo definito il fulcro
dell’economia antica: l’agricoltura. Si è talora ritenuto che le società
puramente agricole siano capaci di vivere e di fiorire in piena ed
isolata indipendenza. Nulla di meno esatto. Condizione necessaria
della loro prosperità è stata in ogni tempo l’esistenza di mercati
prossimi al coltivatore, ove questi possa smerciare le proprie derrate
e donde abbia mezzo di ritrarre gli elementi necessari al proprio
benessere. Quanto diverse non sono le prospettive dell’agricoltura in
Paesi poveri e in Paesi arricchiti dalla febbrile attività del commercio
e dell’industria! Le sorti dell’agricoltura risultano quindi strettamente
connesse con le sorti dell’uno e dell’altra; fioriscono e decadono
insieme. Tale è stata la sorte loro in ogni tempo: tale fu nell’Attica
antica!
Ma gli effetti economici della guerra perenne, quale noi la ritroviamo
in Grecia, non conoscono limiti. Le nazioni antiche non disponevano
di una quantità sempre uguale e costante di prodotti naturali o di
merci di cui godere. Le difficoltà dei trasporti, l’impossibilità di reagire
artificialmente alle violenze degli agenti naturali, la mancanza di un
comune mercato regolatore; sopra tutto, la scarsa potenzialità
produttiva, riescivano di ostacolo insuperabile alla stabilità e alla
costanza della produzione [53]. Ma, fra le cause di squilibrio,
massime erano quelle dipendenti dalla guerra.
La guerra distruggeva ciò che si era prodotto, allontanava le braccia
dalla produzione, arrestava e stornava l’investimento dei capitali,
turbava la pastorizia e l’agricoltura, impediva l’importazione e
l’esportazione, determinava mirabolanti dislivelli nei prezzi delle
derrate naturali e delle merci, che, come l’esperienza oggi ci avverte,
dovevano anche allora prolungarsi per gran tempo, o, magari,
aggravarsi, al ritorno delle brevissime paci [54]; arrestava gli affari o
distruggeva il credito; sospendeva l’obbligo della correttezza e della
legalità, e, sotto la minaccia di oscuri pericoli, terminava per pesare
sulla vita sociale con una continua incertezza, un oscuro malessere,
una paralisi quotidiana delle sue migliori energie, di cui, nell’assai più
stabile regolarità del mondo contemporaneo, riesciamo a mala pena
a cogliere la portata.
Tributi e imposizioni
straordinarie.