perl5
perl5 copied to clipboard
builtin::trim warns when passed undef
Module: builtin::trim
Description
my $text=builtin::trim(undef); produces a run-time warning.
I think it would be better to silently return undef as for all other inputs (without exterior spaces) this function just returns its input unmodified. As a web developer I frequently have a need for something like builtin::trim when validating form inputs. I would like to be able to check for the presence of certain values without special undef handling code, e.g. if (trim($role) eq "boss").
Steps to Reproduce
perl -w -Mbuiltin=trim -E 'no warnings "experimental"; my $text=trim(undef)'
Produces this warning:
Use of uninitialized value in subroutine entry at -e line 1.
Note that there is no print $text in the example above. Merely passing undef as input is enough to trigger the warning.
Expected behavior No warnings.
The behaviour for undef is not explictly documented in the trim documentation, except that is said to be equivalent to a particular regex replacement.
I note that String::Util::trim, which is said to be a comparable implentation to builtin::true does not generate a warning:
perl -w -MString::Util -E 'my $text=String::Util::trim(undef);'
I was initially confused by the warning message when I saw it in the wild due to the phrase "in subroutine entry".
Perl configuration This is perl 5, version 36, subversion 0 (v5.36.0) built for aarch64-linux