home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / perlwin32faq8.html < prev    next >
Encoding:
Extensible Markup Language  |  1999-10-15  |  19.5 KB  |  533 lines

  1. <?xml version="1.0"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3.     "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
  4. <html xmlns="http://www.w3.org/TR/xhtml1">
  5.   <head>
  6.     <title>perlwin32faq8 - General programming</title>
  7.     <link rev="made" href="mailto:bjepson@debian.ids.net" />
  8.     <meta name="GENERATOR" charset="iso-8859-1" />
  9.     <link rel="STYLESHEET" href="../win32prk.css" type="text/css"
  10.     media="screen" />
  11.   </head>
  12.  
  13.   <body bgcolor="#ffffff">
  14.     <!-- beginning of leaf header-->
  15.  
  16.     <table width="100%">
  17.       <tr>
  18.         <td bgcolor="000000" width="70" height="31"><a href= 
  19.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  20.         "ActiveState Home Page" border="0" width="68" height=
  21.         "30" /></a></td>
  22.  
  23.         <td width="10" bgcolor="#ffffff"> </td>
  24.  
  25.         <td valign="middle" bgcolor="#cc0066"><font face=
  26.         "sans-serif" size="+1" color="#ff99cc">   
  27.         Win32 FAQ</font></td>
  28.       </tr>
  29.     </table>
  30.     <!-- end of leaf content--><!-- INDEX BEGIN -->
  31.  
  32.     <ul>
  33.       <li><a href="#NAME">NAME</a></li>
  34.  
  35.       <li>
  36.         <a href="#DESCRIPTION">DESCRIPTION</a> 
  37.  
  38.         <ul>
  39.           <li><a href="#How_do_I_change_the_Win32_Regist">How do I
  40.           change the Win32 Registry?</a></li>
  41.  
  42.           <li><a href="#How_do_I_read_from_write_to_a_na">How do I
  43.           read from/write to a named pipe?</a></li>
  44.  
  45.           <li><a href="#How_do_I_write_socket_scripts_">How do I
  46.           write socket scripts?</a></li>
  47.  
  48.           <li><a href="#What_s_all_this_I_hear_about_not">What's
  49.           all this I hear about not being able to use a socket as
  50.           a</a></li>
  51.  
  52.           <li><a href="#How_do_I_write_a_sockets_server_">How do I
  53.           write a sockets server in Perl for Win32?</a></li>
  54.  
  55.           <li><a href="#How_do_I_send_or_receive_files_b">How do I
  56.           send or receive files by FTP?</a></li>
  57.  
  58.           <li><a href="#How_do_I_send_or_receive_files_b">How do I
  59.           send or receive files by HTTP?</a></li>
  60.  
  61.           <li><a href="#How_do_I_manage_user_accounts_wi">How do I
  62.           manage user accounts with Perl for Win32?</a></li>
  63.  
  64.           <li><a href="#How_do_I_read_from_and_write_to_">How do I
  65.           read from and write to serial ports?</a></li>
  66.  
  67.           <li><a href="#Why_doesn_t_the_d_operator_work">Why
  68.           doesn't the -d operator work?</a></li>
  69.  
  70.           <li><a href="#Reading_from_and_writing_to_file">Reading
  71.           from and writing to files mysteriously fails. What's
  72.           wrong?</a></li>
  73.  
  74.           <li><a href="#When_I_try_to_open_a_file_I_get">When I try
  75.           to open a file, I get a "bad argument" error.</a></li>
  76.  
  77.           <li><a href="#Why_do_I_get_an_error_using_Perl">Why do I
  78.           get an error using Perl's here-doc syntax (<<),
  79.           that</a></li>
  80.         </ul>
  81.       </li>
  82.  
  83.       <li><a href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND
  84.       COPYRIGHT</a></li>
  85.     </ul>
  86.     <!-- INDEX END -->
  87.     <hr />
  88.     <br />
  89.  
  90.     <h1><a name="NAME">NAME</a></h1>
  91.  
  92.     <p>perlwin32faq8 - General programming</p>
  93.     <br />
  94.     <hr />
  95.  
  96.     <h1><a name="DESCRIPTION">DESCRIPTION</a></h1>
  97.  
  98.     <p>General programming questions about Perl for Win32</p>
  99.     <br />
  100.     <hr />
  101.  
  102.     <h2><a name="How_do_I_change_the_Win32_Regist">How do I change
  103.     the Win32 Registry?</a></h2>
  104.  
  105.     <p>There are several Win32 Registry functions provided with
  106.     Perl for Win32. Check the win32mod document provided with Perl
  107.     for Win32.</p>
  108.  
  109.     <p>If you don't understand how the Registry works, remember
  110.     that a Registry <em>key</em> is like a directory, and a
  111.     Registry <em>value</em> is like a file. There are several <em>
  112.     top level keys</em>, and these are kind of like drives.</p>
  113.  
  114.     <p>If you really don't fully understand the Registry, it's
  115.     probably in your best interest not to mess around with it.</p>
  116.     <br />
  117.     <hr />
  118.  
  119.     <h2><a name="How_do_I_read_from_write_to_a_na">How do I read
  120.     from/write to a named pipe?</a></h2>
  121.  
  122.     <p>Named pipes are a interprocess communcations mechanism,
  123.     mainly used with Microsoft operating systems (like Win32
  124.     platforms). <font size="-1">A</font> named pipe can be
  125.     addressed just like a file.</p>
  126.  
  127.     <p>The name of a named pipe is a <font size="-1">UNC</font>
  128.     (Universal Naming Convention) name, and looks like <em>
  129.     \\servername\pipe\pipename</em>. <code>servername</code> is the
  130.     name of the server you're connecting to, or <code>.</code> for
  131.     the current computer. <code>pipe</code> is a constant, and
  132.     <code>pipename</code> is the name of the pipe, such as sql for
  133.     Microsoft <font size="-1">SQL</font> Server.</p>
  134.  
  135.     <p>You can use <code>open(),</code> <code>close(),</code>
  136.     <code>read(),</code> and <code>print()</code> on a named pipe
  137.     just like a file. However, you can't use <code>sysread()</code>
  138.     or <code>syswrite()</code> on one, because they aren't really
  139.     files.</p>
  140.  
  141.     <p>There's a program called Win32Pipe on the <font size="-1">
  142.     CPAN</font> archive that can be used to create a named
  143.     pipe.</p>
  144.  
  145.     <p>If you're starting from scratch, and you have a <font size= 
  146.     "-1">TCP/IP</font> infrastructure, consider using sockets
  147.     rather than named pipes for your <font size="-1">IPC</font>
  148.     mechanism.</p>
  149.     <br />
  150.     <hr />
  151.  
  152.     <h2><a name="How_do_I_write_socket_scripts_">How do I write
  153.     socket scripts?</a></h2>
  154.  
  155.     <p>There are several examples of socket scripts that are
  156.     distributed with Perl for Win32. They're in the <em>eg</em>
  157.     subdirectory of your perl directory.</p>
  158.  
  159.     <p>See <a href="#How_do_I_write_a_sockets_server_">How do I
  160.     write a sockets server in Perl for Win32?</a> for information
  161.     about sockets servers.</p>
  162.     <br />
  163.     <hr />
  164.  
  165.     <h2><a name="What_s_all_this_I_hear_about_not">What's all this
  166.     I hear about not being able to use a socket as a
  167.     filehandle?</a></h2>
  168.  
  169.     <p>Early versions of Perl for Win32 didn't allow you to read or
  170.     write to a socket as if it were a filehandle. The current
  171.     versions fully support this, and you shouldn't worry about it
  172.     too much. If the version that you're using doesn't work well,
  173.     get the latest build from ActiveState (see <a href= 
  174.     "perlwin32faq1.html#Where_is_the_Perl_for_Win32_inte">Where is
  175.     the Perl for Win32 interpreter available?</a>).</p>
  176.  
  177.     <p>You don't have to specify <code>
  178.     USE_SOCKETS_AS_FILEHANDLES</code> when building Perl for Win32
  179.     to get sockets to work like filehandles. It doesn't <em>
  180.     hurt</em>, but it's not necessary.</p>
  181.     <br />
  182.     <hr />
  183.  
  184.     <h2><a name="How_do_I_write_a_sockets_server_">How do I write a
  185.     sockets server in Perl for Win32?</a></h2>
  186.  
  187.     <p>There's an example of a socket server, <font size="-1">
  188.     TCP-SERVER,</font> in the <em>eg</em> directory of your perl
  189.     directory. In general, information on socket programming for
  190.     <font size="-1">UNIX</font> is applicable to Perl for Win32.
  191.     See especially the perlipc page of the documentation.</p>
  192.  
  193.     <p>If you need to develop a server that can service multiple
  194.     clients at once, take a look at the IO::Select module. This
  195.     module allows you to write servers that can manage open
  196.     connections from multiple clients. Individual requests on a
  197.     connection are queued up, so if your server can provide quick
  198.     responses, this approach may work well for you. Here's an
  199.     example, adapted from Erik Olson's Programming with Perl
  200.     Modules (one of the volumes in O'Reilly's Win32 Perl Resource
  201.     Kit):</p>
  202.     <br />
  203. <pre>
  204.     use IO::Socket;
  205.     use IO::Select;
  206.     
  207.     # Create a socket to listen on.
  208.     #
  209.     my $listener = 
  210.       IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
  211.     
  212.     die "Can't create socket for listening: $!" unless $listener;
  213.     print "Listening for connections on port 8008\n";
  214.     
  215.     my $readable = IO::Select->new;     # Create a new IO::Select object
  216.     $readable->add($listener);          # Add the listener to it
  217.     
  218.     while(1) {
  219.     
  220.         # Get a list of sockets that are ready to talk to us.
  221.         #
  222.         my ($ready) = IO::Select->select($readable, undef, undef, undef);
  223.         foreach my $s (@$ready) {
  224.             
  225.             # Is it a new connection?
  226.             #
  227.             if($s == $listener) {
  228.             
  229.                 # Accept the connection and add it to our readable list.
  230.                 #
  231.                 my $new_sock = $listener->accept;
  232.                 $readable->add($new_sock) if $new_sock;
  233.                 
  234.                 print $new_sock "Welcome!\r\n";
  235.                 
  236.             } else {  # It's an established connection
  237.             
  238.                 my $buf = <$s>;   # Try to read a line
  239.                 
  240.                 # Was there anyone on the other end?
  241.                 #
  242.                 if( defined $buf ) {
  243.                     
  244.                     # If they said goodbye, close the socket. If not,
  245.                     # echo what they said to us.
  246.                     #
  247.                     if ($buf =~ /goodbye/i) {
  248.                         print $s "See you later!\n";
  249.                         $readable->remove($s);
  250.                         $s->close;
  251.                     } else {
  252.                         print $s "You said: $buf\n";
  253.                     }
  254.                     
  255.                 } else { # The client disconnected.
  256.                 
  257.                     $readable->remove($s);
  258.                     $s->close;
  259.                     print STDERR "Client Connection closed\n";
  260.                     
  261.                 }
  262.             }
  263.         }
  264.     }
  265. </pre>
  266.  
  267.     <p>For more information, see the IO::Socket and IO::Select
  268.     documentation. It is also possible to write a multithreaded
  269.     server using Perl for Win32, if threads are enabled in the
  270.     version of Perl you are using. However, threading is still
  271.     somewhat experimental in Perl 5.005, so use this feature with
  272.     caution.</p>
  273.     <br />
  274.     <hr />
  275.  
  276.     <h2><a name="How_do_I_send_or_receive_files_b">How do I send or
  277.     receive files by FTP?</a></h2>
  278.  
  279.     <p>See the Net::FTP module. Net::FTP is part of the libnet
  280.     bundle, which is available from <font size="-1">CPAN,</font>
  281.     and can be installed using the Perl Package Manager <font size= 
  282.     "-1">(PPM).</font></p>
  283.  
  284.     <p>Aldo Calpini has developed a Perl for Win32 extension to do
  285.     <font size="-1">FTP</font> and <font size="-1">HTTP</font>
  286.     using the <font size="-1">WININET</font> library. It's in alpha
  287.     testing and is available on his web page at <a href= 
  288.     "http://www.divinf.it/dada/perl/">
  289.     http://www.divinf.it/dada/perl/</a></p>
  290.     <br />
  291.     <hr />
  292.  
  293.     <h2><a name="How_do_I_send_or_receive_files_b">How do I send or
  294.     receive files by HTTP?</a></h2>
  295.  
  296.     <p>The libwww-perl bundle <font size="-1">(LWP)</font> is a
  297.     collection of modules for <font size="-1">WWW</font> access in
  298.     Perl. <font size="-1">LWP</font> is available from <font size= 
  299.     "-1">CPAN</font> in source form, or you can install it using
  300.     the Perl Package Manager <font size="-1">(PPM).</font> <font
  301.     size="-1">LWP</font> may also be included with future binary
  302.     releases of Perl.</p>
  303.  
  304.     <p>Aldo Calpini has developed a Perl for Win32 extension to do
  305.     <font size="-1">FTP</font> and <font size="-1">HTTP</font>
  306.     using the <font size="-1">WININET</font> library. It's in alpha
  307.     testing and is available on his web page at <a href= 
  308.     "http://www.divinf.it/dada/perl/">
  309.     http://www.divinf.it/dada/perl/</a></p>
  310.     <br />
  311.     <hr />
  312.  
  313.     <h2><a name="How_do_I_manage_user_accounts_wi">How do I manage
  314.     user accounts with Perl for Win32?</a></h2>
  315.  
  316.     <p>There's an extension called Win32::NetAdmin distributed with
  317.     Perl for Win32. It has a pretty low-level interface, but it is
  318.     very possible to manage users and groups with this module.</p>
  319.     <br />
  320.     <hr />
  321.  
  322.     <h2><a name="How_do_I_read_from_and_write_to_">How do I read
  323.     from and write to serial ports?</a></h2>
  324.  
  325.     <p>Serial ports can be opened just like files in Perl for
  326.     Win32. To open <em>COM1</em>, just do this:</p>
  327.     <br />
  328. <pre>
  329.     open( PORT, "+>COM1" ) or die "Can't open COM1: $!";
  330. </pre>
  331.  
  332.     <p>You should be able to read from and write to the file handle
  333.     using the standard <font size="-1">I/O</font> functions (read()
  334.     and <code>print()),</code> but not the system functions
  335.     (sysread() and <code>syswrite()).</code></p>
  336.  
  337.     <p>It has been noted (but not tested) that modems that use the
  338.     Hayes command set require a carriage return (\r) rather than a
  339.     line feed (\n) at the end of the command.</p>
  340.     <br />
  341.     <hr />
  342.  
  343.     <h2><a name="Why_doesn_t_the_d_operator_work">Why doesn't the
  344.     -d operator work?</a></h2>
  345.  
  346.     <p>It does, in fact, work. However, people tend to use it
  347.     incorrectly and get bad results. To check for all the
  348.     subdirectories in a directory, try code like this:</p>
  349.     <br />
  350. <pre>
  351.     $path = shift;
  352.     $path = "." unless $path;
  353.     
  354.     opendir( DIR, $path )
  355.         or die "Can't open $path: $!";
  356.     
  357.     while ( $entry = readdir( DIR ) ) {
  358.         $type = ( -d "$path\\$entry" ) ? "dir" : "file"; # $path is crucial!
  359.         print "$type\t$entry\n";
  360.     }
  361.     
  362.     closedir( DIR );
  363. </pre>
  364.  
  365.     <p>It's a common mistake to leave out the <code>$path</code>
  366.     from the <code>-d</code> check. If you do this, perl thinks
  367.     you're talking about files in the current directory. Since the
  368.     dirs don't <code>-e</code> in your current directory, they
  369.     definitely don't <code>-d</code>. Exceptions are <em>.</em> and
  370.     <em>..</em>, which exist in every directory.</p>
  371.     <br />
  372.     <hr />
  373.  
  374.     <h2><a name="Reading_from_and_writing_to_file">Reading from and
  375.     writing to files mysteriously fails. What's wrong?</a></h2>
  376.  
  377.     <p>On Win32 platforms, there's a big difference between text
  378.     files and binary files. For text files, the \r\n characters are
  379.     translated into \n when read from disk, and the <font size=
  380.     "-1">^Z</font> character is read as an end-of-file marker. For
  381.     binary files, no such translation is used.</p>
  382.  
  383.     <p>Although this works great for text files, it really messes
  384.     things up when you're trying to read and write binary files. If
  385.     the read or write does not abort prematurely because a <font
  386.     size="-1">^Z</font> was found in the file, you will almost
  387.     definitely get incorrect bytes in the file due to \n -> \r\n
  388.     translation.</p>
  389.  
  390.     <p>The problem is that Perl for Win32, and the <font size="-1">
  391.     C</font> runtime library it uses, open file in text mode by
  392.     default. For each file handle you use in Perl for binary data,
  393.     you need to specify that the file handle is in binary mode.
  394.     Fortunately, there's a function, binmode, that does just that.
  395.     See the perlfunc documentation file for details.</p>
  396.  
  397.     <p>This script copies one binary file to another. Note its use
  398.     of binmode to set the mode of the file handle.</p>
  399.     <br />
  400. <pre>
  401.     open( INFILE, "<$infile" );
  402.     open( OUTFILE, ">$outfile" );
  403.     
  404.     binmode( INFILE ); binmode( OUTFILE ); # crucial for binary files!
  405.     
  406.     while ( read( INFILE, $buffer, 1024 ) ) {
  407.         print OUTFILE $buffer;
  408.     }
  409.     
  410.     close( INFILE ); 
  411.     close( OUTFILE );
  412. </pre>
  413.     <br />
  414.     <hr />
  415.  
  416.     <h2><a name="When_I_try_to_open_a_file_I_get">When I try to
  417.     open a file, I get a "bad argument" error.</a></h2>
  418.  
  419.     <p>Win32 platforms use the '\' character as a delimiter for
  420.     paths in a file name (C:\like\this). However, Perl uses the '\'
  421.     character as an escape code, to symbolize a special character
  422.     like the line feed character (\n) or the tab character
  423.     (\t).</p>
  424.  
  425.     <p>So, if you try and open a file like this:</p>
  426.     <br />
  427. <pre>
  428.     open( MYFILE, "C:\temp\newfile.txt" );
  429. </pre>
  430.  
  431.     <p>you'll get an error. One solution is to replace each '\'
  432.     with a double-'\', to show that you really mean to use that
  433.     character, not an escape:</p>
  434.     <br />
  435. <pre>
  436.    open( MYFILE, "C:\\temp\\newfile.txt" );
  437. </pre>
  438.  
  439.     <p>Another solution is to use non-interpolating single quote
  440.     strings, which lets Perl know not to use any special
  441.     characters:</p>
  442.     <br />
  443. <pre>
  444.    open( MYFILE, 'C:\temp\newfile.txt' );
  445. </pre>
  446.  
  447.     <p>Finally, you can also use the / character to separate
  448.     directory components in pathnames. You must avoid using this in
  449.     calls to external programs, because some programs tend to treat
  450.     / as the option-prefix instead of directory separator. However,
  451.     Perl for Win32 (and in fact, all of the Win32 API) understands
  452.     that / is a directory separator, so using / allows you to more
  453.     easily port scripts between <font size="-1">UNIX</font> and
  454.     Win32:</p>
  455.     <br />
  456. <pre>
  457.    open( MYFILE, '/temp/newfile.txt' );
  458. </pre>
  459.  
  460.     <p>See the perlop documentation page for more information on
  461.     the differences between single quotes (') and double quotes
  462.     (``).</p>
  463.     <br />
  464.     <hr />
  465.  
  466.     <h2><a name="Why_do_I_get_an_error_using_Perl">Why do I get an
  467.     error using Perl's here-doc syntax (<<), that says "Can't
  468.     find string terminator anywhere before EOF"?</a></h2>
  469.  
  470.     <p>This is a weird error that occurs when your string
  471.     terminator is on the last line of your script. With a script
  472.     like:</p>
  473.     <br />
  474. <pre>
  475.     print <<"END";
  476.     The snake is old, and his skin is cold.
  477.     END
  478. </pre>
  479.  
  480.     <p>perl is looking for the word <font size="-1">END</font> on a
  481.     line by itself, followed by a line-feed character (\n). If the
  482.     <font size="-1">END</font> is the last line of your script, you
  483.     have to remember to hit <Enter> after the word <font
  484.     size="-1">END,</font> so that Perl can recognize it as the
  485.     string terminator.</p>
  486.  
  487.     <p>Most <font size="-1">UNIX</font> text editors will do this
  488.     automatically. Most Windows text editors won't. Thus the
  489.     problem.</p>
  490.  
  491.     <p>Note that this can also cause a problem with Perl formats,
  492.     since these are terminated with a single . on a line by itself.
  493.     However, it's much more rare, since programmers often specify
  494.     the format for output at the top rather than at the bottom of a
  495.     file.</p>
  496.     <br />
  497.     <hr />
  498.  
  499.     <h1><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND
  500.     COPYRIGHT</a></h1>
  501.  
  502.     <p>This <font size="-1">FAQ</font> was originally assembled and
  503.     maintained by Evangelo Prodromou. <a href= 
  504.     "mailto:evangelo@endcontsw.com">evangelo@endcontsw.com.</a> It
  505.     has been revised and updated by Brian Jepson of O'Reilly and
  506.     Associates, and David Grove and David Dmytryshyn of
  507.     ActiveState.</p>
  508.  
  509.     <p>This <font size="-1">FAQ</font> is in the public domain. If
  510.     you use it, however, please ensure that you give credit to the
  511.     original authors.</p>
  512.  
  513.     <p><!-- beginning of leaf footer--></p>
  514.  
  515.     <table width="100%">
  516.       <tr>
  517.         <td bgcolor="000000" width="70" height="31"><a href= 
  518.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  519.         "ActiveState Home Page" border="0" width="68" height=
  520.         "30" /></a></td>
  521.  
  522.         <td width="10"> </td>
  523.  
  524.         <td valign="middle" bgcolor="#cc0066"><font face=
  525.         "sans-serif" size="+1" color="#ff99cc">   
  526.         Win32 FAQ</font></td>
  527.       </tr>
  528.     </table>
  529.     <!-- end  of leaf footer-->
  530.   </body>
  531. </html>
  532.  
  533.