Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perlfaq8.html
blobaba64b85cf59e1613552bca5f97e2b59b46e6408
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perlfaq8 - System Interaction</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perlfaq8 - System Interaction</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#how_do_i_find_out_which_operating_system_i_m_running_under">How do I find out which operating system I'm running under?</a></li>
27 <li><a href="#how_come_exec___doesn_t_return">How come <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec()</code></a> doesn't return?</a></li>
28 <li><a href="#how_do_i_do_fancy_stuff_with_the_keyboard_screen_mouse">How do I do fancy stuff with the keyboard/screen/mouse?</a></li>
29 <li><a href="#how_do_i_print_something_out_in_color">How do I print something out in color?</a></li>
30 <li><a href="#how_do_i_read_just_one_key_without_waiting_for_a_return_key">How do I read just one key without waiting for a return key?</a></li>
31 <li><a href="#how_do_i_check_whether_input_is_ready_on_the_keyboard">How do I check whether input is ready on the keyboard?</a></li>
32 <li><a href="#how_do_i_clear_the_screen">How do I clear the screen?</a></li>
33 <li><a href="#how_do_i_get_the_screen_size">How do I get the screen size?</a></li>
34 <li><a href="#how_do_i_ask_the_user_for_a_password">How do I ask the user for a password?</a></li>
35 <li><a href="#how_do_i_read_and_write_the_serial_port">How do I read and write the serial port?</a></li>
36 <li><a href="#how_do_i_decode_encrypted_password_files">How do I decode encrypted password files?</a></li>
37 <li><a href="#how_do_i_start_a_process_in_the_background">How do I start a process in the background?</a></li>
38 <li><a href="#how_do_i_trap_control_characters_signals">How do I trap control characters/signals?</a></li>
39 <li><a href="#how_do_i_modify_the_shadow_password_file_on_a_unix_system">How do I modify the shadow password file on a Unix system?</a></li>
40 <li><a href="#how_do_i_set_the_time_and_date">How do I set the time and date?</a></li>
41 <li><a href="#how_can_i_sleep___or_alarm___for_under_a_second">How can I <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sleep"><code>sleep()</code></a> or <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_alarm"><code>alarm()</code></a> for under a second?</a></li>
42 <li><a href="#how_can_i_measure_time_under_a_second">How can I measure time under a second?</a></li>
43 <li><a href="#how_can_i_do_an_atexit___or_setjmp___longjmp____exception_handling_">How can I do an <code>atexit()</code> or setjmp()/longjmp()? (Exception handling)</a></li>
44 <li><a href="#why_doesn_t_my_sockets_program_work_under_system_v__solaris__what_does_the_error_message_protocol_not_supported_mean">Why doesn't my sockets program work under System V (Solaris)? What does the error message ``Protocol not supported'' mean?</a></li>
45 <li><a href="#how_can_i_call_my_system_s_unique_c_functions_from_perl">How can I call my system's unique C functions from Perl?</a></li>
46 <li><a href="#where_do_i_get_the_include_files_to_do_ioctl___or_syscall__">Where do I get the include files to do <code>ioctl()</code> or syscall()?</a></li>
47 <li><a href="#why_do_setuid_perl_scripts_complain_about_kernel_problems">Why do setuid perl scripts complain about kernel problems?</a></li>
48 <li><a href="#how_can_i_open_a_pipe_both_to_and_from_a_command">How can I open a pipe both to and from a command?</a></li>
49 <li><a href="#why_can_t_i_get_the_output_of_a_command_with_system__">Why can't I get the output of a command with system()?</a></li>
50 <li><a href="#how_can_i_capture_stderr_from_an_external_command">How can I capture STDERR from an external command?</a></li>
51 <li><a href="#why_doesn_t_open___return_an_error_when_a_pipe_open_fails">Why doesn't <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> return an error when a pipe open fails?</a></li>
52 <li><a href="#what_s_wrong_with_using_backticks_in_a_void_context">What's wrong with using backticks in a void context?</a></li>
53 <li><a href="#how_can_i_call_backticks_without_shell_processing">How can I call backticks without shell processing?</a></li>
54 <li><a href="#why_can_t_my_script_read_from_stdin_after_i_gave_it_eof___d_on_unix___z_on_msdos_">Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</a></li>
55 <li><a href="#how_can_i_convert_my_shell_script_to_perl">How can I convert my shell script to perl?</a></li>
56 <li><a href="#can_i_use_perl_to_run_a_telnet_or_ftp_session">Can I use perl to run a telnet or ftp session?</a></li>
57 <li><a href="#how_can_i_write_expect_in_perl">How can I write expect in Perl?</a></li>
58 <li><a href="#is_there_a_way_to_hide_perl_s_command_line_from_programs_such_as_ps">Is there a way to hide perl's command line from programs such as ``ps''?</a></li>
59 <li><a href="#i__changed_directory__modified_my_environment__in_a_perl_script__how_come_the_change_disappeared_when_i_exited_the_script_how_do_i_get_my_changes_to_be_visible">I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?</a></li>
60 <li><a href="#how_do_i_close_a_process_s_filehandle_without_waiting_for_it_to_complete">How do I close a process's filehandle without waiting for it to complete?</a></li>
61 <li><a href="#how_do_i_fork_a_daemon_process">How do I fork a daemon process?</a></li>
62 <li><a href="#how_do_i_find_out_if_i_m_running_interactively_or_not">How do I find out if I'm running interactively or not?</a></li>
63 <li><a href="#how_do_i_timeout_a_slow_event">How do I timeout a slow event?</a></li>
64 <li><a href="#how_do_i_set_cpu_limits">How do I set CPU limits?</a></li>
65 <li><a href="#how_do_i_avoid_zombies_on_a_unix_system">How do I avoid zombies on a Unix system?</a></li>
66 <li><a href="#how_do_i_use_an_sql_database">How do I use an SQL database?</a></li>
67 <li><a href="#how_do_i_make_a_system___exit_on_controlc">How do I make a <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> exit on control-C?</a></li>
68 <li><a href="#how_do_i_open_a_file_without_blocking">How do I open a file without blocking?</a></li>
69 <li><a href="#how_do_i_tell_the_difference_between_errors_from_the_shell_and_perl">How do I tell the difference between errors from the shell and perl?</a></li>
70 <li><a href="#how_do_i_install_a_module_from_cpan">How do I install a module from CPAN?</a></li>
71 <li><a href="#what_s_the_difference_between_require_and_use">What's the difference between require and use?</a></li>
72 <li><a href="#how_do_i_keep_my_own_module_library_directory">How do I keep my own module/library directory?</a></li>
73 <li><a href="#how_do_i_add_the_directory_my_program_lives_in_to_the_module_library_search_path">How do I add the directory my program lives in to the module/library search path?</a></li>
74 <li><a href="#how_do_i_add_a_directory_to_my_include_path___inc__at_runtime">How do I add a directory to my include path (@INC) at runtime?</a></li>
75 <li><a href="#what_is_socket_ph_and_where_do_i_get_it">What is socket.ph and where do I get it?</a></li>
76 </ul>
78 <li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
79 </ul>
80 <!-- INDEX END -->
82 <hr />
83 <p>
84 </p>
85 <h1><a name="name">NAME</a></h1>
86 <p>perlfaq8 - System Interaction ($Revision: 1.27 $, $Date: 2005/12/31 00:54:37 $)</p>
87 <p>
88 </p>
89 <hr />
90 <h1><a name="description">DESCRIPTION</a></h1>
91 <p>This section of the Perl FAQ covers questions involving operating
92 system interaction. Topics include interprocess communication (IPC),
93 control over the user-interface (keyboard, screen and pointing
94 devices), and most anything else not related to data manipulation.</p>
95 <p>Read the FAQs and documentation specific to the port of perl to your
96 operating system (eg, <a href="file://C|\msysgit\mingw\html/pod/perlvms.html">the perlvms manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perlplan9.html">the perlplan9 manpage</a>, ...). These should
97 contain more detailed information on the vagaries of your perl.</p>
98 <p>
99 </p>
100 <h2><a name="how_do_i_find_out_which_operating_system_i_m_running_under">How do I find out which operating system I'm running under?</a></h2>
101 <p>The $^O variable ($OSNAME if you use English) contains an indication of
102 the name of the operating system (not its release number) that your perl
103 binary was built for.</p>
105 </p>
106 <h2><a name="how_come_exec___doesn_t_return">How come <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec()</code></a> doesn't return?</a></h2>
107 <p>Because that's what it does: it replaces your currently running
108 program with a different one. If you want to keep going (as is
109 probably the case if you're asking this question) use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a>
110 instead.</p>
112 </p>
113 <h2><a name="how_do_i_do_fancy_stuff_with_the_keyboard_screen_mouse">How do I do fancy stuff with the keyboard/screen/mouse?</a></h2>
114 <p>How you access/control keyboards, screens, and pointing devices
115 (``mice'') is system-dependent. Try the following modules:</p>
116 <dl>
117 <dt><strong><a name="item_keyboard">Keyboard</a></strong>
119 <dd>
120 <pre>
121 Term::Cap Standard perl distribution
122 Term::ReadKey CPAN
123 Term::ReadLine::Gnu CPAN
124 Term::ReadLine::Perl CPAN
125 Term::Screen CPAN</pre>
126 </dd>
127 <dt><strong><a name="item_screen">Screen</a></strong>
129 <dd>
130 <pre>
131 Term::Cap Standard perl distribution
132 Curses CPAN
133 Term::ANSIColor CPAN</pre>
134 </dd>
135 <dt><strong><a name="item_mouse">Mouse</a></strong>
137 <dd>
138 <pre>
139 Tk CPAN</pre>
140 </dd>
141 </dl>
142 <p>Some of these specific cases are shown as examples in other answers
143 in this section of the perlfaq.</p>
145 </p>
146 <h2><a name="how_do_i_print_something_out_in_color">How do I print something out in color?</a></h2>
147 <p>In general, you don't, because you don't know whether
148 the recipient has a color-aware display device. If you
149 know that they have an ANSI terminal that understands
150 color, you can use the Term::ANSIColor module from CPAN:</p>
151 <pre>
152 use Term::ANSIColor;
153 print color(&quot;red&quot;), &quot;Stop!\n&quot;, color(&quot;reset&quot;);
154 print color(&quot;green&quot;), &quot;Go!\n&quot;, color(&quot;reset&quot;);</pre>
155 <p>Or like this:</p>
156 <pre>
157 use Term::ANSIColor qw(:constants);
158 print RED, &quot;Stop!\n&quot;, RESET;
159 print GREEN, &quot;Go!\n&quot;, RESET;</pre>
161 </p>
162 <h2><a name="how_do_i_read_just_one_key_without_waiting_for_a_return_key">How do I read just one key without waiting for a return key?</a></h2>
163 <p>Controlling input buffering is a remarkably system-dependent matter.
164 On many systems, you can just use the <strong>stty</strong> command as shown in
165 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_getc">getc in the perlfunc manpage</a>, but as you see, that's already getting you into
166 portability snags.</p>
167 <pre>
168 open(TTY, &quot;+&lt;/dev/tty&quot;) or die &quot;no tty: $!&quot;;
169 system &quot;stty cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;;
170 $key = getc(TTY); # perhaps this works
171 # OR ELSE
172 sysread(TTY, $key, 1); # probably this does
173 system &quot;stty -cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;;</pre>
174 <p>The Term::ReadKey module from CPAN offers an easy-to-use interface that
175 should be more efficient than shelling out to <strong>stty</strong> for each key.
176 It even includes limited support for Windows.</p>
177 <pre>
178 use Term::ReadKey;
179 ReadMode('cbreak');
180 $key = ReadKey(0);
181 ReadMode('normal');</pre>
182 <p>However, using the code requires that you have a working C compiler
183 and can use it to build and install a CPAN module. Here's a solution
184 using the standard POSIX module, which is already on your systems
185 (assuming your system supports POSIX).</p>
186 <pre>
187 use HotKey;
188 $key = readkey();</pre>
189 <p>And here's the HotKey module, which hides the somewhat mystifying calls
190 to manipulate the POSIX termios structures.</p>
191 <pre>
192 # HotKey.pm
193 package HotKey;</pre>
194 <pre>
195 @ISA = qw(Exporter);
196 @EXPORT = qw(cbreak cooked readkey);</pre>
197 <pre>
198 use strict;
199 use POSIX qw(:termios_h);
200 my ($term, $oterm, $echo, $noecho, $fd_stdin);</pre>
201 <pre>
202 $fd_stdin = fileno(STDIN);
203 $term = POSIX::Termios-&gt;new();
204 $term-&gt;getattr($fd_stdin);
205 $oterm = $term-&gt;getlflag();</pre>
206 <pre>
207 $echo = ECHO | ECHOK | ICANON;
208 $noecho = $oterm &amp; ~$echo;</pre>
209 <pre>
210 sub cbreak {
211 $term-&gt;setlflag($noecho); # ok, so i don't want echo either
212 $term-&gt;setcc(VTIME, 1);
213 $term-&gt;setattr($fd_stdin, TCSANOW);
214 }</pre>
215 <pre>
216 sub cooked {
217 $term-&gt;setlflag($oterm);
218 $term-&gt;setcc(VTIME, 0);
219 $term-&gt;setattr($fd_stdin, TCSANOW);
220 }</pre>
221 <pre>
222 sub readkey {
223 my $key = '';
224 cbreak();
225 sysread(STDIN, $key, 1);
226 cooked();
227 return $key;
228 }</pre>
229 <pre>
230 END { cooked() }</pre>
231 <pre>
232 1;</pre>
234 </p>
235 <h2><a name="how_do_i_check_whether_input_is_ready_on_the_keyboard">How do I check whether input is ready on the keyboard?</a></h2>
236 <p>The easiest way to do this is to read a key in nonblocking mode with the
237 Term::ReadKey module from CPAN, passing it an argument of -1 to indicate
238 not to block:</p>
239 <pre>
240 use Term::ReadKey;</pre>
241 <pre>
242 ReadMode('cbreak');</pre>
243 <pre>
244 if (defined ($char = ReadKey(-1)) ) {
245 # input was waiting and it was $char
246 } else {
247 # no input was waiting
248 }</pre>
249 <pre>
250 ReadMode('normal'); # restore normal tty settings</pre>
252 </p>
253 <h2><a name="how_do_i_clear_the_screen">How do I clear the screen?</a></h2>
254 <p>If you only have do so infrequently, use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system</code></a>:</p>
255 <pre>
256 system(&quot;clear&quot;);</pre>
257 <p>If you have to do this a lot, save the clear string
258 so you can print it 100 times without calling a program
259 100 times:</p>
260 <pre>
261 $clear_string = `clear`;
262 print $clear_string;</pre>
263 <p>If you're planning on doing other screen manipulations, like cursor
264 positions, etc, you might wish to use Term::Cap module:</p>
265 <pre>
266 use Term::Cap;
267 $terminal = Term::Cap-&gt;Tgetent( {OSPEED =&gt; 9600} );
268 $clear_string = $terminal-&gt;Tputs('cl');</pre>
270 </p>
271 <h2><a name="how_do_i_get_the_screen_size">How do I get the screen size?</a></h2>
272 <p>If you have Term::ReadKey module installed from CPAN,
273 you can use it to fetch the width and height in characters
274 and in pixels:</p>
275 <pre>
276 use Term::ReadKey;
277 ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();</pre>
278 <p>This is more portable than the raw <code>ioctl</code>, but not as
279 illustrative:</p>
280 <pre>
281 require 'sys/ioctl.ph';
282 die &quot;no TIOCGWINSZ &quot; unless defined &amp;TIOCGWINSZ;
283 open(TTY, &quot;+&lt;/dev/tty&quot;) or die &quot;No tty: $!&quot;;
284 unless (ioctl(TTY, &amp;TIOCGWINSZ, $winsize='')) {
285 die sprintf &quot;$0: ioctl TIOCGWINSZ (%08x: $!)\n&quot;, &amp;TIOCGWINSZ;
287 ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize);
288 print &quot;(row,col) = ($row,$col)&quot;;
289 print &quot; (xpixel,ypixel) = ($xpixel,$ypixel)&quot; if $xpixel || $ypixel;
290 print &quot;\n&quot;;</pre>
292 </p>
293 <h2><a name="how_do_i_ask_the_user_for_a_password">How do I ask the user for a password?</a></h2>
294 <p>(This question has nothing to do with the web. See a different
295 FAQ for that.)</p>
296 <p>There's an example of this in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#crypt">crypt in the perlfunc manpage</a>). First, you put the
297 terminal into ``no echo'' mode, then just read the password normally.
298 You may do this with an old-style <code>ioctl()</code> function, POSIX terminal
299 control (see <a href="file://C|\msysgit\mingw\html/lib/POSIX.html">the POSIX manpage</a> or its documentation the Camel Book), or a call
300 to the <strong>stty</strong> program, with varying degrees of portability.</p>
301 <p>You can also do this for most systems using the Term::ReadKey module
302 from CPAN, which is easier to use and in theory more portable.</p>
303 <pre>
304 use Term::ReadKey;</pre>
305 <pre>
306 ReadMode('noecho');
307 $password = ReadLine(0);</pre>
309 </p>
310 <h2><a name="how_do_i_read_and_write_the_serial_port">How do I read and write the serial port?</a></h2>
311 <p>This depends on which operating system your program is running on. In
312 the case of Unix, the serial ports will be accessible through files in
313 /dev; on other systems, device names will doubtless differ.
314 Several problem areas common to all device interaction are the
315 following:</p>
316 <dl>
317 <dt><strong><a name="item_lockfiles">lockfiles</a></strong>
319 <dd>
320 <p>Your system may use lockfiles to control multiple access. Make sure
321 you follow the correct protocol. Unpredictable behavior can result
322 from multiple processes reading from one device.</p>
323 </dd>
324 </li>
325 <dt><strong><a name="item_open_mode">open mode</a></strong>
327 <dd>
328 <p>If you expect to use both read and write operations on the device,
329 you'll have to open it for update (see <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open">open in the perlfunc manpage</a> for
330 details). You may wish to open it without running the risk of
331 blocking by using <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen()</code></a> and <code>O_RDWR|O_NDELAY|O_NOCTTY</code> from the
332 Fcntl module (part of the standard perl distribution). See
333 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen">sysopen in the perlfunc manpage</a> for more on this approach.</p>
334 </dd>
335 </li>
336 <dt><strong><a name="item_end_of_line">end of line</a></strong>
338 <dd>
339 <p>Some devices will be expecting a ``\r'' at the end of each line rather
340 than a ``\n''. In some ports of perl, ``\r'' and ``\n'' are different from
341 their usual (Unix) ASCII values of ``\012'' and ``\015''. You may have to
342 give the numeric values you want directly, using octal (``\015''), hex
343 (``0x0D''), or as a control-character specification (``\cM'').</p>
344 </dd>
345 <dd>
346 <pre>
347 print DEV &quot;atv1\012&quot;; # wrong, for some devices
348 print DEV &quot;atv1\015&quot;; # right, for some devices</pre>
349 </dd>
350 <dd>
351 <p>Even though with normal text files a ``\n'' will do the trick, there is
352 still no unified scheme for terminating a line that is portable
353 between Unix, DOS/Win, and Macintosh, except to terminate <em>ALL</em> line
354 ends with ``\015\012'', and strip what you don't need from the output.
355 This applies especially to socket I/O and autoflushing, discussed
356 next.</p>
357 </dd>
358 </li>
359 <dt><strong><a name="item_flushing_output">flushing output</a></strong>
361 <dd>
362 <p>If you expect characters to get to your device when you <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print()</code></a> them,
363 you'll want to autoflush that filehandle. You can use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select"><code>select()</code></a>
364 and the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$|</code></a> variable to control autoflushing (see <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#_e_verbar_">$&verbar; in the perlvar manpage</a>
365 and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select">select in the perlfunc manpage</a>, or <a href="file://C|\msysgit\mingw\html/pod/perlfaq5.html">the perlfaq5 manpage</a>, ``How do I flush/unbuffer an
366 output filehandle? Why must I do this?''):</p>
367 </dd>
368 <dd>
369 <pre>
370 $oldh = select(DEV);
371 $| = 1;
372 select($oldh);</pre>
373 </dd>
374 <dd>
375 <p>You'll also see code that does this without a temporary variable, as in</p>
376 </dd>
377 <dd>
378 <pre>
379 select((select(DEV), $| = 1)[0]);</pre>
380 </dd>
381 <dd>
382 <p>Or if you don't mind pulling in a few thousand lines
383 of code just because you're afraid of a little $| variable:</p>
384 </dd>
385 <dd>
386 <pre>
387 use IO::Handle;
388 DEV-&gt;autoflush(1);</pre>
389 </dd>
390 <dd>
391 <p>As mentioned in the previous item, this still doesn't work when using
392 socket I/O between Unix and Macintosh. You'll need to hard code your
393 line terminators, in that case.</p>
394 </dd>
395 </li>
396 <dt><strong><a name="item_non_2dblocking_input">non-blocking input</a></strong>
398 <dd>
399 <p>If you are doing a blocking <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_read"><code>read()</code></a> or sysread(), you'll have to
400 arrange for an alarm handler to provide a timeout (see
401 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_alarm">alarm in the perlfunc manpage</a>). If you have a non-blocking open, you'll likely
402 have a non-blocking read, which means you may have to use a 4-arg
403 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select"><code>select()</code></a> to determine whether I/O is ready on that device (see
404 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select">select in the perlfunc manpage</a>.</p>
405 </dd>
406 </li>
407 </dl>
408 <p>While trying to read from his caller-id box, the notorious Jamie Zawinski
409 &lt;<a href="mailto:jwz@netscape.com">jwz@netscape.com</a>&gt;, after much gnashing of teeth and fighting with sysread,
410 sysopen, POSIX's tcgetattr business, and various other functions that
411 go bump in the night, finally came up with this:</p>
412 <pre>
413 sub open_modem {
414 use IPC::Open2;
415 my $stty = `/bin/stty -g`;
416 open2( \*MODEM_IN, \*MODEM_OUT, &quot;cu -l$modem_device -s2400 2&gt;&amp;1&quot;);
417 # starting cu hoses /dev/tty's stty settings, even when it has
418 # been opened on a pipe...
419 system(&quot;/bin/stty $stty&quot;);
420 $_ = &lt;MODEM_IN&gt;;
421 chomp;
422 if ( !m/^Connected/ ) {
423 print STDERR &quot;$0: cu printed `$_' instead of `Connected'\n&quot;;
425 }</pre>
427 </p>
428 <h2><a name="how_do_i_decode_encrypted_password_files">How do I decode encrypted password files?</a></h2>
429 <p>You spend lots and lots of money on dedicated hardware, but this is
430 bound to get you talked about.</p>
431 <p>Seriously, you can't if they are Unix password files--the Unix
432 password system employs one-way encryption. It's more like hashing than
433 encryption. The best you can check is whether something else hashes to
434 the same string. You can't turn a hash back into the original string.
435 Programs like Crack
436 can forcibly (and intelligently) try to guess passwords, but don't
437 (can't) guarantee quick success.</p>
438 <p>If you're worried about users selecting bad passwords, you should
439 proactively check when they try to change their password (by modifying
440 passwd(1), for example).</p>
442 </p>
443 <h2><a name="how_do_i_start_a_process_in_the_background">How do I start a process in the background?</a></h2>
444 <p>Several modules can start other processes that do not block
445 your Perl program. You can use IPC::Open3, Parallel::Jobs,
446 IPC::Run, and some of the POE modules. See CPAN for more
447 details.</p>
448 <p>You could also use</p>
449 <pre>
450 system(&quot;cmd &amp;&quot;)</pre>
451 <p>or you could use fork as documented in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#fork">fork in the perlfunc manpage</a>, with
452 further examples in <a href="file://C|\msysgit\mingw\html/pod/perlipc.html">the perlipc manpage</a>. Some things to be aware of, if you're
453 on a Unix-like system:</p>
454 <dl>
455 <dt><strong><a name="item_stdin_2c_stdout_2c_and_stderr_are_shared">STDIN, STDOUT, and STDERR are shared</a></strong>
457 <dd>
458 <p>Both the main process and the backgrounded one (the ``child'' process)
459 share the same STDIN, STDOUT and STDERR filehandles. If both try to
460 access them at once, strange things can happen. You may want to close
461 or reopen these for the child. You can get around this with
462 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a>ing a pipe (see <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open">open in the perlfunc manpage</a>) but on some systems this
463 means that the child process cannot outlive the parent.</p>
464 </dd>
465 </li>
466 <dt><strong><a name="item_signals">Signals</a></strong>
468 <dd>
469 <p>You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too.
470 SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is
471 sent when you write to a filehandle whose child process has closed (an
472 untrapped SIGPIPE can cause your program to silently die). This is
473 not an issue with <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system(&quot;cmd&amp;&quot;)</code></a>.</p>
474 </dd>
475 </li>
476 <dt><strong><a name="item_zombies">Zombies</a></strong>
478 <dd>
479 <p>You have to be prepared to ``reap'' the child process when it finishes.</p>
480 </dd>
481 <dd>
482 <pre>
483 $SIG{CHLD} = sub { wait };</pre>
484 </dd>
485 <dd>
486 <pre>
487 $SIG{CHLD} = 'IGNORE';</pre>
488 </dd>
489 <dd>
490 <p>You can also use a double fork. You immediately <code>wait()</code> for your
491 first child, and the init daemon will <code>wait()</code> for your grandchild once
492 it exits.</p>
493 </dd>
494 <dd>
495 <pre>
496 unless ($pid = fork) {
497 unless (fork) {
498 exec &quot;what you really wanna do&quot;;
499 die &quot;exec failed!&quot;;
501 exit 0;
503 waitpid($pid,0);</pre>
504 </dd>
505 <dd>
506 <p>See <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#signals">Signals in the perlipc manpage</a> for other examples of code to do this.
507 Zombies are not an issue with <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system(&quot;prog &amp;&quot;)</code></a>.</p>
508 </dd>
509 </li>
510 </dl>
512 </p>
513 <h2><a name="how_do_i_trap_control_characters_signals">How do I trap control characters/signals?</a></h2>
514 <p>You don't actually ``trap'' a control character. Instead, that character
515 generates a signal which is sent to your terminal's currently
516 foregrounded process group, which you then trap in your process.
517 Signals are documented in <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#signals">Signals in the perlipc manpage</a> and the
518 section on ``Signals'' in the Camel.</p>
519 <p>You can set the values of the %SIG hash to be the functions you want
520 to handle the signal. After perl catches the signal, it looks in %SIG
521 for a key with the same name as the signal, then calls the subroutine
522 value for that key.</p>
523 <pre>
524 # as an anonymous subroutine</pre>
525 <pre>
526 $SIG{INT} = sub { syswrite(STDERR, &quot;ouch\n&quot;, 5 ) };</pre>
527 <pre>
528 # or a reference to a function</pre>
529 <pre>
530 $SIG{INT} = \&amp;ouch;</pre>
531 <pre>
532 # or the name of the function as a string</pre>
533 <pre>
534 $SIG{INT} = &quot;ouch&quot;;</pre>
535 <p>Perl versions before 5.8 had in its C source code signal handlers which
536 would catch the signal and possibly run a Perl function that you had set
537 in %SIG. This violated the rules of signal handling at that level
538 causing perl to dump core. Since version 5.8.0, perl looks at %SIG
539 *after* the signal has been caught, rather than while it is being caught.
540 Previous versions of this answer were incorrect.</p>
542 </p>
543 <h2><a name="how_do_i_modify_the_shadow_password_file_on_a_unix_system">How do I modify the shadow password file on a Unix system?</a></h2>
544 <p>If perl was installed correctly and your shadow library was written
545 properly, the getpw*() functions described in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html">the perlfunc manpage</a> should in
546 theory provide (read-only) access to entries in the shadow password
547 file. To change the file, make a new shadow password file (the format
548 varies from system to system--see <em>passwd</em> for specifics) and use
549 <code>pwd_mkdb(8)</code> to install it (see <em>pwd_mkdb</em> for more details).</p>
551 </p>
552 <h2><a name="how_do_i_set_the_time_and_date">How do I set the time and date?</a></h2>
553 <p>Assuming you're running under sufficient permissions, you should be
554 able to set the system-wide date and time by running the <code>date(1)</code>
555 program. (There is no way to set the time and date on a per-process
556 basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT;
557 the VMS equivalent is <code>set time</code>.</p>
558 <p>However, if all you want to do is change your time zone, you can
559 probably get away with setting an environment variable:</p>
560 <pre>
561 $ENV{TZ} = &quot;MST7MDT&quot;; # unixish
562 $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}=&quot;-5&quot; # vms
563 system &quot;trn comp.lang.perl.misc&quot;;</pre>
565 </p>
566 <h2><a name="how_can_i_sleep___or_alarm___for_under_a_second">How can I <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sleep"><code>sleep()</code></a> or <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_alarm"><code>alarm()</code></a> for under a second?</a></h2>
567 <p>If you want finer granularity than the 1 second that the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sleep"><code>sleep()</code></a>
568 function provides, the easiest way is to use the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select"><code>select()</code></a> function as
569 documented in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_select">select in the perlfunc manpage</a>. Try the Time::HiRes and
570 the BSD::Itimer modules (available from CPAN, and starting from
571 Perl 5.8 Time::HiRes is part of the standard distribution).</p>
573 </p>
574 <h2><a name="how_can_i_measure_time_under_a_second">How can I measure time under a second?</a></h2>
575 <p>In general, you may not be able to. The Time::HiRes module (available
576 from CPAN, and starting from Perl 5.8 part of the standard distribution)
577 provides this functionality for some systems.</p>
578 <p>If your system supports both the <code>syscall()</code> function in Perl as well as
579 a system call like gettimeofday(2), then you may be able to do
580 something like this:</p>
581 <pre>
582 require 'sys/syscall.ph';</pre>
583 <pre>
584 $TIMEVAL_T = &quot;LL&quot;;</pre>
585 <pre>
586 $done = $start = pack($TIMEVAL_T, ());</pre>
587 <pre>
588 syscall(&amp;SYS_gettimeofday, $start, 0) != -1
589 or die &quot;gettimeofday: $!&quot;;</pre>
590 <pre>
591 ##########################
592 # DO YOUR OPERATION HERE #
593 ##########################</pre>
594 <pre>
595 syscall( &amp;SYS_gettimeofday, $done, 0) != -1
596 or die &quot;gettimeofday: $!&quot;;</pre>
597 <pre>
598 @start = unpack($TIMEVAL_T, $start);
599 @done = unpack($TIMEVAL_T, $done);</pre>
600 <pre>
601 # fix microseconds
602 for ($done[1], $start[1]) { $_ /= 1_000_000 }</pre>
603 <pre>
604 $delta_time = sprintf &quot;%.4f&quot;, ($done[0] + $done[1] )
606 ($start[0] + $start[1] );</pre>
608 </p>
609 <h2><a name="how_can_i_do_an_atexit___or_setjmp___longjmp____exception_handling_">How can I do an <code>atexit()</code> or setjmp()/longjmp()? (Exception handling)</a></h2>
610 <p>Release 5 of Perl added the END block, which can be used to simulate
611 atexit(). Each package's END block is called when the program or
612 thread ends (see <a href="file://C|\msysgit\mingw\html/pod/perlmod.html">the perlmod manpage</a> manpage for more details).</p>
613 <p>For example, you can use this to make sure your filter program
614 managed to finish its output without filling up the disk:</p>
615 <pre>
616 END {
617 close(STDOUT) || die &quot;stdout close failed: $!&quot;;
618 }</pre>
619 <p>The END block isn't called when untrapped signals kill the program,
620 though, so if you use END blocks you should also use</p>
621 <pre>
622 use sigtrap qw(die normal-signals);</pre>
623 <p>Perl's exception-handling mechanism is its <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a> operator. You can
624 use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a> as setjmp and <code>die()</code> as longjmp. For details of this, see
625 the section on signals, especially the time-out handler for a blocking
626 <code>flock()</code> in <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#signals">Signals in the perlipc manpage</a> or the section on ``Signals'' in
627 the Camel Book.</p>
628 <p>If exception handling is all you're interested in, try the
629 exceptions.pl library (part of the standard perl distribution).</p>
630 <p>If you want the <code>atexit()</code> syntax (and an <code>rmexit()</code> as well), try the
631 AtExit module available from CPAN.</p>
633 </p>
634 <h2><a name="why_doesn_t_my_sockets_program_work_under_system_v__solaris__what_does_the_error_message_protocol_not_supported_mean">Why doesn't my sockets program work under System V (Solaris)? What does the error message ``Protocol not supported'' mean?</a></h2>
635 <p>Some Sys-V based systems, notably Solaris 2.X, redefined some of the
636 standard socket constants. Since these were constant across all
637 architectures, they were often hardwired into perl code. The proper
638 way to deal with this is to ``use Socket'' to get the correct values.</p>
639 <p>Note that even though SunOS and Solaris are binary compatible, these
640 values are different. Go figure.</p>
642 </p>
643 <h2><a name="how_can_i_call_my_system_s_unique_c_functions_from_perl">How can I call my system's unique C functions from Perl?</a></h2>
644 <p>In most cases, you write an external module to do it--see the answer
645 to ``Where can I learn about linking C with Perl? [h2xs, xsubpp]''.
646 However, if the function is a system call, and your system supports
647 syscall(), you can use the syscall function (documented in
648 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html">the perlfunc manpage</a>).</p>
649 <p>Remember to check the modules that came with your distribution, and
650 CPAN as well---someone may already have written a module to do it. On
651 Windows, try Win32::API. On Macs, try Mac::Carbon. If no module
652 has an interface to the C function, you can inline a bit of C in your
653 Perl source with Inline::C.</p>
655 </p>
656 <h2><a name="where_do_i_get_the_include_files_to_do_ioctl___or_syscall__">Where do I get the include files to do <code>ioctl()</code> or syscall()?</a></h2>
657 <p>Historically, these would be generated by the h2ph tool, part of the
658 standard perl distribution. This program converts <code>cpp(1)</code> directives
659 in C header files to files containing subroutine definitions, like
660 &amp;SYS_getitimer, which you can use as arguments to your functions.
661 It doesn't work perfectly, but it usually gets most of the job done.
662 Simple files like <em>errno.h</em>, <em>syscall.h</em>, and <em>socket.h</em> were fine,
663 but the hard ones like <em>ioctl.h</em> nearly always need to hand-edited.
664 Here's how to install the *.ph files:</p>
665 <pre>
666 1. become super-user
667 2. cd /usr/include
668 3. h2ph *.h */*.h</pre>
669 <p>If your system supports dynamic loading, for reasons of portability and
670 sanity you probably ought to use h2xs (also part of the standard perl
671 distribution). This tool converts C header files to Perl extensions.
672 See <a href="file://C|\msysgit\mingw\html/pod/perlxstut.html">the perlxstut manpage</a> for how to get started with h2xs.</p>
673 <p>If your system doesn't support dynamic loading, you still probably
674 ought to use h2xs. See <a href="file://C|\msysgit\mingw\html/pod/perlxstut.html">the perlxstut manpage</a> and <a href="file://C|\msysgit\mingw\html/lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</a> for
675 more information (in brief, just use <strong>make perl</strong> instead of a plain
676 <strong>make</strong> to rebuild perl with a new static extension).</p>
678 </p>
679 <h2><a name="why_do_setuid_perl_scripts_complain_about_kernel_problems">Why do setuid perl scripts complain about kernel problems?</a></h2>
680 <p>Some operating systems have bugs in the kernel that make setuid
681 scripts inherently insecure. Perl gives you a number of options
682 (described in <a href="file://C|\msysgit\mingw\html/pod/perlsec.html">the perlsec manpage</a>) to work around such systems.</p>
684 </p>
685 <h2><a name="how_can_i_open_a_pipe_both_to_and_from_a_command">How can I open a pipe both to and from a command?</a></h2>
686 <p>The IPC::Open2 module (part of the standard perl distribution) is an
687 easy-to-use approach that internally uses pipe(), fork(), and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec()</code></a> to do
688 the job. Make sure you read the deadlock warnings in its documentation,
689 though (see <a href="file://C|\msysgit\mingw\html/lib/IPC/Open2.html">the IPC::Open2 manpage</a>). See
690 <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#bidirectional_communication_with_another_process">Bidirectional Communication with Another Process in the perlipc manpage</a> and
691 <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#bidirectional_communication_with_yourself">Bidirectional Communication with Yourself in the perlipc manpage</a></p>
692 <p>You may also use the IPC::Open3 module (part of the standard perl
693 distribution), but be warned that it has a different order of
694 arguments from IPC::Open2 (see <a href="file://C|\msysgit\mingw\html/lib/IPC/Open3.html">the IPC::Open3 manpage</a>).</p>
696 </p>
697 <h2><a name="why_can_t_i_get_the_output_of_a_command_with_system__">Why can't I get the output of a command with system()?</a></h2>
698 <p>You're confusing the purpose of <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> and backticks (``). <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a>
699 runs a command and returns exit status information (as a 16 bit value:
700 the low 7 bits are the signal the process died from, if any, and
701 the high 8 bits are the actual exit value). Backticks (``) run a
702 command and return what it sent to STDOUT.</p>
703 <pre>
704 $exit_status = system(&quot;mail-users&quot;);
705 $output_string = `ls`;</pre>
707 </p>
708 <h2><a name="how_can_i_capture_stderr_from_an_external_command">How can I capture STDERR from an external command?</a></h2>
709 <p>There are three basic ways of running external commands:</p>
710 <pre>
711 system $cmd; # using system()
712 $output = `$cmd`; # using backticks (``)
713 open (PIPE, &quot;cmd |&quot;); # using open()</pre>
714 <p>With system(), both STDOUT and STDERR will go the same place as the
715 script's STDOUT and STDERR, unless the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> command redirects them.
716 Backticks and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> read <strong>only</strong> the STDOUT of your command.</p>
717 <p>You can also use the <code>open3()</code> function from IPC::Open3. Benjamin
718 Goldberg provides some sample code:</p>
719 <p>To capture a program's STDOUT, but discard its STDERR:</p>
720 <pre>
721 use IPC::Open3;
722 use File::Spec;
723 use Symbol qw(gensym);
724 open(NULL, &quot;&gt;&quot;, File::Spec-&gt;devnull);
725 my $pid = open3(gensym, \*PH, &quot;&gt;&amp;NULL&quot;, &quot;cmd&quot;);
726 while( &lt;PH&gt; ) { }
727 waitpid($pid, 0);</pre>
728 <p>To capture a program's STDERR, but discard its STDOUT:</p>
729 <pre>
730 use IPC::Open3;
731 use File::Spec;
732 use Symbol qw(gensym);
733 open(NULL, &quot;&gt;&quot;, File::Spec-&gt;devnull);
734 my $pid = open3(gensym, &quot;&gt;&amp;NULL&quot;, \*PH, &quot;cmd&quot;);
735 while( &lt;PH&gt; ) { }
736 waitpid($pid, 0);</pre>
737 <p>To capture a program's STDERR, and let its STDOUT go to our own STDERR:</p>
738 <pre>
739 use IPC::Open3;
740 use Symbol qw(gensym);
741 my $pid = open3(gensym, &quot;&gt;&amp;STDERR&quot;, \*PH, &quot;cmd&quot;);
742 while( &lt;PH&gt; ) { }
743 waitpid($pid, 0);</pre>
744 <p>To read both a command's STDOUT and its STDERR separately, you can
745 redirect them to temp files, let the command run, then read the temp
746 files:</p>
747 <pre>
748 use IPC::Open3;
749 use Symbol qw(gensym);
750 use IO::File;
751 local *CATCHOUT = IO::File-&gt;new_tmpfile;
752 local *CATCHERR = IO::File-&gt;new_tmpfile;
753 my $pid = open3(gensym, &quot;&gt;&amp;CATCHOUT&quot;, &quot;&gt;&amp;CATCHERR&quot;, &quot;cmd&quot;);
754 waitpid($pid, 0);
755 seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
756 while( &lt;CATCHOUT&gt; ) {}
757 while( &lt;CATCHERR&gt; ) {}</pre>
758 <p>But there's no real need for *both* to be tempfiles... the following
759 should work just as well, without deadlocking:</p>
760 <pre>
761 use IPC::Open3;
762 use Symbol qw(gensym);
763 use IO::File;
764 local *CATCHERR = IO::File-&gt;new_tmpfile;
765 my $pid = open3(gensym, \*CATCHOUT, &quot;&gt;&amp;CATCHERR&quot;, &quot;cmd&quot;);
766 while( &lt;CATCHOUT&gt; ) {}
767 waitpid($pid, 0);
768 seek CATCHERR, 0, 0;
769 while( &lt;CATCHERR&gt; ) {}</pre>
770 <p>And it'll be faster, too, since we can begin processing the program's
771 stdout immediately, rather than waiting for the program to finish.</p>
772 <p>With any of these, you can change file descriptors before the call:</p>
773 <pre>
774 open(STDOUT, &quot;&gt;logfile&quot;);
775 system(&quot;ls&quot;);</pre>
776 <p>or you can use Bourne shell file-descriptor redirection:</p>
777 <pre>
778 $output = `$cmd 2&gt;some_file`;
779 open (PIPE, &quot;cmd 2&gt;some_file |&quot;);</pre>
780 <p>You can also use file-descriptor redirection to make STDERR a
781 duplicate of STDOUT:</p>
782 <pre>
783 $output = `$cmd 2&gt;&amp;1`;
784 open (PIPE, &quot;cmd 2&gt;&amp;1 |&quot;);</pre>
785 <p>Note that you <em>cannot</em> simply open STDERR to be a dup of STDOUT
786 in your Perl program and avoid calling the shell to do the redirection.
787 This doesn't work:</p>
788 <pre>
789 open(STDERR, &quot;&gt;&amp;STDOUT&quot;);
790 $alloutput = `cmd args`; # stderr still escapes</pre>
791 <p>This fails because the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> makes STDERR go to where STDOUT was
792 going at the time of the open(). The backticks then make STDOUT go to
793 a string, but don't change STDERR (which still goes to the old
794 STDOUT).</p>
795 <p>Note that you <em>must</em> use Bourne shell (sh(1)) redirection syntax in
796 backticks, not csh(1)! Details on why Perl's <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> and backtick
797 and pipe opens all use the Bourne shell are in the
798 <em>versus/csh.whynot</em> article in the ``Far More Than You Ever Wanted To
799 Know'' collection in <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a> . To
800 capture a command's STDERR and STDOUT together:</p>
801 <pre>
802 $output = `cmd 2&gt;&amp;1`; # either with backticks
803 $pid = open(PH, &quot;cmd 2&gt;&amp;1 |&quot;); # or with an open pipe
804 while (&lt;PH&gt;) { } # plus a read</pre>
805 <p>To capture a command's STDOUT but discard its STDERR:</p>
806 <pre>
807 $output = `cmd 2&gt;/dev/null`; # either with backticks
808 $pid = open(PH, &quot;cmd 2&gt;/dev/null |&quot;); # or with an open pipe
809 while (&lt;PH&gt;) { } # plus a read</pre>
810 <p>To capture a command's STDERR but discard its STDOUT:</p>
811 <pre>
812 $output = `cmd 2&gt;&amp;1 1&gt;/dev/null`; # either with backticks
813 $pid = open(PH, &quot;cmd 2&gt;&amp;1 1&gt;/dev/null |&quot;); # or with an open pipe
814 while (&lt;PH&gt;) { } # plus a read</pre>
815 <p>To exchange a command's STDOUT and STDERR in order to capture the STDERR
816 but leave its STDOUT to come out our old STDERR:</p>
817 <pre>
818 $output = `cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-`; # either with backticks
819 $pid = open(PH, &quot;cmd 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;-|&quot;);# or with an open pipe
820 while (&lt;PH&gt;) { } # plus a read</pre>
821 <p>To read both a command's STDOUT and its STDERR separately, it's easiest
822 to redirect them separately to files, and then read from those files
823 when the program is done:</p>
824 <pre>
825 system(&quot;program args 1&gt;program.stdout 2&gt;program.stderr&quot;);</pre>
826 <p>Ordering is important in all these examples. That's because the shell
827 processes file descriptor redirections in strictly left to right order.</p>
828 <pre>
829 system(&quot;prog args 1&gt;tmpfile 2&gt;&amp;1&quot;);
830 system(&quot;prog args 2&gt;&amp;1 1&gt;tmpfile&quot;);</pre>
831 <p>The first command sends both standard out and standard error to the
832 temporary file. The second command sends only the old standard output
833 there, and the old standard error shows up on the old standard out.</p>
835 </p>
836 <h2><a name="why_doesn_t_open___return_an_error_when_a_pipe_open_fails">Why doesn't <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> return an error when a pipe open fails?</a></h2>
837 <p>If the second argument to a piped <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> contains shell
838 metacharacters, perl fork()s, then exec()s a shell to decode the
839 metacharacters and eventually run the desired program. If the program
840 couldn't be run, it's the shell that gets the message, not Perl. All
841 your Perl program can find out is whether the shell itself could be
842 successfully started. You can still capture the shell's STDERR and
843 check it for error messages. See <a href="#how_can_i_capture_stderr_from_an_external_command">How can I capture STDERR from an external command?</a> elsewhere in this document, or use the
844 IPC::Open3 module.</p>
845 <p>If there are no shell metacharacters in the argument of open(), Perl
846 runs the command directly, without using the shell, and can correctly
847 report whether the command started.</p>
849 </p>
850 <h2><a name="what_s_wrong_with_using_backticks_in_a_void_context">What's wrong with using backticks in a void context?</a></h2>
851 <p>Strictly speaking, nothing. Stylistically speaking, it's not a good
852 way to write maintainable code. Perl has several operators for
853 running external commands. Backticks are one; they collect the output
854 from the command for use in your program. The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system</code></a> function is
855 another; it doesn't do this.</p>
856 <p>Writing backticks in your program sends a clear message to the readers
857 of your code that you wanted to collect the output of the command.
858 Why send a clear message that isn't true?</p>
859 <p>Consider this line:</p>
860 <pre>
861 `cat /etc/termcap`;</pre>
862 <p>You forgot to check <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__"><code>$?</code></a> to see whether the program even ran
863 correctly. Even if you wrote</p>
864 <pre>
865 print `cat /etc/termcap`;</pre>
866 <p>this code could and probably should be written as</p>
867 <pre>
868 system(&quot;cat /etc/termcap&quot;) == 0
869 or die &quot;cat program failed!&quot;;</pre>
870 <p>which will get the output quickly (as it is generated, instead of only
871 at the end) and also check the return value.</p>
872 <p><a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> also provides direct control over whether shell wildcard
873 processing may take place, whereas backticks do not.</p>
875 </p>
876 <h2><a name="how_can_i_call_backticks_without_shell_processing">How can I call backticks without shell processing?</a></h2>
877 <p>This is a bit tricky. You can't simply write the command
878 like this:</p>
879 <pre>
880 @ok = `grep @opts '$search_string' @filenames`;</pre>
881 <p>As of Perl 5.8.0, you can use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> with multiple arguments.
882 Just like the list forms of <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> and exec(), no shell
883 escapes happen.</p>
884 <pre>
885 open( GREP, &quot;-|&quot;, 'grep', @opts, $search_string, @filenames );
886 chomp(@ok = &lt;GREP&gt;);
887 close GREP;</pre>
888 <p>You can also:</p>
889 <pre>
890 my @ok = ();
891 if (open(GREP, &quot;-|&quot;)) {
892 while (&lt;GREP&gt;) {
893 chomp;
894 push(@ok, $_);
896 close GREP;
897 } else {
898 exec 'grep', @opts, $search_string, @filenames;
899 }</pre>
900 <p>Just as with system(), no shell escapes happen when you <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec()</code></a> a list.
901 Further examples of this can be found in <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#safe_pipe_opens">Safe Pipe Opens in the perlipc manpage</a>.</p>
902 <p>Note that if you're use Microsoft, no solution to this vexing issue
903 is even possible. Even if Perl were to emulate fork(), you'd still
904 be stuck, because Microsoft does not have a argc/argv-style API.</p>
906 </p>
907 <h2><a name="why_can_t_my_script_read_from_stdin_after_i_gave_it_eof___d_on_unix___z_on_msdos_">Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?</a></h2>
908 <p>Some stdio's set error and eof flags that need clearing. The
909 POSIX module defines <code>clearerr()</code> that you can use. That is the
910 technically correct way to do it. Here are some less reliable
911 workarounds:</p>
912 <ol>
913 <li>
914 <p>Try keeping around the seekpointer and go there, like this:</p>
915 <pre>
916 $where = tell(LOG);
917 seek(LOG, $where, 0);</pre>
918 </li>
919 <li>
920 <p>If that doesn't work, try seeking to a different part of the file and
921 then back.</p>
922 </li>
923 <li>
924 <p>If that doesn't work, try seeking to a different part of
925 the file, reading something, and then seeking back.</p>
926 </li>
927 <li>
928 <p>If that doesn't work, give up on your stdio package and use sysread.</p>
929 </li>
930 </ol>
932 </p>
933 <h2><a name="how_can_i_convert_my_shell_script_to_perl">How can I convert my shell script to perl?</a></h2>
934 <p>Learn Perl and rewrite it. Seriously, there's no simple converter.
935 Things that are awkward to do in the shell are easy to do in Perl, and
936 this very awkwardness is what would make a shell-&gt;perl converter
937 nigh-on impossible to write. By rewriting it, you'll think about what
938 you're really trying to do, and hopefully will escape the shell's
939 pipeline datastream paradigm, which while convenient for some matters,
940 causes many inefficiencies.</p>
942 </p>
943 <h2><a name="can_i_use_perl_to_run_a_telnet_or_ftp_session">Can I use perl to run a telnet or ftp session?</a></h2>
944 <p>Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from
945 CPAN). <a href="http://www.cpan.org/scripts/netstuff/telnet.emul.shar">http://www.cpan.org/scripts/netstuff/telnet.emul.shar</a>
946 will also help for emulating the telnet protocol, but Net::Telnet is
947 quite probably easier to use..</p>
948 <p>If all you want to do is pretend to be telnet but don't need
949 the initial telnet handshaking, then the standard dual-process
950 approach will suffice:</p>
951 <pre>
952 use IO::Socket; # new in 5.004
953 $handle = IO::Socket::INET-&gt;new('www.perl.com:80')
954 || die &quot;can't connect to port 80 on www.perl.com: $!&quot;;
955 $handle-&gt;autoflush(1);
956 if (fork()) { # XXX: undef means failure
957 select($handle);
958 print while &lt;STDIN&gt;; # everything from stdin to socket
959 } else {
960 print while &lt;$handle&gt;; # everything from socket to stdout
962 close $handle;
963 exit;</pre>
965 </p>
966 <h2><a name="how_can_i_write_expect_in_perl">How can I write expect in Perl?</a></h2>
967 <p>Once upon a time, there was a library called chat2.pl (part of the
968 standard perl distribution), which never really got finished. If you
969 find it somewhere, <em>don't use it</em>. These days, your best bet is to
970 look at the Expect module available from CPAN, which also requires two
971 other modules from CPAN, IO::Pty and IO::Stty.</p>
973 </p>
974 <h2><a name="is_there_a_way_to_hide_perl_s_command_line_from_programs_such_as_ps">Is there a way to hide perl's command line from programs such as ``ps''?</a></h2>
975 <p>First of all note that if you're doing this for security reasons (to
976 avoid people seeing passwords, for example) then you should rewrite
977 your program so that critical information is never given as an
978 argument. Hiding the arguments won't make your program completely
979 secure.</p>
980 <p>To actually alter the visible command line, you can assign to the
981 variable $0 as documented in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html">the perlvar manpage</a>. This won't work on all
982 operating systems, though. Daemon programs like sendmail place their
983 state there, as in:</p>
984 <pre>
985 $0 = &quot;orcus [accepting connections]&quot;;</pre>
987 </p>
988 <h2><a name="i__changed_directory__modified_my_environment__in_a_perl_script__how_come_the_change_disappeared_when_i_exited_the_script_how_do_i_get_my_changes_to_be_visible">I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?</a></h2>
989 <dl>
990 <dt><strong><a name="item_unix">Unix</a></strong>
992 <dd>
993 <p>In the strictest sense, it can't be done--the script executes as a
994 different process from the shell it was started from. Changes to a
995 process are not reflected in its parent--only in any children
996 created after the change. There is shell magic that may allow you to
997 fake it by eval()ing the script's output in your shell; check out the
998 comp.unix.questions FAQ for details.</p>
999 </dd>
1000 </li>
1001 </dl>
1003 </p>
1004 <h2><a name="how_do_i_close_a_process_s_filehandle_without_waiting_for_it_to_complete">How do I close a process's filehandle without waiting for it to complete?</a></h2>
1005 <p>Assuming your system supports such things, just send an appropriate signal
1006 to the process (see <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#kill">kill in the perlfunc manpage</a>). It's common to first send a TERM
1007 signal, wait a little bit, and then send a KILL signal to finish it off.</p>
1009 </p>
1010 <h2><a name="how_do_i_fork_a_daemon_process">How do I fork a daemon process?</a></h2>
1011 <p>If by daemon process you mean one that's detached (disassociated from
1012 its tty), then the following process is reported to work on most
1013 Unixish systems. Non-Unix users should check their Your_OS::Process
1014 module for other solutions.</p>
1015 <ul>
1016 <li>
1017 <p>Open /dev/tty and use the TIOCNOTTY ioctl on it. See <em>tty</em>
1018 for details. Or better yet, you can just use the POSIX::setsid()
1019 function, so you don't have to worry about process groups.</p>
1020 </li>
1021 <li>
1022 <p>Change directory to /</p>
1023 </li>
1024 <li>
1025 <p>Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
1026 tty.</p>
1027 </li>
1028 <li>
1029 <p>Background yourself like this:</p>
1030 <pre>
1031 fork &amp;&amp; exit;</pre>
1032 </li>
1033 </ul>
1034 <p>The Proc::Daemon module, available from CPAN, provides a function to
1035 perform these actions for you.</p>
1037 </p>
1038 <h2><a name="how_do_i_find_out_if_i_m_running_interactively_or_not">How do I find out if I'm running interactively or not?</a></h2>
1039 <p>Good question. Sometimes <code>-t STDIN</code> and <code>-t STDOUT</code> can give clues,
1040 sometimes not.</p>
1041 <pre>
1042 if (-t STDIN &amp;&amp; -t STDOUT) {
1043 print &quot;Now what? &quot;;
1044 }</pre>
1045 <p>On POSIX systems, you can test whether your own process group matches
1046 the current process group of your controlling terminal as follows:</p>
1047 <pre>
1048 use POSIX qw/getpgrp tcgetpgrp/;
1049 open(TTY, &quot;/dev/tty&quot;) or die $!;
1050 $tpgrp = tcgetpgrp(fileno(*TTY));
1051 $pgrp = getpgrp();
1052 if ($tpgrp == $pgrp) {
1053 print &quot;foreground\n&quot;;
1054 } else {
1055 print &quot;background\n&quot;;
1056 }</pre>
1058 </p>
1059 <h2><a name="how_do_i_timeout_a_slow_event">How do I timeout a slow event?</a></h2>
1060 <p>Use the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_alarm"><code>alarm()</code></a> function, probably in conjunction with a signal
1061 handler, as documented in <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#signals">Signals in the perlipc manpage</a> and the section on
1062 ``Signals'' in the Camel. You may instead use the more flexible
1063 Sys::AlarmCall module available from CPAN.</p>
1064 <p>The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_alarm"><code>alarm()</code></a> function is not implemented on all versions of Windows.
1065 Check the documentation for your specific version of Perl.</p>
1067 </p>
1068 <h2><a name="how_do_i_set_cpu_limits">How do I set CPU limits?</a></h2>
1069 <p>Use the BSD::Resource module from CPAN.</p>
1071 </p>
1072 <h2><a name="how_do_i_avoid_zombies_on_a_unix_system">How do I avoid zombies on a Unix system?</a></h2>
1073 <p>Use the reaper code from <a href="file://C|\msysgit\mingw\html/pod/perlipc.html#signals">Signals in the perlipc manpage</a> to call <code>wait()</code> when a
1074 SIGCHLD is received, or else use the double-fork technique described
1075 in <a href="file://C|\msysgit\mingw\html/pod/perlfaq8.html#how_do_i_start_a_process_in_the_background">How do I start a process in the background? in the perlfaq8 manpage</a>.</p>
1077 </p>
1078 <h2><a name="how_do_i_use_an_sql_database">How do I use an SQL database?</a></h2>
1079 <p>The DBI module provides an abstract interface to most database
1080 servers and types, including Oracle, DB2, Sybase, mysql, Postgresql,
1081 ODBC, and flat files. The DBI module accesses each database type
1082 through a database driver, or DBD. You can see a complete list of
1083 available drivers on CPAN: <a href="http://www.cpan.org/modules/by-module/DBD/">http://www.cpan.org/modules/by-module/DBD/</a> .
1084 You can read more about DBI on <a href="http://dbi.perl.org">http://dbi.perl.org</a> .</p>
1085 <p>Other modules provide more specific access: Win32::ODBC, Alzabo, iodbc,
1086 and others found on CPAN Search: <a href="http://search.cpan.org">http://search.cpan.org</a> .</p>
1088 </p>
1089 <h2><a name="how_do_i_make_a_system___exit_on_controlc">How do I make a <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> exit on control-C?</a></h2>
1090 <p>You can't. You need to imitate the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system()</code></a> call (see <a href="file://C|\msysgit\mingw\html/pod/perlipc.html">the perlipc manpage</a> for
1091 sample code) and then have a signal handler for the INT signal that
1092 passes the signal on to the subprocess. Or you can check for it:</p>
1093 <pre>
1094 $rc = system($cmd);
1095 if ($rc &amp; 127) { die &quot;signal death&quot; }</pre>
1097 </p>
1098 <h2><a name="how_do_i_open_a_file_without_blocking">How do I open a file without blocking?</a></h2>
1099 <p>If you're lucky enough to be using a system that supports
1100 non-blocking reads (most Unixish systems do), you need only to use the
1101 O_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with
1102 sysopen():</p>
1103 <pre>
1104 use Fcntl;
1105 sysopen(FH, &quot;/foo/somefile&quot;, O_WRONLY|O_NDELAY|O_CREAT, 0644)
1106 or die &quot;can't open /foo/somefile: $!&quot;:</pre>
1108 </p>
1109 <h2><a name="how_do_i_tell_the_difference_between_errors_from_the_shell_and_perl">How do I tell the difference between errors from the shell and perl?</a></h2>
1110 <p>(answer contributed by brian d foy, <code>&lt;bdfoy@cpan.org&gt;</code></p>
1111 <p>When you run a Perl script, something else is running the script for you,
1112 and that something else may output error messages. The script might
1113 emit its own warnings and error messages. Most of the time you cannot
1114 tell who said what.</p>
1115 <p>You probably cannot fix the thing that runs perl, but you can change how
1116 perl outputs its warnings by defining a custom warning and die functions.</p>
1117 <p>Consider this script, which has an error you may not notice immediately.</p>
1118 <pre>
1119 #!/usr/locl/bin/perl</pre>
1120 <pre>
1121 print &quot;Hello World\n&quot;;</pre>
1122 <p>I get an error when I run this from my shell (which happens to be
1123 bash). That may look like perl forgot it has a <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print()</code></a> function,
1124 but my shebang line is not the path to perl, so the shell runs the
1125 script, and I get the error.</p>
1126 <pre>
1127 $ ./test
1128 ./test: line 3: print: command not found</pre>
1129 <p>A quick and dirty fix involves a little bit of code, but this may be all
1130 you need to figure out the problem.</p>
1131 <pre>
1132 #!/usr/bin/perl -w</pre>
1133 <pre>
1134 BEGIN {
1135 $SIG{__WARN__} = sub{ print STDERR &quot;Perl: &quot;, @_; };
1136 $SIG{__DIE__} = sub{ print STDERR &quot;Perl: &quot;, @_; exit 1};
1137 }</pre>
1138 <pre>
1139 $a = 1 + undef;
1140 $x / 0;
1141 __END__</pre>
1142 <p>The perl message comes out with ``Perl'' in front. The BEGIN block
1143 works at compile time so all of the compilation errors and warnings
1144 get the ``Perl:'' prefix too.</p>
1145 <pre>
1146 Perl: Useless use of division (/) in void context at ./test line 9.
1147 Perl: Name &quot;main::a&quot; used only once: possible typo at ./test line 8.
1148 Perl: Name &quot;main::x&quot; used only once: possible typo at ./test line 9.
1149 Perl: Use of uninitialized value in addition (+) at ./test line 8.
1150 Perl: Use of uninitialized value in division (/) at ./test line 9.
1151 Perl: Illegal division by zero at ./test line 9.
1152 Perl: Illegal division by zero at -e line 3.</pre>
1153 <p>If I don't see that ``Perl:'', it's not from perl.</p>
1154 <p>You could also just know all the perl errors, and although there are
1155 some people who may know all of them, you probably don't. However, they
1156 all should be in the perldiag manpage. If you don't find the error in
1157 there, it probably isn't a perl error.</p>
1158 <p>Looking up every message is not the easiest way, so let perl to do it
1159 for you. Use the diagnostics pragma with turns perl's normal messages
1160 into longer discussions on the topic.</p>
1161 <pre>
1162 use diagnostics;</pre>
1163 <p>If you don't get a paragraph or two of expanded discussion, it
1164 might not be perl's message.</p>
1166 </p>
1167 <h2><a name="how_do_i_install_a_module_from_cpan">How do I install a module from CPAN?</a></h2>
1168 <p>The easiest way is to have a module also named CPAN do it for you.
1169 This module comes with perl version 5.004 and later.</p>
1170 <pre>
1171 $ perl -MCPAN -e shell</pre>
1172 <pre>
1173 cpan shell -- CPAN exploration and modules installation (v1.59_54)
1174 ReadLine support enabled</pre>
1175 <pre>
1176 cpan&gt; install Some::Module</pre>
1177 <p>To manually install the CPAN module, or any well-behaved CPAN module
1178 for that matter, follow these steps:</p>
1179 <ol>
1180 <li>
1181 <p>Unpack the source into a temporary area.</p>
1182 </li>
1183 <li>
1184 <pre>
1185 perl Makefile.PL</pre>
1186 <li>
1187 <pre>
1188 make</pre>
1189 <li>
1190 <pre>
1191 make test</pre>
1192 <li>
1193 <pre>
1194 make install</pre>
1195 </ol>
1196 <p>If your version of perl is compiled without dynamic loading, then you
1197 just need to replace step 3 (<strong>make</strong>) with <strong>make perl</strong> and you will
1198 get a new <em>perl</em> binary with your extension linked in.</p>
1199 <p>See <a href="file://C|\msysgit\mingw\html/lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</a> for more details on building extensions.
1200 See also the next question, ``What's the difference between require
1201 and use?''.</p>
1203 </p>
1204 <h2><a name="what_s_the_difference_between_require_and_use">What's the difference between require and use?</a></h2>
1205 <p>Perl offers several different ways to include code from one file into
1206 another. Here are the deltas between the various inclusion constructs:</p>
1207 <pre>
1208 1) do $file is like eval `cat $file`, except the former
1209 1.1: searches @INC and updates %INC.
1210 1.2: bequeaths an *unrelated* lexical scope on the eval'ed code.</pre>
1211 <pre>
1212 2) require $file is like do $file, except the former
1213 2.1: checks for redundant loading, skipping already loaded files.
1214 2.2: raises an exception on failure to find, compile, or execute $file.</pre>
1215 <pre>
1216 3) require Module is like require &quot;Module.pm&quot;, except the former
1217 3.1: translates each &quot;::&quot; into your system's directory separator.
1218 3.2: primes the parser to disambiguate class Module as an indirect object.</pre>
1219 <pre>
1220 4) use Module is like require Module, except the former
1221 4.1: loads the module at compile time, not run-time.
1222 4.2: imports symbols and semantics from that package to the current one.</pre>
1223 <p>In general, you usually want <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> and a proper Perl module.</p>
1225 </p>
1226 <h2><a name="how_do_i_keep_my_own_module_library_directory">How do I keep my own module/library directory?</a></h2>
1227 <p>When you build modules, use the PREFIX and LIB options when generating
1228 Makefiles:</p>
1229 <pre>
1230 perl Makefile.PL PREFIX=/mydir/perl LIB=/mydir/perl/lib</pre>
1231 <p>then either set the PERL5LIB environment variable before you run
1232 scripts that use the modules/libraries (see <a href="file://C|\msysgit\mingw\html/pod/perlrun.html">the perlrun manpage</a>) or say</p>
1233 <pre>
1234 use lib '/mydir/perl/lib';</pre>
1235 <p>This is almost the same as</p>
1236 <pre>
1237 BEGIN {
1238 unshift(@INC, '/mydir/perl/lib');
1239 }</pre>
1240 <p>except that the lib module checks for machine-dependent subdirectories.
1241 See Perl's <a href="file://C|\msysgit\mingw\html/lib/lib.html">the lib manpage</a> for more information.</p>
1243 </p>
1244 <h2><a name="how_do_i_add_the_directory_my_program_lives_in_to_the_module_library_search_path">How do I add the directory my program lives in to the module/library search path?</a></h2>
1245 <pre>
1246 use FindBin;
1247 use lib &quot;$FindBin::Bin&quot;;
1248 use your_own_modules;</pre>
1250 </p>
1251 <h2><a name="how_do_i_add_a_directory_to_my_include_path___inc__at_runtime">How do I add a directory to my include path (@INC) at runtime?</a></h2>
1252 <p>Here are the suggested ways of modifying your include path:</p>
1253 <pre>
1254 the PERLLIB environment variable
1255 the PERL5LIB environment variable
1256 the perl -Idir command line flag
1257 the use lib pragma, as in
1258 use lib &quot;$ENV{HOME}/myown_perllib&quot;;</pre>
1259 <p>The latter is particularly useful because it knows about machine
1260 dependent architectures. The lib.pm pragmatic module was first
1261 included with the 5.002 release of Perl.</p>
1263 </p>
1264 <h2><a name="what_is_socket_ph_and_where_do_i_get_it">What is socket.ph and where do I get it?</a></h2>
1265 <p>It's a perl4-style file defining values for system networking
1266 constants. Sometimes it is built using h2ph when Perl is installed,
1267 but other times it is not. Modern programs <code>use Socket;</code> instead.</p>
1269 </p>
1270 <hr />
1271 <h1><a name="author_and_copyright">AUTHOR AND COPYRIGHT</a></h1>
1272 <p>Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and
1273 other authors as noted. All rights reserved.</p>
1274 <p>This documentation is free; you can redistribute it and/or modify it
1275 under the same terms as Perl itself.</p>
1276 <p>Irrespective of its distribution, all code examples in this file
1277 are hereby placed into the public domain. You are permitted and
1278 encouraged to use this code in your own programs for fun
1279 or for profit as you see fit. A simple comment in the code giving
1280 credit would be courteous but is not required.</p>
1281 <table border="0" width="100%" cellspacing="0" cellpadding="3">
1282 <tr><td class="block" style="background-color: #cccccc" valign="middle">
1283 <big><strong><span class="block">&nbsp;perlfaq8 - System Interaction</span></strong></big>
1284 </td></tr>
1285 </table>
1287 </body>
1289 </html>