Format Code Now
/

Perl Formatter and Beautifier

WM-96

Paste Perl and get each line re-indented by how many brackets are open. Only whitespace at the start and end of lines changes, never the code itself.

Runs in your browser. Nothing is uploaded.

Perl code

Re-indented Perl

How to use it

  1. Paste your Perl, or open a .pl or .pm file or drop it on the input box.
  2. Pick an indent of 2, 3, 4 or 8 spaces, or a tab.
  3. Press Re-indent.
  4. Copy the result or download it as a .pl file.

What it changes

Each line is indented by the number of open brackets, with a closing bracket stepping back out. Continuation lines inside parentheses and square brackets get an extra level, and trailing spaces are removed. Blank lines are limited to two in a row. No line is joined, split or reordered, so the tokens in your program are the same before and after.

What it leaves alone

Here-documents, POD blocks, everything after __END__, strings, regular expressions and quote-like operators such as qw and q are not touched inside. Because Perl cannot be fully parsed without running it, an unusual delimiter or a slash that could be a division or the start of a regex can occasionally confuse the bracket count.

When you get an error

If brackets do not balance, the tool stops with the line number instead of guessing. That usually means a real missing or extra bracket, but it can also be a construct the scanner misread. This is a re-indenter, not perltidy: it does not align assignments or wrap long lines.

Error messages and what to do

MessageWhat it meansFix
A bracket opened here is never closed (line 1).A bracket opened on the line shown has no matching closing bracket.Add the missing bracket, or check for quote-like text the scanner misread.
Unexpected } with no matching opening bracket (line 2).A closing bracket appears with nothing open.Remove the extra bracket or add the missing opening one.

Questions people ask

How do I format Perl code online?

Paste your Perl, choose an indent and press Re-indent. Lines are indented by bracket depth and the code is not otherwise changed. Lines are indented by bracket depth, and the tokens in the code are not otherwise changed. Copy the result or download it as a .pl file.

Is this the same as perltidy?

No. Perltidy parses Perl and can align and wrap code. This tool only changes leading whitespace, so it is safer but does less. No. Perltidy parses Perl and can align and wrap code. This tool only changes leading whitespace, so it is safer but does much less than perltidy.

Will it break my here-documents?

No. Here-document bodies and their terminators are left exactly as written, as are POD and anything after __END__. No. Here-document bodies and their terminators are left exactly as written, as are POD blocks and anything after __END__, so their text is never re-indented.

Why does it say a bracket is never closed?

Either a bracket really is missing, or something such as an odd regex delimiter looked like one. Check the line named in the message. Either a bracket really is missing, or something such as an odd regular expression delimiter looked like one. Check the line named in the message first.

Can it change spaces inside lines?

No. Only the whitespace at the start and end of a line changes. Spacing inside a line is exactly as you typed it. No. Only the whitespace at the start and end of a line changes. Spacing inside a line, such as around operators or commas, is exactly as you typed it.

About this tool

This is a re-indenter, not a full Perl parser and not perltidy. It changes only the whitespace at the start of each line, based on how many brackets are open, and trims spaces at line ends. It does not add or move code, and it leaves here-documents, POD, __END__ sections, strings and regular expressions untouched. Perl cannot be parsed without running it, so unusual quoting or a regex that looks like division can confuse the bracket count. If the brackets do not balance, you get an error with a line number instead of a guess.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.