perlfaq8.html

来自「perl教程」· HTML 代码 · 共 800 行 · 第 1/5 页

HTML
800
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlfaq8 - System Interaction</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlfaq8 - System Interaction</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<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>
		<li><a href="#how_come_exec___doesn_t_return">How come <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec()</code></a> doesn't return?</a></li>
		<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>
		<li><a href="#how_do_i_print_something_out_in_color">How do I print something out in color?</a></li>
		<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>
		<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>
		<li><a href="#how_do_i_clear_the_screen">How do I clear the screen?</a></li>
		<li><a href="#how_do_i_get_the_screen_size">How do I get the screen size?</a></li>
		<li><a href="#how_do_i_ask_the_user_for_a_password">How do I ask the user for a password?</a></li>
		<li><a href="#how_do_i_read_and_write_the_serial_port">How do I read and write the serial port?</a></li>
		<li><a href="#how_do_i_decode_encrypted_password_files">How do I decode encrypted password files?</a></li>
		<li><a href="#how_do_i_start_a_process_in_the_background">How do I start a process in the background?</a></li>
		<li><a href="#how_do_i_trap_control_characters_signals">How do I trap control characters/signals?</a></li>
		<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>
		<li><a href="#how_do_i_set_the_time_and_date">How do I set the time and date?</a></li>
		<li><a href="#how_can_i_sleep___or_alarm___for_under_a_second">How can I <a href="../../lib/Pod/perlfunc.html#item_sleep"><code>sleep()</code></a> or <a href="../../lib/Pod/perlfunc.html#item_alarm"><code>alarm()</code></a> for under a second?</a></li>
		<li><a href="#how_can_i_measure_time_under_a_second">How can I measure time under a second?</a></li>
		<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>
		<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 &quot;Protocol not supported&quot; mean?</a></li>
		<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>
		<li><a href="#where_do_i_get_the_include_files_to_do_ioctl___or_syscall__">Where do I get the include files to do <a href="../../lib/Pod/perlfunc.html#item_ioctl"><code>ioctl()</code></a> or syscall()?</a></li>
		<li><a href="#why_do_setuid_perl_scripts_complain_about_kernel_problems">Why do setuid perl scripts complain about kernel problems?</a></li>
		<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>
		<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>
		<li><a href="#how_can_i_capture_stderr_from_an_external_command">How can I capture STDERR from an external command?</a></li>
		<li><a href="#why_doesn_t_open___return_an_error_when_a_pipe_open_fails">Why doesn't <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> return an error when a pipe open fails?</a></li>
		<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>
		<li><a href="#how_can_i_call_backticks_without_shell_processing">How can I call backticks without shell processing?</a></li>
		<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>
		<li><a href="#how_can_i_convert_my_shell_script_to_perl">How can I convert my shell script to perl?</a></li>
		<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>
		<li><a href="#how_can_i_write_expect_in_perl">How can I write expect in Perl?</a></li>
		<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 &quot;ps&quot;?</a></li>
		<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>
		<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>
		<li><a href="#how_do_i_fork_a_daemon_process">How do I fork a daemon process?</a></li>
		<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>
		<li><a href="#how_do_i_timeout_a_slow_event">How do I timeout a slow event?</a></li>
		<li><a href="#how_do_i_set_cpu_limits">How do I set CPU limits?</a></li>
		<li><a href="#how_do_i_avoid_zombies_on_a_unix_system">How do I avoid zombies on a Unix system?</a></li>
		<li><a href="#how_do_i_use_an_sql_database">How do I use an SQL database?</a></li>
		<li><a href="#how_do_i_make_a_system___exit_on_controlc">How do I make a <a href="../../lib/Pod/perlfunc.html#item_system"><code>system()</code></a> exit on control-C?</a></li>
		<li><a href="#how_do_i_open_a_file_without_blocking">How do I open a file without blocking?</a></li>
		<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>
		<li><a href="#how_do_i_install_a_module_from_cpan">How do I install a module from CPAN?</a></li>
		<li><a href="#what_s_the_difference_between_require_and_use">What's the difference between require and use?</a></li>
		<li><a href="#how_do_i_keep_my_own_module_library_directory">How do I keep my own module/library directory?</a></li>
		<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>
		<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>
		<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>
	</ul>

	<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfaq8 - System Interaction ($Revision: 1.27 $, $Date: 2005/12/31 00:54:37 $)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This section of the Perl FAQ covers questions involving operating
system interaction.  Topics include interprocess communication (IPC),
control over the user-interface (keyboard, screen and pointing
devices), and most anything else not related to data manipulation.</p>
<p>Read the FAQs and documentation specific to the port of perl to your
operating system (eg, <a href="../../lib/Pod/perlvms.html">the perlvms manpage</a>, <a href="../../lib/Pod/perlplan9.html">the perlplan9 manpage</a>, ...).  These should
contain more detailed information on the vagaries of your perl.</p>
<p>
</p>
<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>
<p>The $^O variable ($OSNAME if you use English) contains an indication of
the name of the operating system (not its release number) that your perl
binary was built for.</p>
<p>
</p>
<h2><a name="how_come_exec___doesn_t_return">How come <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec()</code></a> doesn't return?</a></h2>
<p>Because that's what it does: it replaces your currently running
program with a different one.  If you want to keep going (as is
probably the case if you're asking this question) use <a href="../../lib/Pod/perlfunc.html#item_system"><code>system()</code></a>
instead.</p>
<p>
</p>
<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>
<p>How you access/control keyboards, screens, and pointing devices
(&quot;mice&quot;) is system-dependent.  Try the following modules:</p>
<dl>
<dt><strong><a name="item_keyboard">Keyboard</a></strong>

<dd>
<pre>
        Term::Cap               Standard perl distribution
        Term::ReadKey           CPAN
        Term::ReadLine::Gnu     CPAN
        Term::ReadLine::Perl    CPAN
        Term::Screen            CPAN</pre>
</dd>
<dt><strong><a name="item_screen">Screen</a></strong>

<dd>
<pre>
        Term::Cap               Standard perl distribution
        Curses                  CPAN
        Term::ANSIColor         CPAN</pre>
</dd>
<dt><strong><a name="item_mouse">Mouse</a></strong>

<dd>
<pre>
        Tk                      CPAN</pre>
</dd>
</dl>
<p>Some of these specific cases are shown as examples in other answers
in this section of the perlfaq.</p>
<p>
</p>
<h2><a name="how_do_i_print_something_out_in_color">How do I print something out in color?</a></h2>
<p>In general, you don't, because you don't know whether
the recipient has a color-aware display device.  If you
know that they have an ANSI terminal that understands
color, you can use the Term::ANSIColor module from CPAN:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Term::ANSIColor</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">color</span><span class="operator">(</span><span class="string">"red"</span><span class="operator">),</span> <span class="string">"Stop!\n"</span><span class="operator">,</span> <span class="variable">color</span><span class="operator">(</span><span class="string">"reset"</span><span class="operator">);</span>
    <span class="keyword">print</span> <span class="variable">color</span><span class="operator">(</span><span class="string">"green"</span><span class="operator">),</span> <span class="string">"Go!\n"</span><span class="operator">,</span> <span class="variable">color</span><span class="operator">(</span><span class="string">"reset"</span><span class="operator">);</span>
</pre>
<p>Or like this:</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?