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

Hi folks, this may seem like a newb question, then it is! How can i print on one line across the screen like a status bar without using any modules?
perl -le '$\="";$cnt=5; print "*" for (0..$cnt);sleep 1;' ******root@xxxx [/opt/mx/bin]
this code prints it all at the very end at once, not one at a time. thank u!

Replies are listed 'Best First'.
Re: print on one line
by kennethk (Abbot) on Jun 28, 2010 at 21:29 UTC
    If I understand your intention, you have not structured your code correctly. You need the sleep function to be inside the for loop as well as the print; that means you cannot use the inverted loop format you have above. Try instead:

    perl -le '$\="";$|=1;$cnt=5; for (0..$cnt){print "*";sleep 1;}'

    Note I have also modified the special variable $| ($OUTPUT_AUTOFLUSH) - this modifies the flush behavior of the file handles. For a discussion of why, see Suffering from Buffering?.

Re: print on one line
by rovf (Priest) on Jun 29, 2010 at 10:11 UTC

    In addition to what kennethk said, I think you don't want the -l switch, since you want to print the asterisk horizontally, not vertically; so I suggest that you replace l by w.

    -- 
    Ronald Fischer <ynnor@mm.st>
      It is true that the -l switch is not needed. Nor is $\="" needed since there is no input. Removing both will still output horizontal asterisks.

      However, -w will not affect the output with respect to horizontal or vertical; it enables warnings. See perlrun.

        However, -w will not affect the output with respect to horizontal or vertical; it enables warnings.
        Exactly, and that's why I recommended it.

        -l is not needed, so I suggested to remove it. -w is recommendet, so I suggested to add it. If you remove l and add w, you can equally say that you replace l by w ;-)

        -- 
        Ronald Fischer <ynnor@mm.st>