LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Perl] simple print question. (https://www.linuxquestions.org/questions/programming-9/%5Bperl%5D-simple-print-question-562632/)

////// 06-18-2007 06:36 AM

[Perl] simple print question.
 
Hello all.

I have a little problem with print command, like this:
Code:

#!/usr/bin/perl
use strict;
use warnings;
#*************#

my $foo = "Hello world.\n";

print $foo;

It of course prints $foo to the left side of terminal window,
Code:

slax ~ # /Desktop/hello.pl
Hello world.
slax ~ #

but what I want to do is instead of printing $foo to the left side, I want to print it to right side of terminal.
Code:

slax ~ # /Desktop/hello.pl
                                                              Hello world.
slax ~ #

Is there a way to do that (without using tabs or any kind of whitespace characters)?

Cheers ////

jim mcnamara 06-18-2007 06:54 AM

By default, tty drivers print from left to right. You have to pad with whitespace, or use escape characters (ANSI escape sequences) to move things to wierd positions on the screen like that. So, the problem is not related to perl really. What exactly are you trying to do?


For escape sequences see:
http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html

You can use the COLUMNS environment variable to get your screen width.

////// 06-18-2007 07:39 AM

Quote:

Originally Posted by jim mcnamara
What exactly are you trying to do?

Thanks for answer, what I'm doing is a perl script that takes input from iptables ip_queue module (port 80) and then uses HTML::LinkExtor and parses all links from that traffic.

Then it matches those extracted links with different regexes that I have written/borrowed from different intrusion detection systems.

I show few pics, easier that way for me :p
It prints like this now:
http://img340.imageshack.us/my.php?image=ssim2.png

I would like to print like this:
http://img255.imageshack.us/my.php?image=ssmodbf0.png

My problem is that those url's are different length so I cant use any kind of tabs or whitespaces.

Code:

print colored ("$url", $script_color);
print colored ("  Rule:026", $rule_color);
print YELLOW, "    ", get_time(), RESET, "\n";

That get_time() produces that timestamp that I would like to print to the right side of terminal.

/////

jim mcnamara 06-18-2007 11:21 AM

try something that creates a filler padded dynamically with spaces:
Code:

my $cols = 128;
my $url = "xxxxx xx xx  http://somewhere.com";
my $rule = " Rule 029";
my $tm = sprintf("%s", get_time() );

$tmp = cols - length($url) - length($rule) - length($tm) - length("\n");
$filler = sprintf("%*s", $tmp, " ");

print colored ("$url", $script_color);
print colored ("  Rule:026", $rule_color);
print YELLOW  $filler, $tm, RESET, "\n";


////// 06-18-2007 03:00 PM

Hi, I managed to get it to work. Thanks for hints about ansi codes.

Just change the length of that $url string and it still prints timestamps at the right place.
Code:

#!/usr/bin/perl -w
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
#********************#
my $url = "a href http://www.gogggggggggogle.com/";

my $time = "[18/Jun/2007:21:33:30]";

sub counter
{
        for my $char ('.') {
        my $count = () = ($url =~ /$char/g);
        my $pos = (120 - $count);
        return $pos;
        }
}

print $url;
print YELLOW, "\e[", counter($url), "C", $time, "\e[C", RESET, "\n";
#**********************************************************#

That print looks really ugly right now but I'm going to clean it up later :p

Cheers, /////


All times are GMT -5. The time now is 04:26 AM.