develooper Front page | perl.perl5.porters | Postings from September 2016

Re: pounding the smartmatch drum

Thread Previous
From:
Paul "LeoNerd" Evans
Date:
September 6, 2016 18:09
Subject:
Re: pounding the smartmatch drum
Message ID:
20160906190937.338ca2b0@shy.leonerd.org.uk
On Tue, 6 Sep 2016 13:39:31 -0400
Eric Brine <ikegami@adaelis.com> wrote:

> On Tue, Sep 6, 2016 at 10:51 AM, Paul "LeoNerd" Evans <
> leonerd@leonerd.org.uk> wrote:  
> 
> > Nicer would be if perl had notations for "try to call this method
> > but if it doesn't exist, yield some sentinel falsey value like
> > undef or empty list".  
> 
> 
> sub foo { my $invocant = shift; my $method =
> $invocant->can($method_name); defined($method) ?
> $invocant->$method(@_) : () }
> 
> foo( $o, method_name => ( ... ) )

Oh sure, there's ways to do that. My personal favourite is

 sub possibly { my ($method) = @_;
                \sub { my $inv = shift;
                       $inv->$method(@_) if $inv->can($method) } }


 $obj->${possibly "jump"}( $how_high );

But I wouldn't call that a nice native notation :)

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous


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