Here's a version using an anonymous sub. I'm not saying it's perfect, but I think it's probably much more readable, etc. than your initial version. I've changed many of your variable names. :)

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.

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; } } }
To be invoked like this:
Text::Break::break(\@text, [ $lines ]);
where $lines is an optional argument specifying the number of lines to break on.

BTW: there's an example of Pager class in Damian Conway's Object Oriented Perl.


In reply to Re: Breaking Text by btrott
in thread Breaking Text by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.