Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Controlling already printed text to console.

by humble (Acolyte)
on Aug 02, 2012 at 06:30 UTC ( #984951=perlquestion: print w/replies, xml ) Need Help??
humble has asked for the wisdom of the Perl Monks concerning the following question:


How do I change some char.s of the already printed text string? I.e.having printed

> Hello, World!

I change same string, being at the same place/location (NOT printing another one after it) to

[] Hello, World!

  • Comment on Controlling already printed text to console.

Replies are listed 'Best First'.
Re: Controlling already printed text to console.
by frozenwithjoy (Priest) on Aug 02, 2012 at 06:47 UTC
    Is this type of output what you are talking about?
    #!/usr/bin/env perl use strict; use warnings; $| = 1; my $i=0; while ( $i < 10 ) { print "$i\r"; $i++; sleep 1; } while ( $i >= 0 ) { print " " x 80 . "\r"; print "$i\r"; $i--; sleep 1; } print "\n";

    This script counts up from 0 to 10 (and back to 0) with each number replacing the previous. It does this by using the carriage return character (\r) instead of the new line character (\n), which is essentially a carriage return followed by a line-feed. The $| = 1 makes the output autoflush. One thing to keep in mind is that printing a string that is shorter than the previous will result in artifacts of the previous longer string. You can get around this by printing several spaces with something like: print " " x 80 . "\r";

    note: If you want to keep the final output, be sure to print a \n when you are all done or it will get over-written by the next (unrelated) output.

      Thank You, frozenwithjoy, VERY MUCH! - For the example and its explanation!

      I've got the point and now will work it out for my case - I have to change the printed string, namely one single char. at its start - like a textual icon for visualisation of a status.

Re: Controlling already printed text to console.
by Athanasius (Chancellor) on Aug 02, 2012 at 07:32 UTC

    The technique described by frozenwithjoy will work provided you haven’t moved the cursor to a new line. In addition to \r (carriage return), which moves the cursor to the start of the line, there is also \b (backspace), which moves the cursor back a single character.

    For anything more complicated, you probably need a CPAN module, and you will likely have to get a module suitable for your OS:

    • For Windows there is Win32::Console.
    • For *nix you might investigate Curses (which won’t install on my Windows system).


    Athanasius <°(((><contra mundum

      Oh, thnks for the "\b" addition - it is useful for my case.

      I do not need more complicated stuff. What have been said, enough for me!

      And it is Linux OS.

Re: Controlling already printed text to console.
by Marshall (Abbot) on Aug 02, 2012 at 07:23 UTC
    How do I change some char.s of the already printed text string? I.e.having printed

    This is really hard for me to understand.
    Once you "print" something - it is gone, passed to some other level of software.

    Adding something before your print is fine.

    my $hello ="Hello, World\n"; print "[]$hello";
    I am not sure exactly what you are trying to do.
    Maybe this is GUI? I don't know.

    If you are talking about a command line window, there are various ways to do this - maybe just move the cursor back to the line to be modified, delete that line and then "re-print" that line. This will happen so fast that the user will not see it.

      Thank You, Marshall.

      Yes I wanted to reprint the string that have been printed.

      Please look at the example given by frozenwithjoy.

Re: Controlling already printed text to console.
by flexvault (Monsignor) on Aug 02, 2012 at 16:08 UTC


    If your working on a *nix system, you could use the 'tput' command. There may be something similar in Windows but someone else will have to answer that. Use 'tput' as below (untested):

    my $rows = qx/tput lines/; my $curor_up = qx/tput cuu1/; for my $no ( 0..10 ) { print "$no. Hello World!\n$curor_up"; }

    I use this technique to show status of a long running script, but I use 'time' to print the status every so many seconds. I did't use '$rows', but that tells you how many lines are on the screen. So you could move the cursor to the bottom -2 and display the results at the bottom of the screen. Also 'tput' is a system commands, so try to use it outside of loops for performance reasons.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

      Oh, thank You, flexvault.

      The script in question - works on Linux, but I prefer using PERL tools|solutions in my PERL scripting - I have had a lot of BASH scripting already inserted into my PERL code, so I would like rather to remove it w/ PERL code, than adding more OS tools.

Re: Controlling already printed text to console.
by ww (Archbishop) on Aug 02, 2012 at 15:37 UTC

    Fundamentally, nonsense. Once you've sent the electrons to the console and its painted those electron-groups (i.e., the chars) on the screen, you MUST print your revised version, afresh.


    The sun came up this morning.
    I need another couple hours sleep.
    How do I make it last night, so the sun won't have come up yet?

    If you find a workable answer, please post. I have an idea about buying stocks at last week's prices.

      As for my question, I've got the answer - please look at frozenwithjoy's respond.

      As for Your stock buying - hmm, probably You have to find another frozenwithjoy - working in stock sphere :o)

Re: Controlling already printed text to console.
by Anonymous Monk on Aug 03, 2012 at 04:39 UTC


    There is also Win32::Console.


      OK. Thank You, J.C.

Re: Controlling already printed text to console.
by humble (Acolyte) on Aug 03, 2012 at 19:18 UTC
    Well let's finish here, as the goal is reached.

    Thank You every one, participated. Have a good time!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984951]
Approved by moritz
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-03-25 03:58 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (300 votes). Check out past polls.