PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Morning Esteemed Monks!
What I would like is a pure perl solution in splitting the screen as shown in the code below. Top of the screen shows all the information and at the bottom of the screen is where new values can be entered. I would like the top screen to be shown all the time and the bottom screen changes for each input that needs to be changed. In this case the only value that needs changed is Owner. Thanks in advance.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Win32::Console::ANSI; use Win32::Console; use Term::ANSIColor; use Term::ANSIScreen qw/:cursor :screen/; ################### # # TEST ONLY # ################### Title "Edit Car"; open(MYINPUTFILE, "cardata.txt"); # open for input $| = 1; my @lines = <MYINPUTFILE>; # read file into list close (MYINPUTFILE); printf ("|VERIFY CAR OWNER|\n\n"); my $carownerverify = <STDIN>; $carownerverify = <STDIN> until defined $carownerverify; chomp($carownerverify); cls(); printf ("| OWNER | MAKE | MODEL | YEAR |\n\n"); my $found = 0; foreach my $carownerverify2 (@lines) { my @field = split(':',$carownerverify2); if ($field[0] =~ /(?<![\w-])$carownerverify(?![\w-])/i) { my $format = " %-13s %-15s %-11s %0s\n"; printf ($format, $field[0], $field[1], $field[2], $field[3]); $found =1; } } # END FILE LOOP ######################################### # # THE ABOVE STAYS ON TOP OF THE SCREEN # THE BELOW IS WHAT CHANGES # ######################################### print("| OWNER: |\n\n"); my $origOwner = <STDIN>; $origOwner = <STDIN> until defined $origOwner; chomp $origOwner; cls(); print("| NEW OWNER: |\n\n"); my $newOwner = <STDIN>; $newOwner = <STDIN> until defined $newOwner; chomp $newOwner; cls(); my $file = "cardata.txt"; local $^I = ".bak"; local @ARGV = ($file); while (<>) { chomp; my ($Owner,$Make,$Model,$Year) = split(/\:/); if ($Owner eq $origOwner) { print "$Owner:$Make:$Model:$Year:$newOwner\n"; } else { print "$_\n"; } } unlink("$file.bak"); #close $file; print("CAR UPDATED SUCCESSFULLY!!\n\n"); sleep 10;

Replies are listed 'Best First'.
Re: Pure Perl Split Screen
by blindluke (Hermit) on Nov 10, 2014 at 17:05 UTC

    You should take a look at the Term::Cap module. It's in the set of core modules since at least version 5.10, so it should be available in your installation. I'm assuming that by pure Perl you mean "I don't want to install any modules".

    What a module like this gives you is the ability to write to a specific part of the screen. You would now have to define you own subroutines for printing to the upper part, and printing to the lower part, but it's better than the alternative. The alternative is something like writing to the @upper_lines and @lower_lines arrays, and then, with a refresh_screen sub, cleaning the whole screen and writing the contents of both arrays. to the screen, with a horizontal line in between, if you wish.

    - Luke

        Hello GotToBTru,

        Nice screen name and verse below...anyways I installed POE and Term::Visual via cpan and when I try to run the examples I get the following error which can be view at: www.cfnet.org/~files/TERM-VISUAL-ERROR-1.JPG any solutions? Thanks.

      Hello Luke,

      I'm not against using Perl Modules which I have been using alot of Perl Modules. Will Term::Cap work under Win32? I am using Activestate Perl and Win32::Console module. Thanks.

        Cpan Testers says that version 1.16 of the module works with perl 5.20.0 on Win32. Older versions are listed there, too. They don't mention ActiveState explicitly, however.

        Don't know how to easily find this page via CPAN, but on the Module's MetaCPAN page, there is a link "Testers" top left.

      Hello Luke,

      Would it be to much to ask for a simple working example? Thanks

        Not at all. Here's something simple that I had in mind when I suggested Term::Cap. Don't worry about the use v5.14, it's only needed for 'say', and it replaces use strict;. Just use something recent you have available. Unfortunately, I'm not able to test this under Windows, which I suppose is the OS you're working on, but still, it may be of some use. Best luck with your project.

        #!/usr/bin/perl use POSIX; use Term::Cap; use v5.14; # init the term my $termios = new POSIX::Termios; $termios->getattr; my $ospeed = $termios->getospeed; my $tc = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; # require the following capabilities $tc->Trequire(qw/cl cd ce cm co li/); # clear screen, clear to end of line, clear to end of screen sub cl_scr { $tc->Tputs('cl', 1, *STDOUT) } sub cl_line { $tc->Tputs('ce', 1, *STDOUT) } sub cl_end { $tc->Tputs('cd', 1, *STDOUT) } # move the cursor sub gotoxy { my($x, $y) = @_; $tc->Tgoto('cm', $x, $y, *STDOUT); } sub pline { my ($line_no, $string) = @_; gotoxy(0,$line_no); cl_line; print $string; } my @log = map { " > " } (0..9); # redraw the log sub flush_log { my $i = 0; for my $logline (@log) { pline ($i, $logline); $i++; } pline ($i, "=================================================="); gotoxy(0,++$i); cl_end; } # print line to log sub plog { my $line = shift; shift @log; push @log, " > $line"; flush_log; } my $correct = 42; my $number; my $choice; flush_log; do { print "What is the correct number: "; chomp($number = <STDIN>); if ($number == $correct) { plog "Game result: WIN"; say "Good answer. Play again? (y/n)"; chomp($choice = <STDIN>); } else { plog "Game result: LOSE"; say "Bad answer. Play again? (y/n)"; chomp($choice = <STDIN>); } } while ($choice eq 'y');

        Relevant info from termcap manual:

        `cl' String of commands to clear the entire screen and position the cur +sor at the upper left corner. `cd' String of commands to clear the line the cursor is on, and all the lines below it, down to the bottom of the screen. This command string should be used only with the cursor in column +zero. `ce' String of commands to clear from the cursor to the end of the curr +ent line. `cm' String of commands to position the cursor at line l, column c. Both parameters are origin-zero, and are defined relative to the s +creen, not relative to display memory. `co' Numeric value, the width of the screen in character positions. `li' Numeric value, the height of the screen in lines.

        What it should look like:

        > > > > > > > Game result: LOSE > Game result: WIN > Game result: LOSE > Game result: LOSE ================================================== Bad answer. Play again? (y/n) y What is the correct number:

        - Luke

Re: Pure Perl Split Screen
by fishmonger (Chaplain) on Nov 10, 2014 at 17:38 UTC

      Hello fishmonger,

      Actually we have Perl/Tk running in the mechanics shop as we have the program hooked into the state inspection and emissions system and works nicely....the snippet of code provided is from a larger program that is interconnected to a finance program used by the office staff.

Re: Pure Perl Split Screen
by Discipulus (Canon) on Nov 11, 2014 at 08:28 UTC
    Hello PilotinControl

    your code is a little bit confused: there are many modules you do not use, you are not printing to the file, etc..
    What i can suggest you is a total rewrite... ;=)
    As first step in the program backup your data file with some time info in the name, then, after have read it in memory iterate over records. You also have to use some subroutine to cut away the code repetition of: clear the screen, prompt for input, update the value cycle.

    Note also that dealing with the screen in cmd.exe is a bit uncomfortable: if you want some text to be near the bottom you have to know the size of the actual screen and print out a sort of template with empty lines. in any case the input is the last line.

    More: you may need to verify the succesfull open of file for writing, the data entered by user (you have constraints on such data? can someone input the same data? can be blank?).

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hello Discripulus,

      I've updated the code to reflect what is really needed and removed the modules really not needed for this instance of the code as the code is part of a larger program that uses the above mentioned modules.

      What I've tried to show is that the Upper screen stays the same through out and the lower screen is what changes...in this case changing the Owner of the Car..Typing in Current Owner and then typing in New Owner and once enter is pressed it updates the record and then the top screen would show the new owner? So each function would open, append, and close the file and the last screen would open the file displaying the change made to the file. Thanks for the help.