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
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms