Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Question about oddities when printing a tab

by walkingthecow (Friar)
on Jan 04, 2013 at 05:26 UTC ( #1011552=perlquestion: print w/ replies, xml ) Need Help??
walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

Here's my code:
my @systems=qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print colored("\t\t[DONE]\n", 'bold green'); }
And here's my output:
Now powering on SERVER1: [DONE] Now powering on SERVER2: [DONE] Now powering on SERVER3: [DONE]
I have tried this without using Term::ANSIColor (i.e., no colored(...)), and I still have this strange issue. Anyone know why the heck the "DONEs" would not be lined up? Anyone know a good method for lining them up?

Comment on Question about oddities when printing a tab
Select or Download Code
Re: Question about oddities when printing a tab
by LanX (Canon) on Jan 04, 2013 at 05:53 UTC
    not reproducible on my system.

    please check if that code produces that output:

    lanx@nc10-ubuntu:~$ perl my @systems=qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print "\t\t[DONE]\n"; } __END__ Now powering on SERVER1: [DONE] Now powering on SERVER2: [DONE] Now powering on SERVER3: [DONE] lanx@nc10-ubuntu:~$

    Otherwise try to isolate the bug, eg dump \@systems and check special vars.

    Cheers Rolf

    UPDATE:

    If you click on the download link of my code you will see that my example has real tabs where yours has a list of blanks. Your not posting the real output.

    UPDATE:

    do your servernames really have the same length?

    lanx@nc10-ubuntu:~$ perl my @systems=qw(SRV1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print "\t\t[DONE]\n"; } __END__ Now powering on SRV1: [DONE] Now powering on SERVER2: [DONE] Now powering on SERVER3: [DONE]

    if not better try using printf or formats

      That is my code exactly, minus shebang and use strict/warnings. I just did that simple bit of code to see if I could reproduce the issue, and it did.

      Actually, just tested your code and output was as expected. I had done it earlier without Term::ANSIColor and they were not aligned, but now they are. Adding Term::ANSIColor back in and the output is out of line again. So it does seem to have something to do with Term::ANSIColor, now I just need to figure out what.

      Thanks for your help!

      My Code:
      #!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; my @systems=qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print color 'bold red'; print " \t\t[DONE]\n"; print color 'reset'; } _END_ Now powering on SERVER1: [DONE] Now powering on SERVER2: [DONE] Now powering on SERVER3: [DONE]

      By the way, I am posting my output by copying/pasting from terminal window. Is that the wrong way to do it?

      UPDATE

      Changed my code as follows and now output is as expected. Sorry for such a stupid question...

      #!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; my @systems=qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print "\t\t".colored("[DONE]", 'bold red')."\n"; }
        > By the way, I am posting my output by copying/pasting from terminal window. Is that the wrong way to do it?

        no thats what I'm doing, and it works on my linux box. but now it's evident that Term::ANSIColor tries to translate tabs to blanks.

        lanx@nc10-ubuntu:~$ perl use strict; use warnings; use Term::ANSIColor; my @systems=qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print "Now powering on $system:"; print color 'bold red'; print " \t\t[DONE]\n"; print color 'reset'; } __END__ Now powering on SERVER1: [DONE] Now powering on SERVER2: [DONE] Now powering on SERVER3: [DONE] ^blanks^

        Seems like in your environment the translation is broken.

        Your solution works now, because you transfered the tabs out of colored().

        Anyway this only works reliably as long as your "server" names all have the same length. Do you really have less than 10? ;-)

        Cheers Rolf

Re: Question about oddities when printing a tab
by 2teez (Priest) on Jan 04, 2013 at 06:22 UTC

    Or you can consider using Perl6::Form instead of format.[a]
    This works for me:

    use warnings; use strict; use Perl6::Form; my @systems = qw(SERVER1 SERVER2 SERVER3); foreach my $system (@systems) { print form "Now powering on : {|||||} {||||||}", $system, "[DONE]"; }
    [a] Though I am with LanX on using printf if your initial script doesn't work like you want. Update

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011552]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-12-28 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls