Format Perl code

Only whitespace is rewritten: sigils, comments, regexes, heredocs and POD come back exactly as written. This is a layout formatter, not Perl::Tidy, and it does not check or repair syntax.

Runs locally in your browser
Copy

How to format Perl code in the browser

Paste a Perl snippet and the tool returns the same program with consistent indentation, one statement per line, a chosen brace style and optional line wrapping. Only the whitespace between tokens is rewritten: sigils, comments, regex literals, heredocs, POD and subscripts are copied through byte for byte, so the output is the same Perl program, just easier to read.

The formatter runs entirely in this browser tab and the code is never uploaded. It is a layout formatter, not Perl::Tidy and not a syntax checker: it does not compile, validate or repair the program, and a broken file comes back broken, only laid out consistently.

  1. Paste the Perl code into the box and press Perl Code Formatting, or Ctrl/⌘+Enter. Every run replaces the previous result.
  2. Choose the indentation (tab or 2, 3, 4, 8 spaces), how many blank lines to keep, the brace style, and whether long lines should wrap at 80 or 120 characters.
  3. Press Copy to take the result, or Clear to empty both panes before the next snippet.
  4. Wrapping only breaks lines after commas, and the continuation is indented one level deeper; pick No wrapping if every statement should stay on one line regardless of length.

What the formatter changes — and what stays the same

What the formatter changes

Formatting is a whitespace rewrite. The sequence of tokens in the output is identical to the input, which is what protects the parts of Perl that are easy to break: the sigils $, @ and %, comments from # to the end of the line, regular expressions such as /^Error: (\d+)/ and substitutions like s/#/\#/g, the =~ and !~ operators, -> dereference chains, hash and array subscripts such as $h{a} and $list[0], quote-like operators (q, qq, qw, qx, qr, m, s, tr, y), heredocs with their bodies, POD blocks and __END__/__DATA__ sections.

Because every token is copied verbatim, nothing can be silently rewritten: string contents, regex patterns and comments come back exactly as written. A quick way to check any result is that the formatted code contains the same characters as the input, only re-spaced.

Options in practice

Indentation applies to blocks opened by braces; choose a tab or 2, 3, 4 or 8 spaces. Blank lines are truncated to the selected limit, from removing them entirely to keeping every one. The brace style controls where an opening brace goes: on the same line as sub, if, while and friends (the usual Perl layout, K&R), on its own line (Allman), or on the same line with else and elsif moved to a new line after the closing brace.

Wrapping never cuts a token in half: when a line exceeds the chosen length, breaks are placed after commas and the continuation is indented exactly one level deeper. One-line map, grep and sort blocks that fit stay inline; everything else is expanded onto its own lines.

What it does not do

The formatter does not run perl -c: it does not check syntax, resolve modules, evaluate BEGIN blocks or report errors. If the input has an unbalanced quote, a missing semicolon or a stray brace, the output keeps that problem — fixing code is a job for a linter or the interpreter, not for a layout pass.

It also does not restructure the program. Statements are not reordered, expressions are not rewritten, and the formatter has no opinion about which of two equivalent idioms is better. Keep a copy of the original and compare the result the way you would compare any formatted file.

Recent tools: