Explain Perl code

Paste Perl and Vibe explains what each regular expression captures, whether a sigil means one value, a list or a hash, and what a sort block compares.

slow_pages.pl

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Anthropic to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe25 lines selected

Press Explain it for the sample, or paste your own code first.

What this Perl sample shows

scalars, arrays and hashes
The first character says what a variable holds: $line is one value, @slowest a list and %hits a hash of keys and values. One entry of a hash is a single value, so it takes $: $hits{$path}. The first time a path turns up its entry is empty, and ++ starts it from 0.
regular expressions
m{...} is a match written with braces in place of slashes, which helps when the text has / in it. Each pair of brackets captures one part into $1 to $4: the method, the path, the 3-digit status and the milliseconds. next unless skips a line that doesn't match, so the sample log's health check ok line is ignored.
strict and warnings
use strict makes every variable be declared with my. Without it, a typo such as $hit{...} for $hits{...} quietly made a second hash and the count came out as 0 paths. With it, Perl refused to run and named %hit.
sort with a comparison
sort { ... } keys %hits orders the paths using the block, where $a and $b are the two being compared. <=> compares numbers, and putting $b on the left sorts largest first, here by average time. @slowest[0 .. 2] takes the first three, and last unless defined $path stops early when the log has fewer pages.

Good to know

  • Run with Perl 5.42.2 on a 9-line sample log, the script printed /api/signup at 925 ms average over 2 requests, /pricing at 396 ms and /docs at 92 ms, then 1 server errors for the one 503.
  • <> reads the files named on the command line a line at a time, or standard input when there are none, so cat access.log | perl slow_pages.pl printed the same result.
  • With only two pages in the log, the script printed both and stopped with no warning, because of the defined check.

Watch out: == compares numbers and eq compares text. "GET" == "POST" came out true: Perl turned both words into 0, printed Argument "POST" isn't numeric in numeric eq (==) and carried on. A log saved on Windows has a hidden \r at the end of each line, so ms$ never matched and the script printed only 0 server errors, with no error. Adding $line =~ s/\r$//; as the loop's first line fixed it.

Explain Perl code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).

Install on Mac

curl -fsSL https://unvibe.site/install.sh | bash

Paste into Terminal. Unvibe isn't notarized by Apple yet, so this is the smooth path.

Opening the .dmg the first time
  1. Open the .dmg and drag Unvibe to Applications.
  2. Open it once; macOS says it can't verify it. Click Done.
    macOS dialog: Unvibe Not Opened, with Done and Move to Trash
    Real screen, macOS 15
  3. System Settings › Privacy & Security › Open Anyway. Confirm, then enter your Mac password. The button shows for about an hour after you try to open Unvibe.
    macOS Privacy & Security, Security section: Unvibe was blocked to protect your Mac, with the Open Anyway button
    Real screen, macOS 15

The Terminal line checks the download against the published SHA-256 and Unvibe's self-signed signature before it installs anything. More help with the first open