How can I pad part of a string with spaces, in Perl?

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-03-21T07:58:35Z Indexed on 2010/03/22 15:21 UTC
Read the original article Hit count: 131

Filed under:
|
|

Hello! Which version would you prefer?

#!/usr/bin/env perl
use warnings; 
use strict;
use 5.010;

my $p = 7; # 33
my $prompt = ' : ';
my $key = 'very important text';
my $value = 'Hello, World!';

my $length = length $key . $prompt;
$p -= $length; 

Option 1:

$key = $key . ' ' x $p . $prompt;

Option 2:

if ( $p > 0 ) { 
    $key = $key . ' ' x $p . $prompt;
}
else {
    $key = $key . $prompt;
}

say "$key$value"

© Stack Overflow or respective owner

Related posts about perl

Related posts about print