perlfaq5.html
来自「perl教程」· HTML 代码 · 共 664 行 · 第 1/5 页
HTML
664 行
<?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>perlfaq5 - Files and Formats</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>perlfaq5 - Files and Formats</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_flush_unbuffer_an_output_filehandle_why_must_i_do_this">How do I flush/unbuffer an output filehandle? Why must I do this?</a></li>
<li><a href="#how_do_i_change_one_line_in_a_file_delete_a_line_in_a_file_insert_a_line_in_the_middle_of_a_file_append_to_the_beginning_of_a_file">How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?</a></li>
<li><a href="#how_do_i_count_the_number_of_lines_in_a_file">How do I count the number of lines in a file?</a></li>
<li><a href="#how_can_i_use_perl_s_i_option_from_within_a_program">How can I use Perl's <code>-i</code> option from within a program?</a></li>
<li><a href="#how_can_i_copy_a_file">How can I copy a file?</a></li>
<li><a href="#how_do_i_make_a_temporary_file_name">How do I make a temporary file name?</a></li>
<li><a href="#how_can_i_manipulate_fixedrecordlength_files">How can I manipulate fixed-record-length files?</a></li>
<li><a href="#how_can_i_make_a_filehandle_local_to_a_subroutine_how_do_i_pass_filehandles_between_subroutines_how_do_i_make_an_array_of_filehandles">How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles?</a></li>
<li><a href="#how_can_i_use_a_filehandle_indirectly">How can I use a filehandle indirectly?</a></li>
<li><a href="#how_can_i_set_up_a_footer_format_to_be_used_with_write__">How can I set up a footer format to be used with write()?</a></li>
<li><a href="#how_can_i_write___into_a_string">How can I <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> into a string?</a></li>
<li><a href="#how_can_i_output_my_numbers_with_commas_added">How can I output my numbers with commas added?</a></li>
<li><a href="#how_can_i_translate_tildes_____in_a_filename">How can I translate tildes (~) in a filename?</a></li>
<li><a href="#how_come_when_i_open_a_file_readwrite_it_wipes_it_out">How come when I open a file read-write it wipes it out?</a></li>
<li><a href="#why_do_i_sometimes_get_an_argument_list_too_long_when_i_use____">Why do I sometimes get an "Argument list too long" when I use <*>?</a></li>
<li><a href="#is_there_a_leak_bug_in_glob__">Is there a leak/bug in glob()?</a></li>
<li><a href="#how_can_i_open_a_file_with_a_leading___or_trailing_blanks">How can I open a file with a leading ">" or trailing blanks?</a></li>
<li><a href="#how_can_i_reliably_rename_a_file">How can I reliably rename a file?</a></li>
<li><a href="#how_can_i_lock_a_file">How can I lock a file?</a></li>
<li><a href="#why_can_t_i_just_open_fh___file_lock_">Why can't I just open(FH, ">file.lock")?</a></li>
<li><a href="#i_still_don_t_get_locking__i_just_want_to_increment_the_number_in_the_file__how_can_i_do_this">I still don't get locking. I just want to increment the number in the file. How can I do this?</a></li>
<li><a href="#all_i_want_to_do_is_append_a_small_amount_of_text_to_the_end_of_a_file__do_i_still_have_to_use_locking">All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?</a></li>
<li><a href="#how_do_i_randomly_update_a_binary_file">How do I randomly update a binary file?</a></li>
<li><a href="#how_do_i_get_a_file_s_timestamp_in_perl">How do I get a file's timestamp in perl?</a></li>
<li><a href="#how_do_i_set_a_file_s_timestamp_in_perl">How do I set a file's timestamp in perl?</a></li>
<li><a href="#how_do_i_print_to_more_than_one_file_at_once">How do I print to more than one file at once?</a></li>
<li><a href="#how_can_i_read_in_an_entire_file_all_at_once">How can I read in an entire file all at once?</a></li>
<li><a href="#how_can_i_read_in_a_file_by_paragraphs">How can I read in a file by paragraphs?</a></li>
<li><a href="#how_can_i_read_a_single_character_from_a_file_from_the_keyboard">How can I read a single character from a file? From the keyboard?</a></li>
<li><a href="#how_can_i_tell_whether_there_s_a_character_waiting_on_a_filehandle">How can I tell whether there's a character waiting on a filehandle?</a></li>
<li><a href="#how_do_i_do_a_tail_f_in_perl">How do I do a <code>tail -f</code> in perl?</a></li>
<li><a href="#how_do_i_dup___a_filehandle_in_perl">How do I <code>dup()</code> a filehandle in Perl?</a></li>
<li><a href="#how_do_i_close_a_file_descriptor_by_number">How do I close a file descriptor by number?</a></li>
<li><a href="#why_can_t_i_use_c__temp_foo_in_dos_paths_why_doesn_t__c__temp_foo_exe__work">Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work?</a></li>
<li><a href="#why_doesn_t_glob______get_all_the_files">Why doesn't <a href="../../lib/Pod/perlfunc.html#item_glob"><code>glob("*.*")</code></a> get all the files?</a></li>
<li><a href="#why_does_perl_let_me_delete_readonly_files_why_does_i_clobber_protected_files_isn_t_this_a_bug_in_perl">Why does Perl let me delete read-only files? Why does <code>-i</code> clobber protected files? Isn't this a bug in Perl?</a></li>
<li><a href="#how_do_i_select_a_random_line_from_a_file">How do I select a random line from a file?</a></li>
<li><a href="#why_do_i_get_weird_spaces_when_i_print_an_array_of_lines">Why do I get weird spaces when I print an array of lines?</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>perlfaq5 - Files and Formats ($Revision: 1.42 $, $Date: 2005/12/31 00:54:37 $)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This section deals with I/O and the "f" issues: filehandles, flushing,
formats, and footers.</p>
<p>
</p>
<h2><a name="how_do_i_flush_unbuffer_an_output_filehandle_why_must_i_do_this">How do I flush/unbuffer an output filehandle? Why must I do this?</a></h2>
<p>Perl does not support truly unbuffered output (except
insofar as you can <a href="../../lib/Pod/perlfunc.html#item_syswrite"><code>syswrite(OUT, $char, 1)</code></a>), although it
does support is "command buffering", in which a physical
write is performed after every output command.</p>
<p>The C standard I/O library (stdio) normally buffers
characters sent to devices so that there isn't a system call
for each byte. In most stdio implementations, the type of
output buffering and the size of the buffer varies according
to the type of device. Perl's <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> functions
normally buffer output, while <a href="../../lib/Pod/perlfunc.html#item_syswrite"><code>syswrite()</code></a> bypasses buffering
all together.</p>
<p>If you want your output to be sent immediately when you
execute <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> or <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> (for instance, for some network
protocols), you must set the handle's autoflush flag. This
flag is the Perl variable $| and when it is set to a true
value, Perl will flush the handle's buffer after each
<a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> or write(). Setting $| affects buffering only for
the currently selected default file handle. You choose this
handle with the one argument <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> call (see
<a href="../../lib/Pod/perlvar.html#_e_verbar_">$| in the perlvar manpage</a> and <a href="../../lib/Pod/perlfunc.html#select">select in the perlfunc manpage</a>).</p>
<p>Use <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> to choose the desired handle, then set its
per-filehandle variables.</p>
<pre>
<span class="variable">$old_fh</span> <span class="operator">=</span> <span class="keyword">select</span><span class="operator">(</span><span class="variable">OUTPUT_HANDLE</span><span class="operator">);</span>
<span class="variable">$|</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="keyword">select</span><span class="operator">(</span><span class="variable">$old_fh</span><span class="operator">);</span>
</pre>
<p>Some idioms can handle this in a single statement:</p>
<pre>
<span class="keyword">select</span><span class="operator">((</span><span class="keyword">select</span><span class="operator">(</span><span class="variable">OUTPUT_HANDLE</span><span class="operator">),</span> <span class="variable">$|</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">)</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<pre>
<span class="variable">$|</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">,</span> <span class="keyword">select</span> <span class="variable">$_</span> <span class="keyword">for</span> <span class="keyword">select</span> <span class="variable">OUTPUT_HANDLE</span><span class="operator">;</span>
</pre>
<p>Some modules offer object-oriented access to handles and their
variables, although they may be overkill if this is the only
thing you do with them. You can use IO::Handle:</p>
<pre>
<span class="keyword">use</span> <span class="variable">IO::Handle</span><span class="operator">;</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">DEV</span><span class="operator">,</span> <span class="string">">/dev/printer"</span><span class="operator">);</span> <span class="comment"># but is this?</span>
<span class="variable">DEV</span><span class="operator">-></span><span class="variable">autoflush</span><span class="operator">(</span><span class="number">1</span><span class="operator">);</span>
</pre>
<p>or IO::Socket:</p>
<pre>
<span class="keyword">use</span> <span class="variable">IO::Socket</span><span class="operator">;</span> <span class="comment"># this one is kinda a pipe?</span>
<span class="keyword">my</span> <span class="variable">$sock</span> <span class="operator">=</span> <span class="variable">IO::Socket::INET</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span> <span class="string">'www.example.com:80'</span> <span class="operator">);</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?