I think you mingle the validation code and the user interaction code too much. Here's a somewhat cleaner version...

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 lette +r, 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"; <STDIN>; $file->edit; } say "Valid file!"; say $file;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: File handles and loops by tobyink
in thread File handles and loops by dr.jekyllandme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.