How to pause display of long STDOUT

Discussion in 'Perl Misc' started by Default@IO_Error_1011101.xyz, Nov 20, 2003.

  1. 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
    }
    Default@IO_Error_1011101.xyz, Nov 20, 2003
    #1
    1. Advertising

  2. 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
    Glenn Jackman, Nov 20, 2003
    #2
    1. Advertising

  3. 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.
    Eric Schwartz, Nov 20, 2003
    #3
  4. 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.
    Default@IO_Error_1011101.xyz, Nov 20, 2003
    #4
  5. Default@IO_Error_1011101.xyz

    Sam Huffman Guest

    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
    Sam Huffman, Nov 20, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    674
    Eric Sosman
    Jul 8, 2003
  2. Replies:
    0
    Views:
    1,166
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,185
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Basilisk96
    Replies:
    3
    Views:
    516
    Steven D'Aprano
    May 13, 2007
  5. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    471
    santosh
    Jul 24, 2007
Loading...

Share This Page