#!/usr/bin/perl #----------------------------------------# # Color Text: Perl Cookbook, pg 522 # #----------------------------------------# use Term::ANSIColor; # to upcase the read/write input, I use uc which needs locale use locale; #----------------------------------------# # Clear Screen: Perl Cookbook, pg 521 # #----------------------------------------# use Term::Cap ; $OSPEED=9600; eval { require POSIX; my $termios=POSIX::Termios->new(); $termios->getattr; $OSPEED= $termios->getospeed; }; $terminal = Term::Cap->Tgetent({OSPEED=>$OSPEED}); $terminal->Tputs('cl',1,STDOUT); $clear=$terminal->Tputs('cl'); $clear= `clear`; print $clear; #------------------------------------------# # Main loop: # #------------------------------------------# print color("red"), "###############################################################",color("reset"),"\n", color("white"), "Presenting: ",color("reset"),"\n", color("white"), " Exersize 4.12 Problem 1 ",color("reset"),"\n", color("red"), "###############################################################",color("reset"),"\n", color("white"), "I will ask if you want to \"read\" or \"write\" data. ",color("reset"),"\n", color("white"), "Then ask for a file to read or write to. ",color("reset"),"\n", color("white"), "For writing, I\'ll prompt for input, or 'quit' to exit. ",color("reset"),"\n", color("white"), "If you chose read, I\'ll dump the file\'s contents to the screen. ",color("reset"),"\n"; print color("blue"), "Do you want to ",color("clear"),"READ or WRITE",color("blue")," a file? \t",color("clear"); $Operation=<>;chomp $Operation; $Operation=uc($Operation); if ( "\U$Operation" eq "WRITE" ){ print color("green"),"What is the ",color("clear"),"NAME",color("green")," of the output file?\t",color("clear"); $File=; chomp ($File); open(OUT,">$File") || die color("red"),"Can't open $File: $!",color("clear"); # This variable will contain the keyboard input string my $data =""; # # Input from the user once before entering the Until loop. # If the first input was QUIT, it will not go into the data file. # Once theydo type in quit. # print color("magenta"),"Enter a string, type ",color("clear"),"QUIT",color("magenta")," if you are done:\t",color("clear"); $data=; chomp ($data); # # Perl Cookbook, recipe 1.9 (requires module: locale) # uppercase all the input data, so we can deal with # quit or QUIT until ( "\U$data" eq "QUIT" ){ print color("magenta"),"Enter a string, type ",color("clear"),"QUIT",color("magenta")," if you are done:\t",color("clear"); print OUT "$data\n" ; $data=; chomp ($data); } close (OUT)|| die "Can't close $File: $!"; print color("yellow"),"Finished entering data. Data written to \"",$File,"\".\n",color("clear"); } elsif ( "\U$Operation" eq "READ" ){ print color("green"),"What is the ",color("clear"),"NAME",color("green")," of the input file?\t",color("clear"); $File=; chomp ($File); open(IN,"<$File") || die color("red"),"Can't open $File: $!",color("reset") ; print "This file contains: \n"; while (){ print ; } # # You gota close those file descriptors. # close (IN) || die "Can't close $File: $!"; } else { die "I need you to answer ",color("clear"),"READ or WRITE",color("blue"),", and you typed $Operation.\nError: $!\n",color("clear"); } ############################################################### Presenting: Exersize 4.12 Problem 1 ############################################################### I will ask if you want to "read" or "write" data. Then ask for a file to read or write to. For writing, I'll prompt for input, or 'quit' to exit. If you chose read, I'll dump the file's contents to the screen. Do you want to READ or WRITE a file? write What is the NAME of the output file? test.txt Enter a string, type QUIT if you are done: this is a test Enter a string, type QUIT if you are done: this is only a test Enter a string, type QUIT if you are done: if this where not a test Enter a string, type QUIT if you are done: you would know it Enter a string, type QUIT if you are done: quit Finished entering data. Data written to "test.txt". ############################################################### Presenting: Exersize 4.12 Problem 1 ############################################################### I will ask if you want to "read" or "write" data. Then ask for a file to read or write to. For writing, I'll prompt for input, or 'quit' to exit. If you chose read, I'll dump the file's contents to the screen. Do you want to READ or WRITE a file? read What is the NAME of the input file? test.txt This file contains: this is a test this is only a test if this where not a test you would know it