Lasse Makholm wrote:
>I'm curious as to why you think it cargo-culting...?
It is used in the manner of an idiom, but is rarely a sensible choice.
It's intrinsically somewhat wasteful, because it's extracting
rich information that is being immediately thrown away. (As Reini
pointed out, the waste is mild if your strings are always downgraded.)
Where it's used, usually the operand is known to be a defined string,
and the condition being computed is really $x ne "". Writing it in this
more direct form is clearer and avoids the unbounded expense on long
upgraded strings. On other occasions the operand is either undef or a
non-empty string, and the condition being computed is really defined($x),
which is again clearer and more efficient when written directly.
length($x) in truth-value context is only useful (by way of minimising the
number of operators used) where the operand may be undef, empty string,
or non-empty string, *and* undef and empty string are to be treated the
same, differently from non-empty string. This is a pretty rare need.
The disadvantage of unbounded computation time on upgraded strings
means that even then one should think carefully before favouring it over
($x//"") ne "" and the like, which is in any case clearer.
Most people using length($x) in truth-value context are not using it
because of this rare need and a considered appreciation of the pros,
cons, and alternatives. Rather, they've been given this knowledge packet
that this is the way to overcome problems with strings as truth values,
and so they blindly apply it every time. It's not popular on merit,
it's popular by cargo cult.
>Interesting! I did not know about Devel::GoFaster... Is there anything>written anywhere (apart from the code) about what it actually does and>impact it has?
No. The only documentation of the optimisations is the commentary in
the code. As for impact, you should run your own performance tests, on
your real code and data. Don't expect anything spectacular from D:GF:
it'll be fractions of a percentage point.
-zefram