cvs commit: perlfaq perlfaq5.pod perlfaq6.pod

[email protected]
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     02/09/21 14:04:18

  Modified:    .        perlfaq5.pod perlfaq6.pod
  Log:
  * How do I print to more than one file at once?
       + removed unixist discussion of tee.  Perl has modules to do this now. :)
  
  * How can I read in an entire file all at once?
      + mention File::Slurp
  
  Revision  Changes    Path
  1.26      +13 -21    perlfaq/perlfaq5.pod
  
  Index: perlfaq5.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -w -r1.25 -r1.26
  --- perlfaq5.pod	5 Sep 2002 00:48:40 -0000	1.25
  +++ perlfaq5.pod	21 Sep 2002 21:04:17 -0000	1.26
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq5 - Files and Formats ($Revision: 1.25 $, $Date: 2002/09/05 00:48:40 $)
  +perlfaq5 - Files and Formats ($Revision: 1.26 $, $Date: 2002/09/21 21:04:17 $)
   
   =head1 DESCRIPTION
   
  @@ -686,30 +686,22 @@
   
   =head2 How do I print to more than one file at once?
   
  -If you only have to do this once, you can do this:
  +To connect one filehandle to several output filehandles,
  +you can use the IO::Tee or Tie::FileHandle::Multiplex modules.
   
  -    for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
  +If you only have to do this once, you can print individually
  +to each filehandle.
   
  -To connect up to one filehandle to several output filehandles, it's
  -easiest to use the tee(1) program if you have it, and let it take care
  -of the multiplexing:
  -
  -    open (FH, "| tee file1 file2 file3");
  -
  -Or even:
  -
  -    # make STDOUT go to three files, plus original STDOUT
  -    open (STDOUT, "| tee file1 file2 file3") or die "Teeing off: $!\n";
  -    print "whatever\n"                       or die "Writing: $!\n";
  -    close(STDOUT)                            or die "Closing: $!\n";
  -
  -Otherwise you'll have to write your own multiplexing print
  -function--or your own tee program--or use Tom Christiansen's,
  -at http://www.cpan.org/authors/id/TOMC/scripts/tct.gz , which is
  -written in Perl and offers much greater functionality
  -than the stock version.
  +    for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
   
   =head2 How can I read in an entire file all at once?
  +
  +You can use the File::Slurp module to do it in one step.
  +
  +	use File::Slurp;
  +	
  +	$all_of_it = read_file($filename); # entire file in scalar
  +    @all_lines = read_file($filename); # one line perl element
   
   The customary Perl approach for processing all the lines in a file is to
   do so one line at a time:
  
  
  
  1.17      +28 -28    perlfaq/perlfaq6.pod
  
  Index: perlfaq6.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq6.pod,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -w -r1.16 -r1.17
  --- perlfaq6.pod	20 Jul 2002 17:17:32 -0000	1.16
  +++ perlfaq6.pod	21 Sep 2002 21:04:17 -0000	1.17
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq6 - Regular Expressions ($Revision: 1.16 $, $Date: 2002/07/20 17:17:32 $)
  +perlfaq6 - Regular Expressions ($Revision: 1.17 $, $Date: 2002/09/21 21:04:17 $)
   
   =head1 DESCRIPTION
   
  @@ -147,34 +147,34 @@
   
   =head2 I put a regular expression into $/ but it didn't work. What's wrong?
   
  -$/ must be a string, not a regular expression.  Awk has to be better
  -for something. :-)
  +As of Perl 5.8.0, $/ has to be a string.  This may change in 5.10,
  +but don't get your hopes up. Until then, you can use these examples
  +if you really need to do this.
   
  -Actually, you could do this if you don't mind reading the whole file
  -into memory:
  +Use the four argument form of sysread to continually add to 
  +a buffer.  After you add to the buffer, you check if you have a 
  +complete line (using your regular expression).
   
  -    undef $/;
  -    @records = split /your_pattern/, <FH>;
  +       local $_ = "";
  +       while( sysread FH, $_, 8192, length ) {
  +          while( s/^((?s).*?)your_pattern/ ) {
  +             my $record = $1;
  +             # do stuff here.
  +          }
  +       }
  + 
  + You can do the same thing with foreach and a match using the
  + c flag and the \G anchor, if you do not mind your entire file
  + being in memory at the end.
  +       
  +       local $_ = "";
  +       while( sysread FH, $_, 8192, length ) {
  +          foreach my $record ( m/\G((?s).*?)your_pattern/gc ) {
  +             # do stuff here.
  +          }
  +          substr( $_, 0, pos ) = "" if pos;
  +       }
   
  -The Net::Telnet module (available from CPAN) has the capability to
  -wait for a pattern in the input stream, or timeout if it doesn't
  -appear within a certain time.
  -
  -    ## Create a file with three lines.
  -    open FH, ">file";
  -    print FH "The first line\nThe second line\nThe third line\n";
  -    close FH;
  -
  -    ## Get a read/write filehandle to it.
  -    $fh = new IO::File "+<file";
  -
  -    ## Attach it to a "stream" object.
  -    use Net::Telnet;
  -    $file = new Net::Telnet (-fhopen => $fh);
  -
  -    ## Search for the second line and print out the third.
  -    $file->waitfor('/second line\n/');
  -    print $file->getline;
   
   =head2 How do I substitute case insensitively on the LHS while preserving case on the RHS?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.