home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
- <html xmlns="http://www.w3.org/TR/xhtml1">
- <head>
- <title>perlwin32faq8 - General programming</title>
- <link rev="made" href="mailto:bjepson@debian.ids.net" />
- <meta name="GENERATOR" charset="iso-8859-1" />
- <link rel="STYLESHEET" href="../win32prk.css" type="text/css"
- media="screen" />
- </head>
-
- <body bgcolor="#ffffff">
- <!-- beginning of leaf header-->
-
- <table width="100%">
- <tr>
- <td bgcolor="000000" width="70" height="31"><a href=
- "http://www.activestate.com/"><img src="ASbutton.gif" alt=
- "ActiveState Home Page" border="0" width="68" height=
- "30" /></a></td>
-
- <td width="10" bgcolor="#ffffff"> </td>
-
- <td valign="middle" bgcolor="#cc0066"><font face=
- "sans-serif" size="+1" color="#ff99cc">
- Win32 FAQ</font></td>
- </tr>
- </table>
- <!-- end of leaf content--><!-- INDEX BEGIN -->
-
- <ul>
- <li><a href="#NAME">NAME</a></li>
-
- <li>
- <a href="#DESCRIPTION">DESCRIPTION</a>
-
- <ul>
- <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 Perl for Win32?</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 Perl for Win32?</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>
- <!-- INDEX END -->
- <hr />
- <br />
-
- <h1><a name="NAME">NAME</a></h1>
-
- <p>perlwin32faq8 - General programming</p>
- <br />
- <hr />
-
- <h1><a name="DESCRIPTION">DESCRIPTION</a></h1>
-
- <p>General programming questions about Perl for Win32</p>
- <br />
- <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
- Perl for Win32. Check the win32mod document provided with Perl
- for Win32.</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>
- <br />
- <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). <font size="-1">A</font> named pipe can be
- addressed just like a file.</p>
-
- <p>The name of a named pipe is a <font size="-1">UNC</font>
- (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 <font size="-1">SQL</font> 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 <font size="-1">
- CPAN</font> archive that can be used to create a named
- pipe.</p>
-
- <p>If you're starting from scratch, and you have a <font size=
- "-1">TCP/IP</font> infrastructure, consider using sockets
- rather than named pipes for your <font size="-1">IPC</font>
- mechanism.</p>
- <br />
- <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 Perl for Win32. 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>
- <br />
- <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=
- "perlwin32faq1.html#Where_is_the_Perl_for_Win32_inte">Where is
- the Perl for Win32 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>
- <br />
- <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, <font size="-1">
- TCP-SERVER,</font> in the <em>eg</em> directory of your perl
- directory. In general, information on socket programming for
- <font size="-1">UNIX</font> is applicable to Perl for Win32.
- 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>
- <br />
- <pre>
- 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";
- $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 Perl for Win32, 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>
- <br />
- <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 <font size="-1">CPAN,</font>
- and can be installed using the Perl Package Manager <font size=
- "-1">(PPM).</font></p>
-
- <p>Aldo Calpini has developed a Perl for Win32 extension to do
- <font size="-1">FTP</font> and <font size="-1">HTTP</font>
- using the <font size="-1">WININET</font> library. It's in alpha
- testing and is available on his web page at <a href=
- "http://www.divinf.it/dada/perl/">
- http://www.divinf.it/dada/perl/</a></p>
- <br />
- <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 <font size="-1">(LWP)</font> is a
- collection of modules for <font size="-1">WWW</font> access in
- Perl. <font size="-1">LWP</font> is available from <font size=
- "-1">CPAN</font> in source form, or you can install it using
- the Perl Package Manager <font size="-1">(PPM).</font> <font
- size="-1">LWP</font> may also be included with future binary
- releases of Perl.</p>
-
- <p>Aldo Calpini has developed a Perl for Win32 extension to do
- <font size="-1">FTP</font> and <font size="-1">HTTP</font>
- using the <font size="-1">WININET</font> library. It's in alpha
- testing and is available on his web page at <a href=
- "http://www.divinf.it/dada/perl/">
- http://www.divinf.it/dada/perl/</a></p>
- <br />
- <hr />
-
- <h2><a name="How_do_I_manage_user_accounts_wi">How do I manage
- user accounts with Perl for Win32?</a></h2>
-
- <p>There's an extension called Win32::NetAdmin distributed with
- Perl for Win32. It has a pretty low-level interface, but it is
- very possible to manage users and groups with this module.</p>
- <br />
- <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 Perl for
- Win32. To open <em>COM1</em>, just do this:</p>
- <br />
- <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 <font size="-1">I/O</font> 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>
- <br />
- <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>
- <br />
- <pre>
- $path = shift;
- $path = "." unless $path;
-
- opendir( DIR, $path )
- or die "Can't open $path: $!";
-
- while ( $entry = readdir( DIR ) ) {
- $type = ( -d "$path\\$entry" ) ? "dir" : "file"; # $path is crucial!
- print "$type\t$entry\n";
- }
-
- closedir( DIR );
- </pre>
-
- <p>It's a common mistake to leave out the <code>$path</code>
- from the <code>-d</code> check. If you do this, perl thinks
- you're talking about files in the current directory. Since the
- dirs don't <code>-e</code> in your current directory, they
- definitely don't <code>-d</code>. Exceptions are <em>.</em> and
- <em>..</em>, which exist in every directory.</p>
- <br />
- <hr />
-
- <h2><a name="Reading_from_and_writing_to_file">Reading from and
- writing to files mysteriously fails. What's wrong?</a></h2>
-
- <p>On Win32 platforms, there's a big difference between text
- files and binary files. For text files, the \r\n characters are
- translated into \n when read from disk, and the <font size=
- "-1">^Z</font> character is read as an end-of-file marker. For
- binary files, no such translation is used.</p>
-
- <p>Although this works great for text files, it really messes
- things up when you're trying to read and write binary files. If
- the read or write does not abort prematurely because a <font
- size="-1">^Z</font> was found in the file, you will almost
- definitely get incorrect bytes in the file due to \n -> \r\n
- translation.</p>
-
- <p>The problem is that Perl for Win32, and the <font size="-1">
- C</font> runtime library it uses, open file in text mode by
- default. For each file handle you use in Perl for binary data,
- you need to specify that the file handle is in binary mode.
- Fortunately, there's a function, binmode, that does just that.
- See the perlfunc documentation file for details.</p>
-
- <p>This script copies one binary file to another. Note its use
- of binmode to set the mode of the file handle.</p>
- <br />
- <pre>
- open( INFILE, "<$infile" );
- open( OUTFILE, ">$outfile" );
-
- binmode( INFILE ); binmode( OUTFILE ); # crucial for binary files!
-
- while ( read( INFILE, $buffer, 1024 ) ) {
- print OUTFILE $buffer;
- }
-
- close( INFILE );
- close( OUTFILE );
- </pre>
- <br />
- <hr />
-
- <h2><a name="When_I_try_to_open_a_file_I_get">When I try to
- open a file, I get a "bad argument" error.</a></h2>
-
- <p>Win32 platforms use the '\' character as a delimiter for
- paths in a file name (C:\like\this). However, Perl uses the '\'
- character as an escape code, to symbolize a special character
- like the line feed character (\n) or the tab character
- (\t).</p>
-
- <p>So, if you try and open a file like this:</p>
- <br />
- <pre>
- open( MYFILE, "C:\temp\newfile.txt" );
- </pre>
-
- <p>you'll get an error. One solution is to replace each '\'
- with a double-'\', to show that you really mean to use that
- character, not an escape:</p>
- <br />
- <pre>
- open( MYFILE, "C:\\temp\\newfile.txt" );
- </pre>
-
- <p>Another solution is to use non-interpolating single quote
- strings, which lets Perl know not to use any special
- characters:</p>
- <br />
- <pre>
- open( MYFILE, 'C:\temp\newfile.txt' );
- </pre>
-
- <p>Finally, you can also use the / character to separate
- directory components in pathnames. You must avoid using this in
- calls to external programs, because some programs tend to treat
- / as the option-prefix instead of directory separator. However,
- Perl for Win32 (and in fact, all of the Win32 API) understands
- that / is a directory separator, so using / allows you to more
- easily port scripts between <font size="-1">UNIX</font> and
- Win32:</p>
- <br />
- <pre>
- open( MYFILE, '/temp/newfile.txt' );
- </pre>
-
- <p>See the perlop documentation page for more information on
- the differences between single quotes (') and double quotes
- (``).</p>
- <br />
- <hr />
-
- <h2><a name="Why_do_I_get_an_error_using_Perl">Why do I get an
- error using Perl's here-doc syntax (<<), that says "Can't
- find string terminator anywhere before EOF"?</a></h2>
-
- <p>This is a weird error that occurs when your string
- terminator is on the last line of your script. With a script
- like:</p>
- <br />
- <pre>
- print <<"END";
- The snake is old, and his skin is cold.
- END
- </pre>
-
- <p>perl is looking for the word <font size="-1">END</font> on a
- line by itself, followed by a line-feed character (\n). If the
- <font size="-1">END</font> is the last line of your script, you
- have to remember to hit <Enter> after the word <font
- size="-1">END,</font> so that Perl can recognize it as the
- string terminator.</p>
-
- <p>Most <font size="-1">UNIX</font> text editors will do this
- automatically. Most Windows text editors won't. Thus the
- problem.</p>
-
- <p>Note that this can also cause a problem with Perl formats,
- since these are terminated with a single . on a line by itself.
- However, it's much more rare, since programmers often specify
- the format for output at the top rather than at the bottom of a
- file.</p>
- <br />
- <hr />
-
- <h1><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND
- COPYRIGHT</a></h1>
-
- <p>This <font size="-1">FAQ</font> was originally assembled and
- maintained by Evangelo Prodromou. <a href=
- "mailto:evangelo@endcontsw.com">evangelo@endcontsw.com.</a> It
- has been revised and updated by Brian Jepson of O'Reilly and
- Associates, and David Grove and David Dmytryshyn of
- ActiveState.</p>
-
- <p>This <font size="-1">FAQ</font> is in the public domain. If
- you use it, however, please ensure that you give credit to the
- original authors.</p>
-
- <p><!-- beginning of leaf footer--></p>
-
- <table width="100%">
- <tr>
- <td bgcolor="000000" width="70" height="31"><a href=
- "http://www.activestate.com/"><img src="ASbutton.gif" alt=
- "ActiveState Home Page" border="0" width="68" height=
- "30" /></a></td>
-
- <td width="10"> </td>
-
- <td valign="middle" bgcolor="#cc0066"><font face=
- "sans-serif" size="+1" color="#ff99cc">
- Win32 FAQ</font></td>
- </tr>
- </table>
- <!-- end of leaf footer-->
- </body>
- </html>
-
-