I want to create a subroutine like grep {} @
or map {} @
that can handle code and/or boolean input. Somehow the internet doesn't have much info on this.
I tried to create the sub below, but it can't even handle the first test. I get the error Can't locate object method "BoolTest" via package "input" (perhaps you forgot to load "input"?) at C:\path\to\file.pl line 16.
.
How does this think it's an object? Am I not creating BoolTest correctly?
# Example senarios
BoolTest { 'input' =~ /test[ ]string/xi };
BoolTest { $_ =~ /test[ ]string/xi } @array;
BoolTest(TRUE);
# Example subroutine
sub BoolTest
{
if ( ref($_[0]) == 'CODE') {
my $code = \&{shift @_}; # ensure we have something like CODE
if ($code->()) { say 'TRUE'; } else { say 'FALSE'; }
} else {
if ($_[0]) { say 'TRUE'; } else { say 'FALSE'; }
}
}