Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to pause display of long STDOUT (http://www.velocityreviews.com/forums/t883770-how-to-pause-display-of-long-stdout.html)

Default@IO_Error_1011101.xyz 11-20-2003 07:11 PM

How to pause display of long STDOUT
 
I've implemented a pause in the help section of this.
Thing is in that case i knew the text was more than a screenfull.
What if i'm not sure if the display will be more than a screenfull,
is there a 'more' or 'less' like command i can implement?

Also if anyone is familiar with Getopt... is there a way to collect the
initial file args from this command line w/o using a -<option>?

#!
use strict;
use warnings;
use Getopt::Long;

#Gather input files#
my $file = "";
my $dbfile = "words";
my $files = lc(shift) || help() && die "\n";
unshift (@ARGV, $files);

#Get options#
my $opt_help = ''; # -h Help.
my $opt_ignore = ''; # -i Ignore case while searching words.
my $opt_select = ''; # -s Select a database (or create one).
my $opt_read = ''; # -r Read from the database.
my $opt_write = ''; # -w Write to the database.
my $opt_clear = ''; # -x Clear the database.
GetOptions ('h' => \$opt_help, 'x' => \$opt_clear,
'i' => \$opt_ignore, 's' => \$opt_select,
'r' => \$opt_read, 'w' => \$opt_write);

#Main#
if ($opt_help eq "1") {help();}
if ($opt_select eq "1") {select_db();}
if ($opt_write eq "1") {write_db();}
if ($opt_read eq "1") {read_db();}
if ($opt_clear eq "1") {clear_db();}
if ($opt_help eq "" && $opt_select eq "" && $opt_write eq "" &&
$opt_read eq "" && $opt_clear eq "")
{help() && die "\nQuitting... No options selected.\n";}

print "\nDone.\n";

#Subroutines#
sub select_db
{
my $new_db = "";
print "\n" . ' ' . '='x78 . "\n";
print " Select or create a database. " .
'The default database is: "' . "$dbfile" . '"' . "\n";
print ' ' . '='x78 . "\n\n";
while ($new_db eq "")
{
print 'Enter the name of the database: ';
chomp ($new_db = lc<STDIN>);
}
my $new_db_1 = "$new_db" . ".pag";
my $new_db_2 = "$new_db" . ".dir";
if (!-e $new_db_1 and !-e $new_db_2)
{
print "\n".'Create new database "'."$new_db".'" (y or n) ';
chomp (my $verify = lc<STDIN>);
die "\n".'Quitting: "'."$new_db".'" was not created'."\n"
unless ($verify eq "y");
}
else {print 'Selecting the "'."$new_db".'" database.'."\n\n";}
$dbfile = $new_db;
}
sub write_db
{
print "\nUpdating database...\n";
dbmopen(my %WORDS,"$dbfile",0644)||die "\nCan't open $dbfile $!\n";
foreach $file (<${files}>)
{
open(READFILE, "$file") || warn "\nCan't open $file $!\n";
while (<READFILE>)
{
foreach my $word (split (/\W+/))
{
if ($opt_ignore eq "1") {$word = lc($word);}
$WORDS{$word}++;
}
}
close (READFILE) || warn "\nCan't close $file $!\n";
}
dbmclose(%WORDS) || warn "\nCan't close $dbfile #!\n";
print "\nFinished updating database.\n\n";
}
sub read_db
{
print "\n".' '.'='x78 . "\n This will read the ".'"'."$dbfile" .
'" database.' . "\n" . ' ' . '='x78 . "\n\n";
print "Press <Enter> to continue.";
my $pause = <STDIN>; $pause = undef; print "\n\n";
dbmopen(my %WORDS,"$dbfile",0644)||die "Can't open $dbfile:\n#!\n";
while ((my $key, my $value) = each(%WORDS))
{print "$key has been seen $value times.\n";}
dbmclose(%WORDS) || warn "\nCan't close $dbfile #!\n";
}
sub clear_db
{
print "\n" . ' ' . '='x78 . "\n";
print ' This will clear the "' . "$dbfile" . '" database.';
print "\n" . ' ' . '='x78 . "\n\n";
print 'Are you sure you wish to do this (y or n)? ';
chomp (my $verify = lc<STDIN>);
print "\n";
die 'Quitting: The "'."$dbfile".'" database was not cleared.'."\n"
unless ($verify eq "y");
dbmopen(my %WORDS, $dbfile,0666)||die "Can't open $dbfile: $!\n";
%WORDS = ();
dbmclose (%WORDS);
die '"' . "$dbfile" . '" has been cleared.' . "\n";
}
sub help
{
print <<ENDTEXT;

USAGE: perl 17_1.plx [file or wildcard] [options]

OPTIONS: -h Help.
-i Ignore case while searching words.
-r Read from the database.
-s Select a database (or create a new one).
-w Write to the database.
-x Clear the database.

NOTES: Default database will be used unless -n is specified.
You will not be able to write to the database unless
files are specified and the -w switch is issued.
ENDTEXT
print "\nPress <Enter> to continue... ";
chomp (my $pause = <STDIN>);
print <<ENDTEXT;
-----------------------------Examples-----------------------------------

perl 17_1.plx file.txt -w Updates the default database with
information obtained from the
specified files.
perl 17_1.plx *.* -n -w -r Creates a new database from the
information contained in the
specified files, and then reads it.
perl 17_1.plx -n -r Selects an alternate database and
reads the contents.
perl 17_1.plx -r -n -c Selects an alternate database and
clears then reads the contents.
perl 17_1.plx -r -? Reads from the default database and
displays this help screen.

ENDTEXT
}

Glenn Jackman 11-20-2003 08:16 PM

Re: How to pause display of long STDOUT
 
Default@IO_Error_1011101.xyz <Default@IO_Error_1011101.xyz> wrote:
> is there a 'more' or 'less' like command i can implement?


Collect your help into a scalar $help, then:
system "echo '$help'|less";

> Also if anyone is familiar with Getopt... is there a way to collect the
> initial file args from this command line w/o using a -<option>?


Just read them from @ARGV after calling GetOptions.

--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca

Eric Schwartz 11-20-2003 08:39 PM

Re: How to pause display of long STDOUT
 
Glenn Jackman <xx087@freenet.carleton.ca> writes:
> Default@IO_Error_1011101.xyz <Default@IO_Error_1011101.xyz> wrote:
>> is there a 'more' or 'less' like command i can implement?

>
> Collect your help into a scalar $help, then:
> system "echo '$help'|less";


Use $ENV{PAGER}, by preference, falling back to less and then more if
$ENV{PAGER} is not set.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.

Default@IO_Error_1011101.xyz 11-20-2003 09:17 PM

Re: How to pause display of long STDOUT
 
hmm im getting strange results.. likely due to the fact that the
print statement is part of a while loop.

wondering if perhaps a for statement could be the solution.
something like this: for ($i = 1; $i <= 40; $i++)
only thing with that is after the for is exhausted i need it to start the
for operation over again untill the while is completly finished.

footnote: im on a w2k $erver.

Sam Huffman 11-20-2003 10:35 PM

Re: How to pause display of long STDOUT
 
Glenn Jackman <xx087@freenet.carleton.ca> writes:

> Default@IO_Error_1011101.xyz <Default@IO_Error_1011101.xyz> wrote:
> > is there a 'more' or 'less' like command i can implement?

>
> Collect your help into a scalar $help, then:
> system "echo '$help'|less";


Or, to avoid the problem of shell-mangling of the help message:

open(HELP, "| /usr/bin/less");

<print your help output to HELP>

close(HELP);


Sam


All times are GMT. The time now is 12:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.