home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _5709b364fc81fcea2dbb1ed859516b0b < prev    next >
Text File  |  2004-06-01  |  17KB  |  356 lines

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3.  
  4. <head>
  5. <title>ActivePerl FAQ - General programming</title>
  6. <link rel="STYLESHEET" href="../../Active.css" type="text/css" media="screen">
  7. </head>
  8.  
  9. <body>
  10.  
  11. <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#EAE2BB">
  12. <tr> 
  13. <td width="57"><a target=_blank href="http://www.ActiveState.com/ActivePerl/">
  14. <img src="../../images/activeperl_logo.gif" width="57" height="48" border="0" alt="ActivePerl"></a></td>
  15. <td><div align="center" class="heading">ActivePerl User Guide</div></td>
  16. <td width="112"><a target=_blank  href="http://www.ActiveState.com">
  17. <img src="../../images/AS_logo.gif" width="112" height="48" border="0"  alt="ActiveState" /></a></td>
  18. </tr>
  19. <tr>
  20. <td class="lineColour" colspan="3"></td>
  21. </tr>
  22. </table>
  23.  
  24. <h1>General programming</h1>
  25.  
  26. <ul>
  27.   <li><a class="doc" href="#NAME">NAME</a></li>
  28.   <li><a class="doc" href="#DESCRIPTION">DESCRIPTION</a>
  29.     <ul>
  30.       <li><a class="doc" href="#How_do_I_change_the_Win32_Regist">How do I change the Win32
  31.         Registry?</a></li>
  32.       <li><a class="doc" href="#How_do_I_read_from_write_to_a_na">How do I read from/write
  33.         to a named pipe?</a></li>
  34.       <li><a class="doc" href="#How_do_I_write_socket_scripts_">How do I write socket
  35.         scripts?</a></li>
  36.       <li><a class="doc" href="#What_s_all_this_I_hear_about_not">What's all this I hear
  37.         about not being able to use a socket as a</a></li>
  38.       <li><a class="doc" href="#How_do_I_write_a_sockets_server_">How do I write a sockets
  39.         server in ActivePerl?</a></li>
  40.       <li><a class="doc" href="#How_do_I_send_or_receive_files_b">How do I send or receive
  41.         files by FTP?</a></li>
  42.       <li><a class="doc" href="#How_do_I_send_or_receive_files_b">How do I send or receive
  43.         files by HTTP?</a></li>
  44.       <li><a class="doc" href="#How_do_I_manage_user_accounts_wi">How do I manage user
  45.         accounts with ActivePerl?</a></li>
  46.       <li><a class="doc" href="#How_do_I_read_from_and_write_to_">How do I read from and
  47.         write to serial ports?</a></li>
  48.       <li><a class="doc" href="#Why_doesn_t_the_d_operator_work">Why doesn't the -d operator
  49.         work?</a></li>
  50.       <li><a class="doc" href="#Reading_from_and_writing_to_file">Reading from and writing
  51.         to files mysteriously fails. What's wrong?</a></li>
  52.       <li><a class="doc" href="#When_I_try_to_open_a_file_I_get">When I try to open a file,
  53.         I get a "bad argument" error.</a></li>
  54.       <li><a class="doc" href="#Why_do_I_get_an_error_using_Perl">Why do I get an error
  55.         using Perl's here-doc syntax (<<), that</a></li>
  56.     </ul>
  57.   </li>
  58.   <li><a class="doc" href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></li>
  59. </ul>
  60.  
  61. <h2><a name="NAME">NAME</a></h2>
  62. <p>ActivePerl-faq8 - General programming</p>
  63.  
  64. <h2><a name="DESCRIPTION">DESCRIPTION</a></h2>
  65. <p>General programming questions about ActivePerl</p>
  66.  
  67. <h2><a name="How_do_I_change_the_Win32_Regist">How do I change the Win32
  68. Registry?</a></h2>
  69. <p>There are several Win32 Registry functions provided with ActivePerl. Check
  70. the win32mod document provided with ActivePerl.</p>
  71. <p>If you don't understand how the Registry works, remember that a Registry <em>key</em>
  72. is like a directory, and a Registry <em>value</em> is like a file. There are
  73. several <em>top level keys</em>, and these are kind of like drives.</p>
  74. <p>If you really don't fully understand the Registry, it's probably in your best
  75. interest not to mess around with it.</p>
  76.  
  77. <h2><a name="How_do_I_read_from_write_to_a_na">How do I read from/write to a
  78. named pipe?</a></h2>
  79. <p>Named pipes are a interprocess communcations mechanism, mainly used with
  80. Microsoft operating systems (like Win32 platforms). A named pipe can be
  81. addressed just like a file.</p>
  82. <p>The name of a named pipe is a UNC (Universal Naming Convention) name, and
  83. looks like <em>\\servername\pipe\pipename</em>. <code>servername</code> is the
  84. name of the server you're connecting to, or <code>.</code> for the current
  85. computer. <code>pipe</code> is a constant, and <code>pipename</code> is the name
  86. of the pipe, such as sql for Microsoft SQL Server.</p>
  87. <p>You can use <code>open(),</code> <code>close(),</code> <code>read(),</code>
  88. and <code>print()</code> on a named pipe just like a file. However, you can't
  89. use <code>sysread()</code> or <code>syswrite()</code> on one, because they
  90. aren't really files.</p>
  91. <p>There's a program called Win32Pipe on the CPAN archive that can be used to
  92. create a named pipe.</p>
  93. <p>If you're starting from scratch, and you have a TCP/IP infrastructure,
  94. consider using sockets rather than named pipes for your IPC mechanism.</p>
  95.  
  96. <h2><a name="How_do_I_write_socket_scripts_">How do I write socket scripts?</a></h2>
  97. <p>There are several examples of socket scripts that are distributed with
  98. ActivePerl. They're in the <em>eg</em> subdirectory of your perl directory.</p>
  99. <p>See <a class="doc" href="#How_do_I_write_a_sockets_server_">How do I write a sockets
  100. server in Perl for Win32?</a> for information about sockets servers.</p>
  101.  
  102. <h2><a name="What_s_all_this_I_hear_about_not">What's all this I hear about not
  103. being able to use a socket as a filehandle?</a></h2>
  104. <p>Early versions of Perl for Win32 didn't allow you to read or write to a
  105. socket as if it were a filehandle. The current versions fully support this, and
  106. you shouldn't worry about it too much. If the version that you're using doesn't
  107. work well, get the latest build from ActiveState (see <a class="doc" href="../ActivePerl-faq1.html#Where_is_the_Perl_for_Win32_inte">Where
  108. is the ActivePerl interpreter available?</a>).</p>
  109. <p>You don't have to specify <code>USE_SOCKETS_AS_FILEHANDLES</code> when
  110. building Perl for Win32 to get sockets to work like filehandles. It doesn't <em>hurt</em>,
  111. but it's not necessary.</p>
  112.  
  113. <h2><a name="How_do_I_write_a_sockets_server_">How do I write a sockets server
  114. in Perl for Win32?</a></h2>
  115. <p>There's an example of a socket server, TCP-SERVER, in the <em>eg</em>
  116. directory of your perl directory. In general, information on socket programming
  117. for UNIX is applicable to ActivePerl. See especially the perlipc page of the
  118. documentation.</p>
  119. <p>If you need to develop a server that can service multiple clients at once,
  120. take a look at the IO::Select module. This module allows you to write servers
  121. that can manage open connections from multiple clients. Individual requests on a
  122. connection are queued up, so if your server can provide quick responses, this
  123. approach may work well for you. Here's an example, adapted from Erik Olson's
  124. Programming with Perl Modules (one of the volumes in O'Reilly's Win32 Perl
  125. Resource Kit):</p>
  126. <pre>
  127.     use IO::Socket;
  128.     use IO::Select;
  129.     
  130.     # Create a socket to listen on.
  131.     #
  132.     my $listener = 
  133.       IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
  134.     
  135.     die "Can't create socket for listening: $!" unless $listener;
  136.     print "Listening for connections on port 8008\n";
  137.     
  138.     my $readable = IO::Select->new;     # Create a new IO::Select object
  139.     $readable->add($listener);          # Add the listener to it
  140.     
  141.     while(1) {
  142.     
  143.         # Get a list of sockets that are ready to talk to us.
  144.         #
  145.         my ($ready) = IO::Select->select($readable, undef, undef, undef);
  146.         foreach my $s (@$ready) {
  147.             
  148.             # Is it a new connection?
  149.             #
  150.             if($s == $listener) {
  151.             
  152.                 # Accept the connection and add it to our readable list.
  153.                 #
  154.                 my $new_sock = $listener->accept;
  155.                 $readable->add($new_sock) if $new_sock;
  156.                 
  157.                 print $new_sock "Welcome!\r\n";
  158.                 
  159.             } else {  # It's an established connection
  160.             
  161.                 my $buf = <$s>;   # Try to read a line
  162.                 
  163.                 # Was there anyone on the other end?
  164.                 #
  165.                 if( defined $buf ) {
  166.                     
  167.                     # If they said goodbye, close the socket. If not,
  168.                     # echo what they said to us.
  169.                     #
  170.                     if ($buf =~ /goodbye/i) {
  171.                         print $s "See you later!\n";
  172.                         $readable->remove($s);
  173.                         $s->close;
  174.                     } else {
  175.                         print $s "You said: $buf\n";
  176.                     }
  177.                     
  178.                 } else { # The client disconnected.
  179.                 
  180.                     $readable->remove($s);
  181.                     $s->close;
  182.                     print STDERR "Client Connection closed\n";
  183.                     
  184.                 }
  185.             }
  186.         }
  187.     }
  188. </pre>
  189. <p>For more information, see the IO::Socket and IO::Select documentation. It is
  190. also possible to write a multithreaded server using ActivePerl, if threads are
  191. enabled in the version of Perl you are using. However, threading is still
  192. somewhat experimental in Perl 5.005, so use this feature with caution.</p>
  193.  
  194. <h2><a name="How_do_I_send_or_receive_files_b">How do I send or receive files by
  195. FTP?</a></h2>
  196. <p>See the Net::FTP module. Net::FTP is part of the libnet bundle, which is
  197. available from CPAN, and can be installed using the Perl Package Manager (PPM).</p>
  198. <p>Aldo Calpini has developed a ActivePerl extension to do FTP and HTTP using
  199. 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>
  200.  
  201. <h2><a name="How_do_I_send_or_receive_files_b">How do I send or receive files by
  202. HTTP?</a></h2>
  203. <p>The libwww-perl bundle (LWP) is a collection of modules for WWW access in
  204. Perl. LWP is available from CPAN in source form, or you can install it using the
  205. Perl Package Manager (PPM). LWP may also be included with future binary releases
  206. of Perl.</p>
  207. <p>Aldo Calpini has developed a ActivePerl extension to do FTP and HTTP using
  208. 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>
  209.  
  210. <h2><a name="How_do_I_manage_user_accounts_wi">How do I manage user accounts
  211. with ActivePerl?</a></h2>
  212. <p>There's an extension called Win32::NetAdmin distributed with ActivePerl. It
  213. has a pretty low-level interface, but it is very possible to manage users and
  214. groups with this module.</p>
  215.  
  216. <h2><a name="How_do_I_read_from_and_write_to_">How do I read from and write to
  217. serial ports?</a></h2>
  218. <p>Serial ports can be opened just like files in ActivePerl. To open <em>COM1</em>,
  219. just do this:</p>
  220. <pre>
  221.     open( PORT, "+>COM1" ) or die "Can't open COM1: $!";
  222. </pre>
  223. <p>You should be able to read from and write to the file handle using the
  224. standard I/O functions (read() and <code>print()),</code> but not the system
  225. functions (sysread() and <code>syswrite()).</code></p>
  226. <p>It has been noted (but not tested) that modems that use the Hayes command set
  227. require a carriage return (\r) rather than a line feed (\n) at the end of the
  228. command.</p>
  229.  
  230. <h2><a name="Why_doesn_t_the_d_operator_work">Why doesn't the -d operator work?</a></h2>
  231. <p>It does, in fact, work. However, people tend to use it incorrectly and get
  232. bad results. To check for all the subdirectories in a directory, try code like
  233. this:</p>
  234. <pre>
  235.     $path = shift;
  236.     $path = "." unless $path;
  237.     
  238.     opendir( DIR, $path )
  239.         or die "Can't open $path: $!";
  240.     
  241.     while ( $entry = readdir( DIR ) ) {
  242.         $type = ( -d "$path\\$entry" ) ? "dir" : "file"; # $path is crucial!
  243.         print "$type\t$entry\n";
  244.     }
  245.     
  246.     closedir( DIR );
  247. </pre>
  248. <p>It's a common mistake to leave out the <code>$path</code> from the <code>-d</code>
  249. check. If you do this, perl thinks you're talking about files in the current
  250. directory. Since the dirs don't <code>-e</code> in your current directory, they
  251. definitely don't <code>-d</code>. Exceptions are <em>.</em> and <em>..</em>,
  252. which exist in every directory.</p>
  253.  
  254. <h2><a name="Reading_from_and_writing_to_file">Reading from and writing to files
  255. mysteriously fails. What's wrong?</a></h2>
  256. <p>On Win32 platforms, there's a big difference between text files and binary
  257. files. For text files, the \r\n characters are translated into \n when read from
  258. disk, and the ^Z character is read as an end-of-file marker. For binary files,
  259. no such translation is used.</p>
  260. <p>Although this works great for text files, it really messes things up when
  261. you're trying to read and write binary files. If the read or write does not
  262. abort prematurely because a ^Z was found in the file, you will almost definitely
  263. get incorrect bytes in the file due to \n -> \r\n translation.</p>
  264. <p>The problem is that ActivePerl, and the C runtime library it uses, open file
  265. in text mode by default. For each file handle you use in Perl for binary data,
  266. you need to specify that the file handle is in binary mode. Fortunately, there's
  267. a function, binmode, that does just that. See the perlfunc documentation file
  268. for details.</p>
  269. <p>This script copies one binary file to another. Note its use of binmode to set
  270. the mode of the file handle.</p>
  271. <pre>
  272.     open( INFILE, "<$infile" );
  273.     open( OUTFILE, ">$outfile" );
  274.     
  275.     binmode( INFILE ); binmode( OUTFILE ); # crucial for binary files!
  276.     
  277.     while ( read( INFILE, $buffer, 1024 ) ) {
  278.         print OUTFILE $buffer;
  279.     }
  280.     
  281.     close( INFILE ); 
  282.     close( OUTFILE );
  283. </pre>
  284.  
  285. <h2><a name="When_I_try_to_open_a_file_I_get">When I try to open a file, I get a
  286. "bad argument" error.</a></h2>
  287. <p>Win32 platforms use the '\' character as a delimiter for paths in a file name
  288. (C:\like\this). However, Perl uses the '\' character as an escape code, to
  289. symbolize a special character like the line feed character (\n) or the tab
  290. character (\t).</p>
  291. <p>So, if you try and open a file like this:</p>
  292. <pre>
  293.     open( MYFILE, "C:\temp\newfile.txt" );
  294. </pre>
  295. <p>you'll get an error. One solution is to replace each '\' with a double-'\',
  296. to show that you really mean to use that character, not an escape:</p>
  297. <pre>
  298.    open( MYFILE, "C:\\temp\\newfile.txt" );
  299. </pre>
  300. <p>Another solution is to use non-interpolating single quote strings, which lets
  301. Perl know not to use any special characters:</p>
  302. <pre>
  303.    open( MYFILE, 'C:\temp\newfile.txt' );
  304. </pre>
  305. <p>Finally, you can also use the / character to separate directory components in
  306. pathnames. You must avoid using this in calls to external programs, because some
  307. programs tend to treat / as the option-prefix instead of directory separator.
  308. However, ActivePerl (and in fact, all of the Win32 API) understands that / is a
  309. directory separator, so using / allows you to more easily port scripts between
  310. UNIX and Win32:</p>
  311. <pre>
  312.    open( MYFILE, '/temp/newfile.txt' );
  313. </pre>
  314. <p>See the perlop documentation page for more information on the differences
  315. between single quotes (') and double quotes (``).</p>
  316.  
  317. <h2><a name="Why_do_I_get_an_error_using_Perl">Why do I get an error using
  318. Perl's here-doc syntax (<<), that says "Can't find string terminator
  319. anywhere before EOF"?</a></h2>
  320. <p>This is a weird error that occurs when your string terminator is on the last
  321. line of your script. With a script like:</p>
  322. <pre>
  323.     print <<"END";
  324.     The snake is old, and his skin is cold.
  325.     END
  326. </pre>
  327. <p>perl is looking for the word END on a line by itself, followed by a line-feed
  328. character (\n). If the END is the last line of your script, you have to remember
  329. to hit <Enter> after the word END, so that Perl can recognize it as the
  330. string terminator.</p>
  331. <p>Most UNIX text editors will do this automatically. Most Windows text editors
  332. won't. Thus the problem.</p>
  333. <p>Note that this can also cause a problem with Perl formats, since these are
  334. terminated with a single . on a line by itself. However, it's much more rare,
  335. since programmers often specify the format for output at the top rather than at
  336. the bottom of a file.</p>
  337.  
  338. <h2><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</a></h2>
  339. <p>This FAQ was originally assembled and maintained by Evangelo Prodromou. It
  340. has been revised and updated by Brian Jepson of O'Reilly & Associates, David
  341. Grove, David Dmytryshyn and David Sparks of ActiveState.</p>
  342. <p>This FAQ is in the public domain. If you use it, however, please ensure that
  343. you give credit to the original authors.</p>
  344.                        
  345. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  346.   <tr>
  347.     <td class="block" valign="MIDDLE" width="100%" bgcolor="#cccccc"><strong>
  348.       <p class="block"> ActivePerl FAQ - General programming</p>
  349.       </strong></td>
  350.   </tr>
  351. </table>
  352.  
  353. </body>
  354.  
  355. </html>
  356.