PerlIO::scalar(3pm)

πŸ’œ PerlIO::scalar β€” in-memory IO, scalar IO

okay bestie, this is a page from the Perl Programmers Reference Guide and it's lowkey one of the cutest tricks in Perl: doing file stuff without an actual file. no disk, no paths, just vibes and a variable. let's get into it 🫢


✨ SYNOPSIS ✨

you can open a filehandle on a scalar variable, either by just handing open a reference to it:

my $scalar = '';
...
open my $fh, "<",  \$scalar or die;
open my $fh, ">",  \$scalar or die;
open my $fh, ">>", \$scalar or die;

or, because you're an icon who likes to be explicit, with the :scalar layer spelled out:

my $scalar = '';
...
open my $fh, "<:scalar",  \$scalar or die;
open my $fh, ">:scalar",  \$scalar or die;
open my $fh, ">>:scalar", \$scalar or die;

both ways slay, it's giving same energy πŸ’…


🌸 DESCRIPTION 🌸

so here's the tea: a filehandle gets opened, right, but the file operations happen "in-memory" β€” on a scalar variable instead of an actual file. no cap. πŸ’–

all the normal file operations can be performed on that handle, so you can read/write/seek/whatever like it's any other filehandle. the scalar itself is treated as a stream of bytes.

one lil exception though: currently fileno($fh) returns -1. so like, don't ask that handle for its file descriptor number, because it doesn't have one, it's literally a string 🫠

🚨 also important: attempting to open a read-only scalar for writing will fail β€” and if warnings are enabled, it'll produce a warning too. so don't try to write into a read-only thing and expect it to work, because it absolutely will not 😭

🧚 IMPLEMENTATION NOTE 🧚

"PerlIO::scalar" only exists as a stub. that's it, that's the whole personality πŸ’€ you do not need to explicitly "use PerlIO::scalar" β€” it's just there, doing nothing, serving looks. so don't bother, bestie, no cap ✨


that's the whole page, bestie β€” short and iconic, as it should be πŸ‘‘

this lil masterpiece comes to you from perl v5.42.3, dated 2026-11-03, and it's part of the Perl Programmers Reference Guide. mwah, go write to a string instead of a file, periodt πŸŽ€πŸ’œ

πŸ“¦ where this page comes from