<LI><A href="#How_do_I_change_the_Win32_Regist">How do I change the Win32 Registry?</A></LI>
<LI><A href="#How_do_I_read_from_write_to_a_na">How do I read from/write to a named pipe?</A></LI>
<LI><A href="#How_do_I_write_socket_scripts_">How do I write socket scripts?</A></LI>
<LI><A href="#What_s_all_this_I_hear_about_not">What's all this I hear about not being able to
use a socket as a</A></LI>
<LI><A href="#How_do_I_write_a_sockets_server_">How do I write a sockets server in ActivePerl?</A></LI>
<LI><A href="#How_do_I_send_or_receive_files_b">How do I send or receive files by FTP?</A></LI>
<LI><A href="#How_do_I_send_or_receive_files_b">How do I send or receive files by HTTP?</A></LI>
<LI><A href="#How_do_I_manage_user_accounts_wi">How do I manage user accounts with ActivePerl?</A></LI>
<LI><A href="#How_do_I_read_from_and_write_to_">How do I read from and write to serial ports?</A></LI>
<LI><A href="#Why_doesn_t_the_d_operator_work">Why doesn't the -d operator work?</A></LI>
<LI><A href="#Reading_from_and_writing_to_file">Reading from and writing to files mysteriously
fails. What's wrong?</A></LI>
<LI><A href="#When_I_try_to_open_a_file_I_get">When I try to open a file, I get a "bad
argument" error.</A></LI>
<LI><A href="#Why_do_I_get_an_error_using_Perl">Why do I get an error using Perl's here-doc
syntax (<<), that</A></LI>
</UL>
</LI>
<LI><A href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</A></LI>
</UL>
<HR>
<H1><A name="NAME">NAME</A></H1>
<P>ActivePerl faq8 - General programming</P>
<HR>
<H1><A name="DESCRIPTION">DESCRIPTION</A></H1>
<P>General programming questions about ActivePerl</P>
<HR>
<H2><A name="How_do_I_change_the_Win32_Regist">How do I change the Win32 Registry?</A></H2>
<P>There are several Win32 Registry functions provided with ActivePerl. Check the win32mod document
provided with ActivePerl.</P>
<P>If you don't understand how the Registry works, remember that a Registry <EM>key</EM> is like a
directory, and a Registry <EM>value</EM> is like a file. There are several <EM>top level keys</EM>,
and these are kind of like drives.</P>
<P>If you really don't fully understand the Registry, it's probably in your best interest not to
mess around with it.</P>
<HR>
<H2><A name="How_do_I_read_from_write_to_a_na">How do I read from/write to a named pipe?</A></H2>
<P>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.</P>
<P>The name of a named pipe is a UNC (Universal Naming Convention) name, and looks like <EM>\\servername\pipe\pipename</EM>.
<CODE>servername</CODE> is the name of the server you're connecting to, or <CODE>.</CODE> for the
current computer. <CODE>pipe</CODE> is a constant, and <CODE>pipename</CODE> is the name of the
pipe, such as sql for Microsoft SQL Server.</P>
<P>You can use <CODE>open(),</CODE> <CODE>close(),</CODE> <CODE>read(),</CODE> and <CODE>print()</CODE>
on a named pipe just like a file. However, you can't use <CODE>sysread()</CODE> or <CODE>syswrite()</CODE>
on one, because they aren't really files.</P>
<P>There's a program called Win32Pipe on the CPAN archive that can be used to create a named pipe.</P>
<P>If you're starting from scratch, and you have a TCP/IP infrastructure, consider using sockets
rather than named pipes for your IPC mechanism.</P>
<HR>
<H2><A name="How_do_I_write_socket_scripts_">How do I write socket scripts?</A></H2>
<P>There are several examples of socket scripts that are distributed with ActivePerl. They're in the
<EM>eg</EM> subdirectory of your perl directory.</P>
<P>See <A href="#How_do_I_write_a_sockets_server_">How do I write a sockets server in Perl for
Win32?</A> for information about sockets servers.</P>
<HR>
<H2><A name="What_s_all_this_I_hear_about_not">What's all this I hear about not being able to use a
socket as a filehandle?</A></H2>
<P>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 <A href="../ActivePerl-faq1.html#Where_is_the_Perl_for_Win32_inte">Where
is the ActivePerl interpreter available?</A>).</P>
<P>You don't have to specify <CODE>USE_SOCKETS_AS_FILEHANDLES</CODE> when building Perl for Win32 to
get sockets to work like filehandles. It doesn't <EM>hurt</EM>, but it's not necessary.</P>
<HR>
<H2><A name="How_do_I_write_a_sockets_server_">How do I write a sockets server in Perl for Win32?</A></H2>
<P>There's an example of a socket server, TCP-SERVER, in the <EM>eg</EM> 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.</P>
<P>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):</P>
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>
<HR>
<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 href="http://dada.perl.it/">http://</A><A href="http://dada.perl.it">dada.perl.it/</A></P>
<HR>
<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 href="http://dada.perl.it">http://dada.perl.it/</A></P>
<HR>
<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>
<HR>
<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>
<HR>
<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 this:</P>