Connected: An Internet Encyclopedia

Up: Connected: An Internet Encyclopedia
Up: Topics
Up: Software
Up: Languages
Prev: Java
Next: Mail



Perl, the Practical Extraction and Report Language, is Larry Wall's catch-all scripting language that has become nearly ubiquitous on UNIX systems. Of the many UNIX scripting languages available, Perl is the only one with built-in Internet networking support. For example, here a short script (taken verbatim from the Perl manual page), that implements a simple Internet server, which listens for TCP connections on port 2345 and prints anything it receives on standard output:


   ($port) = @ARGV;
   $port = 2345 unless $port;

   require 'sys/';

   $sockaddr = 'S n a4 x8';

   ($name, $aliases, $proto) = getprotobyname('tcp');
   ($name, $aliases, $port) = getservbyname($port, 'tcp')
        unless $port =~ /^\d+$/;

   $this = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");

   select(NS); $| = 1; select(stdout);

   socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
   bind(S, $this) || die "bind: $!";
   listen(S, 5) || die "connect: $!";

   select(S); $| = 1; select(stdout);

   for (;;) {
        print "Listening again\n";
        ($addr = accept(NS,S)) || die $!;
        print "accept ok\n";

        ($af,$port,$inetaddr) = unpack($sockaddr,$addr);
        @inetaddr = unpack('C4',$inetaddr);
        print "$af $port @inetaddr\n";

        while () {
             print NS;

An online version of the Perl manual best describes how to use it. Tom Christenson maintains the Perl Language Home Page, and Yahoo has a Perl page.

Next: Mail

Connected: An Internet Encyclopedia