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

Re: improving $! usability

Thread Previous | Thread Next
From:
Dr.Ruud
Date:
June 21, 2009 14:54
Subject:
Re: improving $! usability
Message ID:
20090621213753.2036.qmail@lists.develooper.com
H.Merijn Brand wrote:
> Michael G Schwern:

>> 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?

Welcome to the wonderful world of objects.

-- 
Ruud


perl -Mstrict -MData::Dumper -wle'

   my $value;  # undef
   my $obj = bless \$value, "Foo";

   print Dumper $obj;
   $obj->show;

package Foo;

   use overload bool  => \&_val;
   use overload q{""} => \&_val;
   use overload q{0+} => \&_val;

   sub show {
     print "<", $_ // "-undef-", ">" for @_;
   }

   sub _val {
     ${ $_[0] };
   }
'
$VAR1 = bless( do{\(my $o = undef)}, 'Foo' );

<-undef->

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