develooper Front page | perl.perl5.porters | Postings from June 2009

Re: improving $! usability

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
June 25, 2009 01:15
Subject:
Re: improving $! usability
Message ID:
20090625101526.2d1baa6f@pc09.procura.nl
On Thu, 25 Jun 2009 03:35:34 -0400, Michael G Schwern
<schwern@pobox.com> wrote:

> H.Merijn Brand wrote:
> > On Sun, 21 Jun 2009 07:22:45 -0400, Michael G Schwern
> > <schwern@pobox.com> wrote:
> >> What I was thinking was something like:
> >>
> >>     open my $fh, "<", $file or die $fh->error;
> > 
> > this is *very* counter-intuitive.
> > After a fail, I expect $fh to be false. How do I call a method on an
> > undefined value?
> 
> false != undefined.  $fh can be false and yet still be a valid object.  It
> represents a filehandle which failed to open.  I don't think the open () docs
> say they will leave $fh untouched on failure.
> 
> Here's an expansion of what open () is conceptually doing:
> 
> 	my $fh = IO::File->new;
> 	$fh->open ("<", $file) or die $fh->error;
> 
> After all, its not open's return value that we're checking.  Its making an
> IO::Handle object.  The reason it seems so odd is we don't think of
> filehandles as objects, but they are.

Blame my history, indeed I don't think of them as objects, but as fiel
descriptors. Maybe my life was littered with C too much.

> And its the most logical place to attach the error to.  Its an error about
> failing to open that filehandle.  Otherwise you need an unassociated global
> which is what got us into this mess in the first place.
> 
> >>     while (<$fh>) {
> >>         print;
> >>         }
> >>     die $fh->error if $fh->error;
> > 
> > Here, I like it

-- 
H.Merijn Brand  http://tux.nl      Perl Monger  http://amsterdam.pm.org/
using & porting perl 5.6.2, 5.8.x, 5.10.x, 5.11.x on HP-UX 10.20, 11.00,
11.11, 11.23, and 11.31, OpenSuSE 10.3, 11.0, and 11.1, AIX 5.2 and 5.3.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About