Perl Weekly Challenge 360.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 360.

Task 1: Text Justifier

Submitted by: Mohammad Sajid Anwar
You are given a string and a width.

Write a script to return the string that centers the text within that width
using asterisks * as padding.

Example 1
Input: $str = "Hi", $width = 5
Output: "*Hi**"

Text length = 2, Width = 5
Need 3 padding characters total
Left padding: 1 star, Right padding: 2 stars

Example 2
Input: $str = "Code", $width = 10
Output: "***Code***"

Text length = 4, Width = 10
Need 6 padding characters total
Left padding: 3 stars, Right padding: 3 stars

Example 3
Input: $str = "Hello", $width = 9
Output: "**Hello**"

Text length = 5, Width = 9
Need 4 padding characters total
Left padding: 2 stars, Right padding: 2 stars

Example 4
Input: $str = "Perl", $width = 4
Output: "Perl"

No padding needed

Example 5
Input: $str = "A", $width = 7
Output: "***A***"

Text length = 1, Width = 7
Need 6 padding characters total
Left padding: 3 stars, Right padding: 3 stars

Example 6
Input: $str = "", $width = 5
Output: "*****"

Text length = 0, Width = 5
Entire output is padding

I find the length of the string, subtract it from the desired width. and divide by two to get the left padding. The right padding is whatever is needed to get to the desired width. I use integer arithmetic to avoid fractional numbers. The result fits a one-liner.

Examples:

perl -Minteger -E '
for my($s,$w)(@ARGV){$r=$w-($l=($w-($t=length($s)))/2)-$t;say"$s,$w -> ","*" x $l,$s, "*"x$r}
' Hi 5 Code 10 Hello 9 Perl 4 A 7 "" 5

Results:

Hi,5 -> *Hi**
Code,10 -> ***Code***
Hello,9 -> **Hello**
Perl,4 -> Perl
A,7 -> ***A***
,5 -> *****

The full code is similar, with a couple of checks.

 1  # Perl weekly challenge 360
 2  # Task 1:  Text Justifier
 3  #
 4  # See https://wlmb.github.io/2026/02/08/PWC360/#task-1-text-justifier
 5  use v5.36;
 6  use feature qw(try);
 7  use Scalar::Util qw(looks_like_number);
 8  use integer;
 9  die <<~"FIN" unless @ARGV && @ARGV%2==0;
10      Usage: $0 S0 W0 S1 W1...
11      to center string Si in a field of width Wi
12      FIN
13  for my($string, $width)(@ARGV){
14      try{
15          my $length=length $string;
16          die "Expected a number: $width" unless looks_like_number $width;
17          die "Width $width is too small for string $string" unless $width>=$length;
18          $string=~s/^\s*(.*)\s*$/$1/; # Remove leading and trailing spaces
19          my $left = ($width- $length)/2;
20          my $right = $width - $left-$length;
21          say "\"$string\", $width -> ", "*" x $left, $string, "*" x $right;
22      }
23      catch($e){warn $e}
24  }

Examples:

./ch-1.pl "Hi" 5 Code 10 Hello 9 Perl 4 A 7 "" 5

Results:

"Hi", 5 -> *Hi*
"Code", 10 -> ***Code***
"Hello", 9 -> **Hello**
"Perl", 4 -> Perl
"A", 7 -> ***A***
"", 5 -> *****

Task 2: Word Sorter

Submitted by: Mohammad Sajid Anwar
You are give a sentence.

Write a script to order words in the given sentence alphabetically
but keeps the words themselves unchanged.

Example 1
Input: $str = "The quick brown fox"
Output: "brown fox quick The"

Example 2
Input: $str = "Hello    World!   How   are you?"
Output: "are Hello How World! you?"

Example 3
Input: $str = "Hello"
Output: "Hello"

Example 4
Input: $str = "Hello, World! How are you?"
Output: "are Hello, How World! you?"

Example 5
Input: $str = "I have 2 apples and 3 bananas!"
Output: "2 3 and apples bananas! have I"

I split the input strings on space, sort the resulting list ignoring case and join it back together. I use sort_by from List::UtilsBy to sort using lowecase words while keeping the case of the list. The result fits a one-liner.

Examples:

perl -MList::UtilsBy=sort_by -E '
for(@ARGV){@x=split " ";say "$_ -> ", join " ", sort_by{lc}@x}
' "The quick brown fox" "Hello    World!   How   are you?" "Hello" \
  "Hello, World! How are you?" "I have 2 apples and 3 bananas!"

Results:

The quick brown fox -> brown fox quick The
Hello    World!   How   are you? -> are Hello How World! you?
Hello -> Hello
Hello, World! How are you? -> are Hello, How World! you?
I have 2 apples and 3 bananas! -> 2 3 and apples bananas! have I

The full code is similar:

 1  # Perl weekly challenge 360
 2  # Task 2:  Word Sorter
 3  #
 4  # See https://wlmb.github.io/2026/02/08/PWC360/#task-2-word-sorter
 5  use v5.36;
 6  use List::UtilsBy qw(sort_by);
 7  die <<~"FIN" unless @ARGV;
 8      Usage: $0 S0 S1...
 9      to sort the words of sentence Sn
10      FIN
11  for(@ARGV){
12      my @words=split " ";
13      say "$_ -> ", join " ", sort_by{lc}@words;
14  }

Example:

./ch-2.pl "The quick brown fox" "Hello    World!   How   are you?" "Hello" \
  "Hello, World! How are you?" "I have 2 apples and 3 bananas!"

Results:

The quick brown fox -> brown fox quick The
Hello    World!   How   are you? -> are Hello How World! you?
Hello -> Hello
Hello, World! How are you? -> are Hello, How World! you?
I have 2 apples and 3 bananas! -> 2 3 and apples bananas! have I

/;

Written on February 8, 2026