Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you delete or change the display on the last print on perl?

Tags:

perl

What I'm trying to do it print out a string and then after a few background checks, change the previously said string?

So here is what it will initially look like:

#
Loading design modules....please wait.

Then after a few background checks it will remove "Loading design modules....please wait." then change it to something like this without clearing the whole CLI Screen:

#
Design module loaded!

This is my code so far:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

use feature 'say';
use feature 'switch';

use IO::Socket;
use Scalar::Util qw(looks_like_number);
use Term::ANSIScreen qw/:cursor :screen/;

$| = 1;
print "Loading design modules....please wait";
sleep(5);
say "Design module loaded!";
like image 324
minimum666 Avatar asked Jan 26 '23 00:01

minimum666


1 Answers

Use savepos() to save the position of the cursor and loadpos() to go back to that saved position.

Here's a first attempt:

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Term::ANSIScreen ':cursor';

$| = 1;

savepos;

print "Loading design modules....please wait";
sleep 5;
loadpos;
say "Design module loaded!";

But as the second string is shorter than the first, we'll end up seeing bits of both string:

Design module loaded!s....please wait

The easiest solution is probably to just print a string of spaces to overwrite the first string before printing the second.

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Term::ANSIScreen ':cursor';

$| = 1;

savepos;

print "Loading design modules....please wait";
sleep 5;
loadpos;
print ' ' x 37;
loadpos;
say "Design module loaded!";
like image 196
Dave Cross Avatar answered Jan 29 '23 07:01

Dave Cross