Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: File handles and loops

by tobyink (Abbot)
on Jul 31, 2012 at 09:56 UTC ( #984590=note: print w/replies, xml ) Need Help??

in reply to File handles and loops

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'

Replies are listed 'Best First'.
Re^2: File handles and loops
by dr.jekyllandme (Sexton) on Jul 31, 2012 at 21:37 UTC
    Hi tobyink, Thank you for replying. You, Neighbour, and the other Monks really helped me out. But I wanted to ask you how should I close the filehandle $fh after it has been opened? I've written my code somewhat similar to you and Neighbours but I am not sure if it is correct. Here is a snippet of the problem:
    sub my_close { my( $fh ) = @_; + close($fh) or die "$!\n"; } + sub my_open { + my( $file, $mode ) = @_; + open my $fh, $mode, $file or die "$!\n"; + return $fh; } sub errors { + my( $file, $mode ) = @_; my $fh = my_open($file, $mode); my @errors = (); while( <$fh> ) { chomp; + next if /^[a-z]/; push( @errors, qq(Line $. '$_' does not begin with lowercase l +etter.) ); } my_close($fh); # Safe to close file like this??? + + return @errors; }
    Thank you.

      You rarely need to close lexical file handles. They automatically close once the variable goes out of scope. That is, if the my $fh is defined within a function, then once that function finishes executing, the file handle disappears.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984590]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-05-24 02:44 GMT
Find Nodes?
    Voting Booth?