in reply to Stringifying undefs

You could play tricks with tie -- I'm not advocating it, but it is a possibility depending upon your purpose.

{ package Undef; sub TIESCALAR{ bless \pop, 'Undef';} sub STORE{ $$_[0] = $_[1]; } sub FETCH{ defined $$_[0] ? $$_[0] : 0 } }; tie $x, 'Undef'; $x = 0; print "'$x'"; '0' $x = ''; print "'$x'"; '' $x = undef; print "'$x'"; '0'

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: Stringifying undefs
by ambrus (Abbot) on Mar 02, 2004 at 15:39 UTC

    That can help, but you're probably have to redefine the builtins undef and return because tieing evrything to Undef by hand is not easier then just writing ||0 everywhere.

    This, however won't help at all if BUU gets the undefs from uninitialized hash or array elements. In that case, you should probably create a tied array/hash that gives 0 for uninitialized elements. (Let me note that you can set what a hash returns instead of an uninitialized element in ruby.)