The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


String::Pad - String padding routines


This document describes version 0.021 of String::Pad (from Perl distribution String-Pad), released on 2021-08-01.


 use String::Pad qw(pad);

 my $res;

 # pad a single string
 $res = pad("foo", 5);           # => "foo  "  # default $which is 'right' or 'r'
 $res = pad("foo", 5, "left");   # => "  foo"  # left padding, which means to be 'right-justified'
 $res = pad("foo", 5, "l");      # => "  foo"  # 'l' is same thing as 'left'
 $res = pad("foo", 5, "c");      # => " foo "  # 'center' ('c') padding
 $res = pad("foo", 5, "c", "x"); # => "xfoox"  # pad with a custom character
 $res = pad("foo", 5, "c", "x"); # => "xfoox"  # pad with a custom character

 $res = pad("foobar", 5, 'r', undef, 'truncate'); # => "fooba"

 # pad multiple strings




 $res = pad($text | \@texts, $width [, $which [, $padchar=' ' [, $truncate=0] ] ] ); # => str or arrayref

Return $text padded with $padchar to $width columns. Can accept multiple texts (\@texts); in which case will return a new arrayref of padded texts.

$width can be undef or -1 if you supply multiple texts, in which case the width will be determined from the longest text.

$which is either "r" or "right" for padding on the right (the default if not specified), "l" or "left" for padding on the right, or "c" or "center" or "centre" for left+right padding to center the text. Note that "r" will mean "left justified", while "l" will mean "right justified".

$padchar is whitespace if not specified. It should be string having the width of 1 column.

$truncate is boolean. When set to 1, then text will be truncated when it is longer than $width.


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Other string padding modules: Text::Padding, Text::WideChar::Util (for strings that contain wide Unicode characters), Text::ANSI::Util (for strings that contain ANSI color codes), Text::ANSIWide::Util (for strings that contain both ANSI color codes and wide Unicode characters).

Other alignment modules: Number::Pad (for padding numbers so that the decimal points align).


perlancar <>


This software is copyright (c) 2021, 2018, 2014 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.