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