home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / getline.pl < prev    next >
Encoding:
Text File  |  1990-01-14  |  658 b   |  29 lines

  1. # This getline() function returns the next line from an 
  2. # input file.  It checks for lines ending in an escaped
  3. # newline (i.e. backslash) and concats the next line if
  4. # it finds such.  Use this way:
  5. #     open(AFILE,$pathname);
  6. #     $line = &getline('AFILE');
  7. #     
  8. # This shows good use of local variables, passing 
  9. # file descriptors by name, and the redo loop control
  10. # mechanism.  Perl optimizes "/xxx$/" tests as well as appending
  11. # the next input line "$foo .= <bar>", so this is pretty quick.
  12.  
  13. sub getline {
  14.     local($file) = @_;
  15.     local($_);
  16.  
  17.     $_ = <$file>;
  18.     {
  19.     chop;
  20.     if (/\\$/) {
  21.         chop;
  22.         $_ .= <$file>;
  23.         redo;
  24.     }
  25.     }
  26.     $_;
  27. }
  28.