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";
$readable->remove($s);
$s->close;
} else {
print $s "You said: $buf\n";
}
} else { # The client disconnected.
$readable->remove($s);
$s->close;
print STDERR "Client Connection closed\n";
}
}
}
}
</pre>
<p>For more information, see the IO::Socket and IO::Select documentation. It is
also possible to write a multithreaded server using ActivePerl, if threads are
enabled in the version of Perl you are using. However, threading is still
somewhat experimental in Perl 5.005, so use this feature with caution.</p>
<h2><a name="How_do_I_send_or_receive_files_b">How do I send or receive files by
FTP?</a></h2>
<p>See the Net::FTP module. Net::FTP is part of the libnet bundle, which is
available from CPAN, and can be installed using the Perl Package Manager (PPM).</p>
<p>Aldo Calpini has developed a ActivePerl extension to do FTP and HTTP using
the WININET library. It's in alpha testing and is available on his web page at <a class="doc" href="http://dada.perl.it/">http://</a><a class="doc" href="http://dada.perl.it">dada.perl.it/</a></p>
<h2><a name="How_do_I_send_or_receive_files_b">How do I send or receive files by
HTTP?</a></h2>
<p>The libwww-perl bundle (LWP) is a collection of modules for WWW access in
Perl. LWP is available from CPAN in source form, or you can install it using the
Perl Package Manager (PPM). LWP may also be included with future binary releases
of Perl.</p>
<p>Aldo Calpini has developed a ActivePerl extension to do FTP and HTTP using
the WININET library. It's in alpha testing and is available on his web page at <a class="doc" href="http://dada.perl.it">http://dada.perl.it/</a></p>
<h2><a name="How_do_I_manage_user_accounts_wi">How do I manage user accounts
with ActivePerl?</a></h2>
<p>There's an extension called Win32::NetAdmin distributed with ActivePerl. It
has a pretty low-level interface, but it is very possible to manage users and
groups with this module.</p>
<h2><a name="How_do_I_read_from_and_write_to_">How do I read from and write to
serial ports?</a></h2>
<p>Serial ports can be opened just like files in ActivePerl. To open <em>COM1</em>,
just do this:</p>
<pre>
open( PORT, "+>COM1" ) or die "Can't open COM1: $!";
</pre>
<p>You should be able to read from and write to the file handle using the
standard I/O functions (read() and <code>print()),</code> but not the system
functions (sysread() and <code>syswrite()).</code></p>
<p>It has been noted (but not tested) that modems that use the Hayes command set
require a carriage return (\r) rather than a line feed (\n) at the end of the
command.</p>
<h2><a name="Why_doesn_t_the_d_operator_work">Why doesn't the -d operator work?</a></h2>
<p>It does, in fact, work. However, people tend to use it incorrectly and get
bad results. To check for all the subdirectories in a directory, try code like