use 5.010; use strict; use warnings; { package Local::File; use Any::Moose; use overload q[""] => \&contents; has filename => ( is => 'ro', isa => 'Str', required => 1, ); sub open { my $self = shift; my $mode = shift // '<'; open my $fh, $mode, $self->filename or confess sprintf( "could not open '%s' in '%s' mode: $!", $self->filename, $mode, ); return $fh; } sub init { my $fh = shift->open('>'); say $fh $_ for qw( howdy partner goodbye Friend saynora adios Amigo ); } sub edit { system($ENV{EDITOR}, shift->filename); } sub errors { my $fh = shift->open; my @errors; while (<$fh>) { next if /^[a-z]/; chomp; push @errors, qq{Line does not begin with lower-case letter, syntax error on line $. ("$_")}; } return @errors; } sub contents { my $fh = shift->open; local $/ = undef; return <$fh>; } } my $file = Local::File::->new(filename => '/home/tai/tmp/my-file.txt'); $file->init; while (my @errors = $file->errors) { say for @errors; say "please press ENTER to edit file"; ; $file->edit; } say "Valid file!"; say $file;