When executing a perl script from the command line how can I ensure that my output doesn't scroll off the screen?
In others words, how do I mimic the functionality of the unix "more" or "less" commands?
When executing a perl script from the command line how can I ensure that my output doesn't scroll off the screen?
In others words, how do I mimic the functionality of the unix "more" or "less" commands?
Can't the user just pipe the output to less
? That gives them the option of using their favourite pager, or even not using any pager at all, if they prefer that.
As Matti Virkkunen says, it's better that the user pipes your script to less
.
A *nix user would expect output in plain text, so (s)he can pipe it to other commands if they need to. Making your script not displaying output as plain text, you user may find your script less usable.
For quick and dirty can pipe the text to less or more:
my $text = <<'EOD';
Lots
and
lots
of
text
EOD
my $pager = $ENV{PAGER} || 'less';
open(my $less, '|-', $pager, '-e') || die "Cannot pipe to $pager: $!";
print $less $text;
close($less);
There are various less/more flags to allow the script to continue when it reaches the bottom of the text.