Pad or truncate a string to a fixed length, $len, . . .

Padding is spaces; for ASCII NUL bytes use the 'a' or 'Z' format instead. 'Z' guarantees at least one NUL, for dealing with C strings.

$_ = pack 'A'.$len, $_;

Replies are listed 'Best First'.
Re: A Procrustean Statement
by ikegami (Patriarch) on Jun 16, 2006 at 05:01 UTC
    The following will pad but not truncate:
    $_ = sprintf '%-*s', $len, $_;
      Pad and truncate:
      $_ = sprintf '%-*.*s', $len, $len, $_;