DevTools Logo

Perl Playground

Perl Playground

Try Perl 5 one-liners, regex substitutions, hashes and virtual files in your browser.

Runs in your browser
Capability note: this is a small client-side subset interpreter, not WebPerl. Core syntax and bundled virtual files work; CPAN modules, XS, sockets, threads and real host files do not. Runs stop at a 2,000-step budget.
Try an example

Perl script

Result

Exit: 0 · 0 ms · 0 steps

Enter a Perl script to run it

Load an example above to see print, regex, hashes and virtual files.

Equivalent CLI command

Single quotes are escaped for POSIX shells. Copy it to try the same snippet in a local Perl 5.

Details

Perl Playground executes a deliberately small subset of Perl 5 locally: string and numeric expressions, print and printf, arrays, hashes, sort, substitution, numeric for loops, if blocks and STDIN or virtual-file reads. Output is split into STDOUT and STDERR with an exit code, step count and duration. A shell-quoted `perl -e` preview makes it easy to move a working one-liner to a terminal. This is not WebPerl: CPAN modules, XS, sockets, threads and real host files are unavailable and are called out in the UI before you run anything. An unbounded loop stops at a 2,000-step budget rather than freezing the tab.

More examples

Print basics

print "Hello, world!\n";
print "line " x 3, "\n";

String literals, interpolation-free repetition with the x operator and comma-separated print arguments.

Regex substitution

$text = "the cat sat on the mat";
$text =~ s/cat/dog/;
print "$text\n";

Assigns a scalar, substitutes the first match and interpolates the result into a printed string.

Virtual file read

open(my $fh, "<", "data.txt");
while (<$fh>) { print; }

Reads the bundled in-memory data.txt file line by line; nothing touches the host filesystem.

    Examples

    Regex lab

    Input
    $text = "cat cat"; $text =~ s/cat/dog/g;
    Output
    dog dog

    Global substitution replaces every match in the scalar.

    Sorted hash

    Input
    for my $k (sort keys %ages) { print "$k=$ages{$k}\n"; }
    Output
    One line per key in lexical order

    Hash keys are sorted, and the interpolated key resolves the corresponding value.

    Fatal script

    Input
    print "before"; die "boom\n";
    Output
    STDOUT: before · STDERR: boom · exit 255

    Output produced before the fatal call is kept and separated from the error.

    About this tool

    Perl Playground runs a documented subset of Perl 5 entirely in your browser. Write a one-liner or a short script, supply optional STDIN lines, and read STDOUT and STDERR separately with the exit code, step count and duration shown under the result. Nothing is uploaded and no interpreter download is needed.

    The supported surface covers the idioms people most often want to check before touching production: string interpolation, numeric and concatenation expressions, print and printf, arrays and push, hashes with sorted keys, substitution with s///g, numeric for loops, if blocks, and line reads from STDIN or from two bundled in-memory files.

    A CLI preview turns the editor buffer into a shell-quoted `perl -e '…'` command. The quoting uses POSIX single-quote escaping, so the preview can be pasted into a terminal without surprises. Examples cover printing, regex substitution, hash sorting, array operations, virtual-file reads and formatting, plus a deliberately fatal script that demonstrates STDERR capture.

    This is intentionally not a full Perl runtime. There is no CPAN, no XS, no sockets, no threads and no real filesystem; an unsupported statement is reported on STDERR while the rest of the script continues. A 2,000-step budget bounds loops so a stray `while (1)` cannot freeze the tab.

    What this tool does not do

    • This is a subset interpreter, not WebPerl or a Perl 5 binary; many language features are unsupported and reported on STDERR.
    • CPAN modules, XS extensions, sockets, threads, signals and shell commands are unavailable.
    • The virtual filesystem contains only two bundled text files and resets with the page.
    • A 2,000-step budget stops loops; very large in-memory output is capped at 1 MB.
    • The CLI preview targets POSIX shells; Windows cmd.exe quoting differs.

    How to use this tool

    1. Write a snippet

      Type Perl into the editor, or click one of the example chips to load a working snippet.

    2. Add STDIN

      If your script reads <STDIN>, put one input line per line in the STDIN field. Empty input means EOF.

    3. Run and inspect

      Results update as you type and when you press Run. Switch between STDOUT and STDERR in the result footer; the exit code and step count are shown above it.

    4. Copy the command

      Use Copy CLI command to get the shell-quoted perl -e equivalent for a local Perl 5 installation.

    5. Reset

      Reset clears the script and STDIN and returns to a fresh interpreter state. There is no shared state between runs.

    Use cases

    One-liner checks

    Verify a regex or formatting one-liner before running it against production data.

    Legacy maintenance

    Confirm Perl scalar, hash and array semantics while maintaining older scripts.

    Teaching

    Demonstrate interpolation, loops and substitution with immediate, separated output.

    Terminal handoff

    Copy the generated perl -e command to reproduce the experiment in a real shell.

    Supported subset

    ConstructExample
    Scalars$name = "Ada"; print "$name";
    Arrays@a = (3,1,2); print join(",", sort @a);
    Hashes%h = (a => 1); print $h{a};
    Regex$s =~ s/cat/dog/g;
    Loopsfor my $i (1..3) { print $i; }
    Filesopen(my $fh, "<", "data.txt");

    Anything outside this table produces an explicit STDERR note rather than a silent wrong answer.

    Common mistakes

    Mistake:Expecting CPAN modules to load.

    Fix:Only the documented subset is supported; use a local Perl for module-dependent code.

    Mistake:Using C-style for loops.

    Fix:Use for my $i (1..N) or a list loop; the step budget handles termination.

    Mistake:Assuming an open() call reaches disk.

    Fix:Only the bundled data.txt and log.txt exist, in memory and reset on reload.

    Mistake:Forgetting that <STDIN> includes the newline.

    Fix:Printed input lines keep their line ending, so an extra newline may appear.

    Mistake:Pasting shell variables into the CLI preview.

    Fix:The command is single-quoted; shell expansion inside it is intentionally disabled.

    Frequently asked questions

    Is this a real Perl 5 interpreter?

    No. It is a small, zero-dependency interpreter for a documented subset of Perl 5: scalars, arrays, hashes, print, sprintf, substitution, numeric loops and line reads. Unsupported statements are reported on STDERR instead of being executed. For full Perl semantics, use a local installation or a WebPerl-based runtime.

    Can I install modules from CPAN?

    No. There is no module loader, no package manager and no network access. `use strict` and `use warnings` are accepted as no-ops; other imports produce a warning. Code that depends on CPAN modules must be run locally.

    Why can my script only read two files?

    The virtual filesystem contains bundled data.txt and log.txt, held in memory for the session. It demonstrates idiomatic open and line-read code without touching your disk. Files written by the host are invisible, and files created in memory reset when the page reloads.

    How are STDOUT and STDERR separated?

    Each run collects the two streams independently. Switch between the STDOUT and STDERR tabs in the result footer; the exit code, duration and step count appear above the result. A `die` produces STDERR text and exit code 255, while output printed before the failure remains in STDOUT.

    What stops an infinite loop?

    Every executed statement consumes one step from a 2,000-step budget (configurable through the library API). When the budget is exceeded the run ends with exit code 124 and a STDERR note. The tab stays responsive because no real Perl VM is blocked.

    What does the CLI command do?

    It renders your editor buffer as `perl -e '…'` with POSIX single-quote escaping, so embedded apostrophes survive. Copy it into a terminal with a local Perl 5 to run the same snippet outside the browser; remember that the virtual files will not exist there.

    Is my code uploaded or stored?

    No. Execution happens in the browser tab. The editor draft is kept in localStorage so a reload does not lose work; there is no server request, telemetry or account. Use Reset to clear the stored draft.

    References & standards