General programming


ActivePerl-faq8 - General programming


General programming questions about ActivePerl

How do I change the Win32 Registry?

There are several Win32 Registry functions provided with ActivePerl. Check the win32mod document provided with ActivePerl.

If you don't understand how the Registry works, remember that a Registry key is like a directory, and a Registry value is like a file. There are several top level keys, and these are kind of like drives.

If you really don't fully understand the Registry, it's probably in your best interest not to mess around with it.

How do I read from/write to a named pipe?

Named pipes are a interprocess communcations mechanism, mainly used with Microsoft operating systems (like Win32 platforms). A named pipe can be addressed just like a file.

The name of a named pipe is a UNC (Universal Naming Convention) name, and looks like \\servername\pipe\pipename. servername is the name of the server you're connecting to, or . for the current computer. pipe is a constant, and pipename is the name of the pipe, such as sql for Microsoft SQL Server.

You can use open(), close(), read(), and print() on a named pipe just like a file. However, you can't use sysread() or syswrite() on one, because they aren't really files.

There's a program called Win32Pipe on the CPAN archive that can be used to create a named pipe.

If you're starting from scratch, and you have a TCP/IP infrastructure, consider using sockets rather than named pipes for your IPC mechanism.

How do I write socket scripts?

There are several examples of socket scripts that are distributed with ActivePerl. They're in the eg subdirectory of your perl directory.

See How do I write a sockets server in Perl for Win32? for information about sockets servers.

What's all this I hear about not being able to use a socket as a filehandle?

Early versions of Perl for Win32 didn't allow you to read or write to a socket as if it were a filehandle. The current versions fully support this, and you shouldn't worry about it too much. If the version that you're using doesn't work well, get the latest build from ActiveState (see Where is the ActivePerl interpreter available?).

You don't have to specify USE_SOCKETS_AS_FILEHANDLES when building Perl for Win32 to get sockets to work like filehandles. It doesn't hurt, but it's not necessary.

How do I write a sockets server in Perl for Win32?

There's an example of a socket server, TCP-SERVER, in the eg directory of your perl directory. In general, information on socket programming for UNIX is applicable to ActivePerl. See especially the perlipc page of the documentation.

If you need to develop a server that can service multiple clients at once, take a look at the IO::Select module. This module allows you to write servers that can manage open connections from multiple clients. Individual requests on a connection are queued up, so if your server can provide quick responses, this approach may work well for you. Here's an example, adapted from Erik Olson's Programming with Perl Modules (one of the volumes in O'Reilly's Win32 Perl Resource Kit):

    use IO::Socket;
    use IO::Select;
    # Create a socket to listen on.
    my $listener = 
      IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
    die "Can't create socket for listening: $!" unless $listener;
    print "Listening for connections on port 8008\n";
    my $readable = IO::Select->new;     # Create a new IO::Select object
    $readable->add($listener);          # Add the listener to it
    while(1) {
        # Get a list of sockets that are ready to talk to us.
        my ($ready) = IO::Select->select($readable, undef, undef, undef);
        foreach my $s (@$ready) {
            # Is it a new connection?
            if($s == $listener) {
                # Accept the connection and add it to our readable list.
                my $new_sock = $listener->accept;
                $readable->add($new_sock) if $new_sock;
                print $new_sock "Welcome!\r\n";
            } else {  # It's an established connection
                my $buf = <$s>;   # Try to read a line
                # Was there anyone on the other end?
                if( defined $buf ) {
                    # If they said goodbye, close the socket. If not,
                    # echo what they said to us.
                    if ($buf =~ /goodbye/i) {
                        print $s "See you later!\n";