In this session, we will revisit Pharo syntax.
We will review it entirely using a concrete example,
the example of a simple HTTP application from the real world.
We will analyze the code and review Pharo syntax through it.
This application is a simple web application
which displays information on books.
I will give you the first example: I'll use the framework Zinc,
a HTTP framework we'll study in another course.
It comes in two parts: client and server.
It can make HTTP requests, etc.
First, we can see ZnClient is a class.
So it starts with uppercase Z as we saw previously.
Names of classes and global variables start with uppercase.
ZnClient is a class.
You send the message new to this class;
it will create a new instance of ZnClient.
To this new instance, you send a first message url: with a parameter,
which is a string.
Then we'll use the cascade.
Let me remind you that ";" at the end means:
you send a new message, get, to the same receiver.
This bit of code simulates what your navigator will do
when you type in the url and press enter.
If I type in an url and press enter,
it contacts the server of this web application
and it returns a HTTP response:
in this case, the information on a book, the book number 1.
I will give you another example,
another request built with Pharo and ZnClient.
So it's the same: you instantiate an object Znclient.
You send messages using cascades.
And then, you can see something we have already studied:
in Pharo, we have keyword messages,
meaning that the method name is formAt: put:.
This is equivalent in typical Java syntax
to formAtput (the method name is all attached).
And I put all the arguments between parentheses.
On this slide, I'd like to show you
the complete code of this web application
constructed with Zinc.
I will show you some parts of this code one element at a time.
First of all, the first part corresponds to the server configuration.
On the first line,
there are local variable declarations.
Remember they start with lowercase.
books and teapot are two local variables between vertical bars.
Next, there are assignments: here is the first one.
It's := in Pharo.
You can have symbols.
Thirdly: there is #port
which is located here in the code.
It corresponds to a symbol, a unique string.
It's created only once.
There are messages sent to the class Teapot.
For example, the class Teapot is here.
And I can send the message configure to it.
Look at configure:.
Let me remind you that ":" marks the presence of an argument
which you send to this method.
The argument starts with a brace
and ends with a brace.
So what's between the braces is a dynamic array:
all this between braces.
Each expression separated by "." will be evaluated
and we'll build an array with the results of each expression.
There's only one argument in the method configure.
And inside this dynamic array between braces,
you will use an arrow to build association objects.
So it's one key, one value.
So to make the server work,
you won't just define the server's configuration
but also routes.
I will explain using a smaller example: this is an extract of the complete code.
If I want my application to process URLs like this one,
which ends with books/1,
if I want the information on the book 1, for example,
I define a route in my Zinc application.
When I receive a request of the type GET,
which starts with /books/ and an integer,
- can you see this ? - it executes this block of code.
Remember that a block starts and ends with brackets.
So it's an anonymous method which can take a parameter:
in this case, the parameter is called :request
and it is separated by a vertical bar.
Next, the body of the block is all here.
There's only one expression here, this one.
So you send the message at: to the object books.
What book do you want in the book collection?
You want the book whose id was passed as parameter in the url.
I query the request.
The variable here is the parameter of the block defined in the beginning.
I get the element called id in this request.
I convert this element. Remember, it's an integer.
I convert it into a string.
This allows me to get the book 1.
To conclude, we have reviewed all the Pharo syntax
in this class.
The goal was to take an interest in syntax,
but we were also able to rediscover the syntax through a package
which is quite interesting and fun to program with, Teapot.
Go find out by yourself. You can download it from smalltalk.
You can build HTTP applications very easily.
Also, it's a package built based on Zinc.
Zinc is one of the very strong libraries in Pharo
to make more complex web applications,
as we will see in following courses.