7.4.1 Perl’s Built-in Sockets

7.4.1  Perl’s Built-in Sockets

Perl provides us with a set of built-in functions for dealing with sockets. The names of Perl functions that deal with sockets are the same as the names of the corresponding system calls one sees in the Unix environment. The number and the names of the arguments, however, vary.

In Unix, all devices have a file-like interface. Sockets are treated like devices and therefore, have a file-like interface. Perl also takes this approach to files, pipes, sockets and other so-called devices.

The concept of a socket provides an abstraction that is file-like, but deals with communication. When a program creates a socket, Perl creates a handle just like a filehandle or stream. This filehandle is used in all further socket-related commands and can be used to read and write just like filehandles. Writing to a socket causes the written information to be sent to the corresponding peer or companion socket on the same or another machine. Reading from a socket lets a process get the information sent to it by another process. Of course, the line of communication has to be open for the information to be transferred. That is, if we are talking about two separate machines, they have to be
linked by a network.

Perl’s built-in socket functions are quite low-level. They provide enormous flexibility, but at the same time require significant effort to understand and hence, use. In this book, we refrain from discussing them, since for almost all practical purposes, the contributed modules discussed next suffice.