SelectSaver(3pm)

πŸŽ€ SelectSaver β€” save and restore selected file handle πŸŽ€

Bestie, this is a page from the Perl Programmers Reference Guide, section 3pm, and honestly it's a little gem. Like, tiny module, huge save-your-life energy. πŸ’–


✨ SYNOPSIS ✨

Okay so here's the whole vibe, copy it exactly, no cap:

    use SelectSaver;

    {
       my $saver = SelectSaver->new(FILEHANDLE);
       # FILEHANDLE is selected
    }
    # previous handle is selected

    {
       my $saver = SelectSaver->new;
       # new handle may be selected, or not
    }
    # previous handle is selected

See how the previous handle comes back the second the block ends? That's the whole serve. 🫢


πŸ’… DESCRIPTION πŸ’…

A SelectSaver object is literally just a little container holding a reference to the file handle that was selected at the exact moment it was created. It's giving main character memory. πŸ‘‘

So, the new method:

  • If it gets an extra parameter, then that parameter gets selected. Done. Slay.
  • If it doesn't get one, the selected file handle just… remains unchanged. No drama, no touching anything.

And then the iconic part: when a SelectSaver gets destroyed, it re-selects the file handle that was selected when it was created. Like a rubber band snapping right back. πŸͺ€βœ¨ That's the whole point, iykyk.

Lowkey the cleanest way to do scoped select without manually saving and restoring and inevitably forgetting to restore and breaking everything. Not that anyone here has ever done that. πŸ™ˆ

πŸ¦‹ SEE ALSO πŸ¦‹

Nope, nothing listed here β€” this module keeps its circle small. πŸ’€


Sign-off time, bestie: this page is perl v5.42.3, dated 2026-11-03, and it's filed under SelectSaver(3pm) in the Perl Programmers Reference Guide. Go select responsibly. πŸ’œπŸŒΈ

πŸ“¦ where this page comes from