develooper Front page | perl.perl5.porters | Postings from February 2019

Re: New defined-match operator

Thread Previous | Thread Next
From:
Hauke D
Date:
February 21, 2019 15:28
Subject:
Re: New defined-match operator
Message ID:
20190221152850.25711.qmail@lists-nntp.develooper.com
Hi,

I find myself writing that pattern a lot as well. Just an idea:

---8<---
use Test::More tests=>6;

my $foo;           ok !(defined($foo) && $foo =~ /bar/);
$foo = "hello";    ok !(defined($foo) && $foo =~ /bar/);
$foo = "bar";      ok  (defined($foo) && $foo =~ /bar/);

use Sub::Infix;
BEGIN { *d = infix { defined $_[0] and $_[0]=~$_[1] } };

$foo = undef;      ok !( $foo /d/ qr/bar/ );
$foo = "hello";    ok !( $foo /d/ qr/bar/ );
$foo = "bar";      ok  ( $foo /d/ qr/bar/ );
--->8---

;-)

Best,
-- Hauke D


On 21.02.19 12:55, David Cantrell wrote:
> We all know how convenient it is to write this:
> 
>    $foo //= 'bar';
> 
> instead of:
> 
>    $foo = 'bar' unless(defined($foo));
> 
> Well, wouldn't it be nice to be able to write this:
> 
>    if($foo //~ /bar/)
> 
> instead of:
> 
>    if(defined($foo) && $foo =~ /bar/)
> 
> Thoughts?
> 

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