in reply to Breaking Text
Also, you said that you expect the first argument to be Text::Break; so you're calling this as a class method, then? I'm not sure that's necessary, really, because your routine isn't OO in the least. So I changed that. Mine also expects the array to be passed by reference.
To be invoked like this:package Text::Break; $VERSION = 0.01; :) sub break { my($text, $how_many) = @_; $how_many ||= 24; my $total = @$text; ## Anon sub $printer is a closure and has access ## to the $text array reference. Yes, there's really ## no point to having an anon sub here; I may as ## well just write the print statement inline. Oh well. my $printer = sub { my($start, $how_many) = @_; print @{$text}[$start..$start+$how_many-1]; }; my $printed = 0; ## Separate the printing from the input, etc. PAGER: while ($printed <= $total) { $printed = 0 unless $printed >= 0; $printer->($printed, $how_many); $printed += $how_many; print $printed >= $total ? "--No More--" : "--More--"; my $in = <STDIN>; if ($in =~ /^b/i) { $printed -= $how_many * 2; } elsif ($in =~ /^x/i || $in =~ /^q/i) { last PAGER; } } }
where $lines is an optional argument specifying the number of lines to break on.Text::Break::break(\@text, [ $lines ]);
BTW: there's an example of Pager class in Damian Conway's Object Oriented Perl.
|
|---|