File::Inplace(3pm)

πŸ“¦
version 0.20 Β· 5 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸŽ€ File::Inplace πŸŽ€

ok bestie, so this whole page is the manual for File::Inplace(3pm) β€” that's the User Contributed Perl Documentation section, iykyk β€” and it's literally about editing files in place. Let's get into it πŸ’…


✨ NAME ✨

File::Inplace β€” Perl module for in-place editing of files


🌸 SYNOPSIS 🌸

  use File::Inplace;

  my $editor = new File::Inplace(file => "file.txt");
  while (my ($line) = $editor->next_line) {
    $editor->replace_line(reverse $line);
  }
  $editor->commit;

πŸ’– DESCRIPTION πŸ’–

So File::Inplace is a perl module that exists to make the super common task of editing a file in-place way less of a whole thing. It was inspired by the various flavors of perl's -i option, but it's built for editing that's a little more structured and reusable than what command-line perl usually lets you get away with. Periodt.

The big promise here β€” and it's giving main character energy ngl β€” is file integrity. Meaning: either all the changes you made get saved to the file, or none of them do. No half-edited mess left behind 😭

It also hands you a bunch of extra goodies: backup creation, automatic field splitting per-line, automatic chomping/unchomping, and the ability to abort your edits partway through without ever touching the original file. Obsessed.


🌈 CONSTRUCTOR 🌈

File::Inplace gives you exactly one constructor, it takes a bunch of parameters, and one of them is required. That's it, that's the whole vibe.

File::Inplace->new(file => "filename", ...)

  • file β€” the one required parameter, no exceptions bestie. This is the name of the file you're editing. You literally cannot skip this one.
  • suffix β€” the suffix for your backup files. If you don't specify it, no backups get made at all. Your call 🫢
  • chomp β€” if you set this to zero, then automatic chomping just… doesn't happen. Newlines (or more precisely, whatever's in $/) stay sitting in the strings that come back from next_line. And also, the contents of $/ won't get appended when you replace lines.
  • regex β€” if you specify this, then every line gets split by this parameter whenever you use the next_line_split method. If you leave it unspecified, it defaults to \s+.
  • separator β€” the default character used to join each line when replace_line is invoked with a list instead of a single value. Defaults to a single space.

πŸ¦‹ INSTANCE METHODS πŸ¦‹

  • $editor->next_line () β€” in scalar context, it returns the next line of the input file, or undef if there's no line left. In an array context, it returns a single value of the line, or an empty list if there's no line.
  • $editor->replace_line (value) β€” replaces the current line in the output file with the value you gave it. If you pass it a list, then each value gets joined by the separator you specified at construction time.
  • $editor->next_line_split () β€” literally next_line, except it splits based on the regex you specified in the constructor.
  • $editor->has_lines () β€” returns true if the file still has any further lines. Vibe check on whether there's more to read.
  • $editor->all_lines () β€” returns an array of all the lines in the file being edited. The whole thing, yes ma'am.
  • $editor->replace_all_lines (@lines) β€” replaces all remaining lines in the file with the @lines you passed in.
  • $editor->commit () β€” completes the edit operation and saves your changes to the edited file. This is the slay moment ✨
  • $editor->rollback () β€” aborts the edit process. Nope, we're not doing this anymore.
  • $editor->commit_to_backup () β€” saves your edits to the backup file instead of the original file. Sneaky, but legal πŸ’œ

πŸ’œ AUTHOR πŸ’œ

Chip Turner, <chipt@cpan.org>


Copyright (C) 2005 by Chip Turner

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6.0 or, at your option, any later version of Perl 5 you may have available.


and that's the whole page bestie, no cap 🧚

ok so real talk, that footer at the bottom of the original said perl v5.38.2 and it was dated 2024-03-04 β€” so that's the version of perl this page shipped with and the day it got stamped. bye, love u, go edit some files πŸ’–πŸŒΈπŸŽ€

πŸ“¦ where this page comes from