Guest User

^__-/

a guest
Aug 21st, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.20 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Modern::Perl 2010;
  4. use Data::Dumper;
  5.  
  6.  
  7. sub readFileNums {
  8.         my ($fileName, $begin, $end) = @_;
  9.         die if (defined $end and $begin > $end); ## можно реверз сделать
  10.         my $cur = 1; ## см 1
  11.         open (IN, $fileName) || die;
  12.  
  13.         while ($cur < $begin) { # как и тут
  14.                 <IN>;
  15.                 ++$cur;
  16.         }
  17.  
  18.         sub {
  19.                 if (!wantarray) {
  20.                         return if defined $end and $cur == ($end + 1); # 1 тут реши сам с 0 или с 1
  21.                         ++$cur;
  22.                         <IN>;
  23.                 } else {
  24.                         my @text;
  25.                         if (defined $end) {
  26.                                 while (defined (my $str = <IN>) and ($cur++ <= $end)) { ## как и тут
  27.                                         push @text => $str;
  28.                                 }
  29.                         } else {
  30.                                 @text = <IN>;
  31.                         }
  32.                         @text;
  33.                 }
  34.         }
  35. }
  36.  
  37.  
  38. my $it = readFileNums "test.txt", 2;
  39. while (my $t = $it->()) {
  40.         print $t
  41. }
  42.  
  43. print $it->();
Advertisement
Add Comment
Please, Sign In to add comment