Closed p5pRT closed 21 years ago
Checking for hash emptiness with code like C\< if (%x) > fails if %x is tied.
~ $perl -MTie::Hash -wle'tie %x\,"Tie::StdHash"; %x=1..2; print(%x?"ok":"not ok")' not ok
This comes from this code in pp_padhv and pp_rv2hv: ~/bleadperl/perl $fgrep -A2 HvFILL pp*.c pp.c: if (HvFILL((HV*)TARG)) pp.c- Perl_sv_setpvf(aTHX_ sv\, "%ld/%ld"\, pp.c: (long)HvFILL((HV*)TARG)\, (long)HvMAX((HV*)TARG) + 1); pp.c- else pp.c- sv_setiv(sv\, 0); -- pp_hot.c: if (HvFILL(hv)) pp_hot.c- Perl_sv_setpvf(aTHX_ TARG\, "%"IVdf"/%"IVdf\, pp_hot.c: (IV)HvFILL(hv)\, (IV)HvMAX(hv) + 1); pp_hot.c- else pp_hot.c- sv_setiv(TARG\, 0);
where HvFILL doesn't seem to reflect whether the tied hash has any contents. There isn't any way to return valid bucket info if the hash is not empty--perhaps it should return "1/1"? But if the hash is empty it needs to return a false value regardless of HvFILL.
"Ticket #17718 %tiedhash in bool context doesn't check if hash is empty"
http://rt.perl.org/rt2/Ticket/Display.html?id=17718
-- Jarkko Hietaniemi \jhi@​iki\.fi http://www.iki.fi/jhi/ "There is this special biologist word we use for 'stable'. It is 'dead'." -- Jack Cohen
On 5 May 2003 19:17:21 -0000\, perlbug-followup@perl.org wrote:
"Ticket #17718 %tiedhash in bool context doesn't check if hash is empty"
http://rt.perl.org/rt2/Ticket/Display.html?id=17718
Not so low hanging. See the unanswered questions under [perl #18186].
It would be easy enough to call mg_size when formatting the bucket info and make magic_sizepack (currently never used for hashes) call FETCHSIZE only for arrays and BUCKETS for hashes\, but that might get in the way of a future HASHSIZE (or FETCHSIZE??) method for efficient scalar(keys(%tiedhash)). I guess magic_sizepack could could check if the current op was pp_keys or not...
Any comments\, anybody?
On Mon\, 2003-05-05 at 21:06\, Yitzchak Scott-Thoennes wrote:
It would be easy enough to call mg_size when formatting the bucket info and make magic_sizepack (currently never used for hashes) call FETCHSIZE only for arrays and BUCKETS for hashes\, but that might get in the way of a future HASHSIZE (or FETCHSIZE??) method for efficient scalar(keys(%tiedhash)). I guess magic_sizepack could could check if the current op was pp_keys or not...
Any comments\, anybody?
it appears to this chronic suggestor of tie extensions that the whole exercise is a waste of time unless one is willing to implement your extension\, not just to suggest how cool it would be.
That's my comment\, (and I have been up all night at this point.)
-- David Nicol\, independent consultant and contractor have a nice day\, really. http://www.funnytimes.com
On 2 Oct 2002 02:21:30 -0000\, perl5-porters@perl.org wrote:
Checking for hash emptiness with code like C\< if (%x) > fails if %x is tied.
~ $perl -MTie::Hash -wle'tie %x\,"Tie::StdHash"; %x=1..2; print(%x?"ok":"not ok")' not ok
There is no way to check a tied hash for emptiness without affecting the iterator\, so I'm going to suggest that it croak for now (instead of returning bad data) and we can add an experimental BUCKETS (or some such) in the devel branch later.
Note that scalar(%tiedhash) was used erroneously in op/magic.t (though the value was never actually used.)
Thanks\, applied (change #19452). Even though this introduces a new croak\, I might even take it to 5.8.1\, since the croak stops people from using a construct that doesn't do what they think it is doing.
-- Jarkko Hietaniemi \jhi@​iki\.fi http://www.iki.fi/jhi/ "There is this special biologist word we use for 'stable'. It is 'dead'." -- Jack Cohen
@cwest - Status changed from 'new' to 'resolved'
Migrated from rt.perl.org#17718 (status was 'resolved')
Searchable as RT17718$