Let's say that I have something like this:
$foo = 'this is a test';
if ($foo) {
$_ = $foo;
s/ a / a bad /g;
}
elsif ($bar) {
$_ = $bar;
s/ a / a good /g;
}
else { $_ = false; }
I know that if it there were no conditions and I only had $foo then I could do this:
($_ = $foo) =~ s/ a / a bad /g;
And if it wasn't for the regexes then I could do this:
$_ = $foo //
$bar //
false;
The question is, is there a way to shorten this to a one liner and eliminate the conditions AND regexes?
I tried these, but both just return true or false:
# Test 1, tried with and without ( )
$_ = ($foo =~ s/ a / a bad /g) //
($bar =~ s/ a / a good /g) //
false;
# Test 2
$_ = $foo ?
$foo =~ s/ a / a bad /g :
$bar =~ s/ a / a good /g;
I know, I know... readability! LOL But this is mainly for my own education.