New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tied HASH as boolean always return false. #6621
Comments
From gm@virtuasites.com.brA tied HASH when used as a boolean always return it as false! Example: my %hash ; tie(%hash , 'TiedHash') ; print "key: $hash{key}\n" ; if ( %hash ) { print "BOOL: 1\n" ;} package TiedHash ; sub TIEHASH { bless({}, __PACKAGE__ ) ;} sub FETCH { return( 'fetch_val' ) ;} But if you set some data before set it as HASH not: my %hash = (1) ; tie(%hash , 'TiedHash') ; if ( %hash ) { print "BOOL: 1\n" ;} package TiedHash ; sub TIEHASH { bless({}, __PACKAGE__ ) ;} I know that some Perl developers already know that. But this isn't What I think that can be done is to add a new TIE function BOOLEAN to catch I will look at Perl source to send some path for the new function. But if Regards, PS: perlbug script never works for Win32! Summary of my perl5 (revision 5 version 8 subversion 0) configuration: Characteristics of this binary (from libperl): |
From @schwernFWIW, 5.8.1 RC2 hash changed the behavior of testing the boolean condition of a tied Can't provide tied hash usage; use keys(%hash) to test if empty at - line 7. until there is some way of returning bucket information from a tied hash (ie. what a |
@schwern - Status changed from 'new' to 'open' |
From gm@virtuasites.com.br
Noooo!!! Now all my scripts that uses tied HASH will die!!! For example, in mod_perl, I use a module that handles the incoming formulary The best thing of %FORM is to can check easy if I have formulary data using: if ( %FORM ) { ... } ... and now this isn't possible, and every thing will die! Note that to enable the boolean check to %FORM, the HASH that is tied has a %hash = (1) ; tie( \%hash , 'tiehandle' ) ;
OK, but I think that kind of change is not good for a fix release, since And create an erro?! At least keep the compatibility with what we have Please, please, remove the error, and keep what exists before! And when we a Other thing. The best thing that should be made without implement the
I will look for a path, maybe before release 5.8.1! Regards, |
From @schwernOn Wed, Jul 16, 2003 at 10:51:21AM -0300, Graciliano M. P. (Virtua Sites) wrote:
I'd tend to agree.
That just changes the behavior from wrong to a different kind of wrong. What could be done is: A) downgrade the error to a warning I prefer B or C. C remains the Right Thing even if we introduce a true -- |
From @mjdominus
It seems to me that this is the wrong approach. I'm not sure what the It might also be a KEYS method which can be called in list context or |
From @schwernOn Wed, Jul 16, 2003 at 05:35:45PM -0400, Mark Jason Dominus wrote:
Trouble is, you need more than just the number of keys to fully emulate the $ perl -wle '%h = (foo => 42); print scalar %h' -- |
From @timjOn Wed, 16 Jul 2003, Michael G Schwern wrote:
Since a tied hash does not have to refer to a real hash couldn't -- |
From @tamiasOn Wed, Jul 16, 2003 at 02:52:06PM -0700, Michael G Schwern wrote:
In fact, for that you don't really need the number of keys at all. :) But precisely emulating the behavior of a hash in scalar context seems a Ronald |
From @schwernOn Wed, Jul 16, 2003 at 12:04:07PM -1000, Tim Jenness wrote:
Given that the scalar value of a hash is rather silly, and having a method -- |
From enache@rdslink.roOn Wed, Jul 16, 2003 at 02:20:03PM -0700, Michael G Schwern wrote:
$ perl -le '{package P; sub TIEHASH {bless{}}} tie my %h, P; print "yeah\n" if scalar %h' why does Perl issue the error only when %h is in the pad ?
Why should perl start doing a lot of crap behind your back (iterate through Regards, |
From enache@rdslink.roOn Wed, Jul 16, 2003 at 05:35:45PM -0400, Mark Jason Dominus wrote:
Does testing if FIRSTKEY returns something define'd sound reasonable Regards, |
From enache@rdslink.roOn Thu, Jul 17, 2003 at 12:51:32AM +0100, Dave Mitchell wrote:
Agreed. Regards, |
From @schwernOn Thu, Jul 17, 2003 at 02:46:10AM +0300, Enache Adrian wrote:
Yes, that'll do for boolean context. Not sure about non-boolean scalar |
From @tamiasOn Thu, Jul 17, 2003 at 02:46:10AM +0300, Enache Adrian wrote:
What effect will that have on the hash iterator? Ronald |
From gm@virtuasites.com.brThe use of HASH as boolean was fixed and the compatibility with the Changes: ( File: pp.c at pp_padhv() ) Behavior: If the method FILL doesn't exists, get the buckets size as the Test Script of Changes: ## To test the previous behavior (when method FILL doesn't exists): my $tieobj = tie(%hash , 'TiedHash') ; package TiedHash ; |
From gm@virtuasites.com.brpp.c.diff109,114c109,130
< SV* sv = sv_newmortal();
< if (SvRMAGICAL(TARG) && mg_find(TARG, PERL_MAGIC_tied))
< Perl_croak(aTHX_ "Can't provide tied hash usage; "
< "use keys(%%hash) to test if empty");
< if (HvFILL((HV*)TARG))
< Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
---
> bool ret_buckets = TRUE ;
> MAGIC *mg;
> if ( SvRMAGICAL(TARG) && (mg = mg_find(TARG, PERL_MAGIC_tied)) ) {
> SV *obj = SvRV(mg->mg_obj);
> GV *gv;
> CV *cv = NULL;
> if ( (gv = gv_fetchmethod_autoload(SvSTASH(obj), "FILL", FALSE)) && isGV(gv) && (cv = GvCV(gv))) {
> ret_buckets = FALSE ;
> PUSHMARK(SP);
> XPUSHs(SvTIED_obj((SV*)gv, mg));
> XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
> PUTBACK;
> ENTER;
> call_sv((SV *)cv, G_SCALAR);
> LEAVE;
> SPAGAIN;
> }
> }
> if (ret_buckets) {
> SV* sv = sv_newmortal();
> if (HvFILL((HV*)TARG))
> Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
116,118c132,135
< else
< sv_setiv(sv, 0);
< SETs(sv);
---
> else
> sv_setiv(sv, 0);
> SETs(sv);
> }
|
gm@virtuasites.com.br - Status changed from 'open' to 'resolved' |
From @iabynOn Thu, Jul 17, 2003 at 02:21:39AM +0300, Enache Adrian wrote:
Why can't a tied hash in a scalar context call FIRSTKEY, and if -- |
From gm@virtuasites.com.br
This is a good point, since it's faster to process, instead fo: (keys But I still think that the best way is to introduce a new function: BOOLEAN. And this is not to about to fully emulate the current behavior of a hash! my $hash_buckets_use = %hash ; We just want to make: if ( %HASH ) { ... } ...and the problem is in the behavior of the HASH as boolean, not to get the But looking in the implementation of this in Perl, at pp.c, I saw that is if (HvFILL((HV*)TARG)) ... The best way is to implement a method that works in the FILL area (if it *** I'm working in a path for this, I just need to know what name to use! Regards, |
From rick.delaney@rogers.comOn Thu, Jul 17, 2003 at 02:46:10AM +0300, Enache Adrian wrote:
That might screw up someone's iterator pointer. Using the example while (($k, $v) = each %h) { -- |
From gm@virtuasites.com.brI have sent the PATH for that (for Perl-5.8.1-RC2)! But this is the 1st time Regards, |
@rgs - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#22973 (status was 'resolved')
Searchable as RT22973$
The text was updated successfully, but these errors were encountered: