my ($x) = map { s{/X$}{}r } grep { /X$/ } f();
or
my $x = ( grep { /X$/ } f() )[0] =~ s{/X$}{}r;
or
use List::Util qw( first );
my $x = ( first { /X$/ } f() ) =~ s{/X$}{}r;
The first silently sets $x
to undef
if f()
it returns nothing, and the other two are set $x
to an empty string with a warning in this situation. The second avoids unnecessary work. The third avoids even more.
Aside, you asked for the equivalent of the following
my ($x) = map { s{/X$}{}r } grep { m{X$} } f();
, :
my ($x) = map { s{/X$}{}r } grep { m{/X$} } f();
, !
my ($x) = map { my $s=$_; $s =~ s{/X$}{} ? $s : () } f();
use File::Basename qw( fileparse );
my ($x) = map { my ($fn, $dir_qn) = fileparse($_); $fn eq 'X' ? $dir_qn : () } f();