On Sun, Nov 14, 2004 at 09:21:17AM -0500, Robert Rothenberg wrote:
>
> Reference: some code for testing if an argument is string-like:
>
> sub is_string_like {
>
> return 1,
why the comma?
> unless (defined $_[0] && ref $_[0]);
>
> # We don't evaluate whether the "." and ".=" operators are
> # supported, since for many applications that use strings, the
> # comparison operators are the most important.
>
> eval {
> ("$_[0]")
> && (($_[0] cmp $_[0])==0)
> && ($_[0] eq $_[0])
> && (!($_[0] ne $_[0]))
> && (!($_[0] le $_[0]))
> && (!($_[0] ge $_[0]))
> && (!($_[0] lt $_[0]))
> && (!($_[0] gt $_[0]))
> } and return 1;
>
> # Testing for behavior related to copy constructors is another issue
> # to be determined....
This all seems overkill. Isn't something like this (untested) enough?:
sub is_string_like {
return 1 unless ref $_[0]; # returns 1 for undef
return UNIVERSAL::can($_[0], '""');
}
Tim.