summaryrefslogtreecommitdiff
path: root/008/ch2.pl
blob: d9857bfd005106e293b620fdad60fb4b43642d35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env perl

use strict;
use warnings;

use List::Util qw(max reduce);

sub center {
    # Get length of the longest string
    my $maxlen = reduce { max($a, length $b) } 0, @_;

    # Add the needed spaces to the beginning of each string
    return map { ' ' x (($maxlen - length) / 2) . $_ } @_;
}

my @lines;
while (<>) {
    chomp;
    push @lines, $_;
}
for (center(@lines)) {
    print $_, "\n";
}