Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to pause display of long STDOUT

Reply
Thread Tools

How to pause display of long STDOUT

 
 
Default@IO_Error_1011101.xyz
Guest
Posts: n/a
 
      11-20-2003
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
}
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      11-20-2003
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

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      11-20-2003
Glenn Jackman <> 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.
 
Reply With Quote
 
Default@IO_Error_1011101.xyz
Guest
Posts: n/a
 
      11-20-2003
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.
 
Reply With Quote
 
Sam Huffman
Guest
Posts: n/a
 
      11-20-2003
Glenn Jackman <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
How to cleanly pause/stop a long running function? Basilisk96 Python 3 05-13-2007 03:45 AM
reason for sporadic long full GC pause times?!?.... m.ahaus@usu.de Java 0 07-28-2006 08:48 AM
Do DVDs wear out if left on PAUSE too long? bigfatmama@375lbs.com DVD Video 21 02-10-2005 03:52 AM



Advertisments