In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/a3cdda958864a31cda0f11d63006f3591e345b7e?hp=1f0fc1c94b0da3182a0963953d7c62e0b6a20afe>
- Log ----------------------------------------------------------------- commit a3cdda958864a31cda0f11d63006f3591e345b7e Author: Father Chrysostomos <spr...@cpan.org> Date: Sat Feb 19 07:05:36 2011 -0800 Minor perlfaq6 tweaks M pod/perlfaq.pod M pod/perlfaq6.pod commit 5c121ae8f85c7837608b02373917934b2997b0c1 Author: Father Chrysostomos <spr...@cpan.org> Date: Sat Feb 19 06:46:36 2011 -0800 Re-order âHow can I read in an entire file all at once?â This entry has been edited about five times in the past without regard to the way the text flows. Each new bit has been slapped on to the top, so parts of it are now repetitive. M pod/perlfaq5.pod commit 09c1cbc255467ec821a618561761fdb554dc0f55 Author: Father Chrysostomos <spr...@cpan.org> Date: Sat Feb 19 06:15:04 2011 -0800 Minor perlfaq5 tweaks M pod/perlfaq5.pod ----------------------------------------------------------------------- Summary of changes: pod/perlfaq.pod | 2 +- pod/perlfaq5.pod | 80 +++++++++++++++++++++++++++--------------------------- pod/perlfaq6.pod | 4 +- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/pod/perlfaq.pod b/pod/perlfaq.pod index 09b40cf..614ff56 100644 --- a/pod/perlfaq.pod +++ b/pod/perlfaq.pod @@ -914,7 +914,7 @@ I put a regular expression into $/ but it didn't work. What's wrong? =item * -How do I substitute case insensitively on the LHS while preserving case on the RHS? +How do I substitute case-insensitively on the LHS while preserving case on the RHS? =item * diff --git a/pod/perlfaq5.pod b/pod/perlfaq5.pod index 1866a03..76b6d3e 100644 --- a/pod/perlfaq5.pod +++ b/pod/perlfaq5.pod @@ -494,8 +494,8 @@ Berkeley-style ps: } We've used a hash slice in order to easily handle the fields of each row. -Storing the keys in an array means it's easy to operate on them as a -group or loop over them with for. It also avoids polluting the program +Storing the keys in an array makes it easy to operate on them as a +group or loop over them with C<for>. It also avoids polluting the program with global variables and using symbolic references. =head2 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? @@ -542,7 +542,7 @@ check out the Symbol or IO::Handle modules. =head2 How can I use a filehandle indirectly? X<filehandle, indirect> -An indirect filehandle is using something other than a symbol +An indirect filehandle is the use of something other than a symbol in a place that a filehandle is expected. Here are ways to get indirect filehandles: @@ -553,8 +553,7 @@ to get indirect filehandles: $fh = *SOME_FH{IO}; # blessed IO::Handle from *SOME_FH typeglob Or, you can use the C<new> method from one of the IO::* modules to -create an anonymous filehandle, store that in a scalar variable, -and use it as though it were a normal filehandle. +create an anonymous filehandle and store that in a scalar variable. use IO::Handle; # 5.004 or higher my $fh = IO::Handle->new(); @@ -843,7 +842,7 @@ be an atomic operation over NFS. That is, two processes might both successfully create or unlink the same file! Therefore O_EXCL isn't as exclusive as you might wish. -See also the new L<perlopentut>. +See also L<perlopentut>. =head2 Why do I sometimes get an "Argument list too long" when I use E<lt>*E<gt>? X<argument list too long> @@ -873,13 +872,13 @@ X<filename, special characters> (contributed by Brian McCauley) -The special two argument form of Perl's open() function ignores +The special two-argument form of Perl's open() function ignores trailing blanks in filenames and infers the mode from certain leading characters (or a trailing "|"). In older versions of Perl this was the only version of open() and so it is prevalent in old code and books. -Unless you have a particular reason to use the two argument form you -should use the three argument form of open() which does not treat any +Unless you have a particular reason to use the two-argument form you +should use the three-argument form of open() which does not treat any characters in the filename as special. open my $fh, "<", " file "; # filename is " file " @@ -998,7 +997,7 @@ X<append> X<file, append> If you are on a system that correctly implements C<flock> and you use the example appending code from "perldoc -f flock" everything will be OK even if the OS you are on doesn't implement append mode correctly -(if such a system exists.) So if you are happy to restrict yourself to +(if such a system exists). So if you are happy to restrict yourself to OSs that implement C<flock> (and that's not really much of a restriction) then that is what you should do. @@ -1017,7 +1016,7 @@ C<syswrite> function which is simply a wrapper around your system's C<write(2)> system call. There is still a small theoretical chance that a signal will interrupt -the system level C<write()> operation before completion. There is also +the system-level C<write()> operation before completion. There is also a possibility that some STDIO implementations may call multiple system level C<write()>s even if the buffer was empty to start. There may be some systems where this probability is reduced to zero, and this is @@ -1119,26 +1118,6 @@ to each filehandle. =head2 How can I read in an entire file all at once? X<slurp> X<file, slurping> -Are you sure you want to read the entire file and store it in memory? -If you mmap the file, you can virtually load the entire file into a -string without actually storing it in memory: - - use File::Map qw(map_file); - - map_file my $string, $filename; - -Once mapped, you can treat C<$string> as you would any other string. -Since you don't necessarily have to load the data, mmap-ing can be -very fast and may not increase your memory footprint. - -If you want to load the entire file, you can use the C<File::Slurp> -module to do it in one one simple and efficient step: - - use File::Slurp; - - my $all_of_it = read_file($filename); # entire file in scalar - my @all_lines = read_file($filename); # one line per element - The customary Perl approach for processing all the lines in a file is to do so one line at a time: @@ -1157,13 +1136,35 @@ you see someone do this: my @lines = <INPUT>; You should think long and hard about why you need everything loaded at -once. It's just not a scalable solution. You might also find it more +once. It's just not a scalable solution. + +If you "mmap" the file with the File::Map module from +CPAN, you can virtually load the entire file into a +string without actually storing it in memory: + + use File::Map qw(map_file); + + map_file my $string, $filename; + +Once mapped, you can treat C<$string> as you would any other string. +Since you don't necessarily have to load the data, mmap-ing can be +very fast and may not increase your memory footprint. + +You might also find it more fun to use the standard C<Tie::File> module, or the C<DB_File> module's C<$DB_RECNO> bindings, which allow you to tie an array to a file so that -accessing an element the array actually accesses the corresponding +accessing an element of the array actually accesses the corresponding line in the file. -You can read the entire filehandle contents into a scalar. +If you want to load the entire file, you can use the C<File::Slurp> +module to do it in one one simple and efficient step: + + use File::Slurp; + + my $all_of_it = read_file($filename); # entire file in scalar + my @all_lines = read_file($filename); # one line per element + +Or you can read the entire file contents into a scalar like this: my $var; { @@ -1177,8 +1178,7 @@ close the file at block exit. If the file is already open, just use this: my $var = do { local $/; <$fh> }; -You can do that one better by using a localized C<@ARGV> so you can -eliminate the C<open>: +You can also use a localized C<@ARGV> to eliminate the C<open>: my $var = do { local( @ARGV, $/ ) = $file; <> }; @@ -1186,7 +1186,7 @@ For ordinary files you can also use the C<read> function. read( $fh, $var, -s $fh ); -That third argument tests the byte size of the data on the C<INPUT> filehandle +That third argument tests the byte size of the data on the C<$fh> filehandle and reads that many bytes into the buffer C<$var>. =head2 How can I read in a file by paragraphs? @@ -1281,7 +1281,7 @@ not POSIX, not Unix, etc.) systems. You should also check out the Frequently Asked Questions list in comp.unix.* for things like this: the answer is essentially the same. -It's very system dependent. Here's one solution that works on BSD +It's very system-dependent. Here's one solution that works on BSD systems: sub key_ready { @@ -1320,7 +1320,7 @@ Or write a small C program using the editor of champions: % ./fionread 0x4004667f -And then hard code it, leaving porting as an exercise to your successor. +And then hard-code it, leaving porting as an exercise to your successor. $FIONREAD = 0x4004667f; # XXX: opsys dependent @@ -1373,7 +1373,7 @@ Or even with a literal numeric descriptor: my $fd = $ENV{MHCONTEXTFD}; open $mhcontext, "<&=$fd"; # like fdopen(3S) -Note that "<&STDIN" makes a copy, but "<&=STDIN" make +Note that "<&STDIN" makes a copy, but "<&=STDIN" makes an alias. That means if you close an aliased handle, all aliases become inaccessible. This is not true with a copied one. diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index 1a37978..3066187 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -235,7 +235,7 @@ being in memory at the end. } -=head2 How do I substitute case insensitively on the LHS while preserving case on the RHS? +=head2 How do I substitute case-insensitively on the LHS while preserving case on the RHS? X<replace, case preserving> X<substitute, case preserving> X<substitution, case preserving> X<s, case preserving> @@ -644,7 +644,7 @@ An example: Notice how the second substitution stopped matching as soon as it encountered "y ". The C<*?> quantifier effectively tells the regular expression engine to find a match as quickly as possible and pass -control on to whatever is next in line, like you would if you were +control on to whatever is next in line, as you would if you were playing hot potato. =head2 How do I process each word on each line? -- Perl5 Master Repository