Skip Menu |

This queue is for tickets about the Hash-MoreUtils CPAN distribution.

Report information
The Basics
Id: 57095
Status: resolved
Priority: 0/
Queue: Hash-MoreUtils

People
Owner: Nobody in particular
Requestors: GRAF [...] cpan.org
titi [...] leadkarma.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in:
  • 0.01
  • 0.02
Fixed in: (no value)



"slice" HASHREF[, LIST] Returns a hash containing the (key, value) pair for every key in LIST. ~> perl -MData::Dump=dump -MHash::MoreUtils=slice -E \ 'dump {slice({a =>1, b => 2}, "b")}' { b => 2 } OK. As I understand this for an EMPTY list the result must be an empty HASHREF...?? Check: ~> perl -MData::Dump=dump -MHash::MoreUtils=slice -E \ 'dump {slice({a =>1, b => 2})}' { a => 1, b => 2 } Hmm, is that the intended behaviour?
Yes, this is the intended behaviour. Empty list specified will use "keys %$href" as LIST.2 Since it's not clearly documented, I added some clear words regarding this behaviour for the next release. Thanks for reporting, Jens
Subject: slice has undocumented, and frankly undesirable behavior, for empty lists
Date: Fri, 25 May 2012 11:26:50 -0400
To: bug-Hash-MoreUtils [...] rt.cpan.org
From: "Titi Ala'ilima" <titi [...] leadkarma.com>
I'm trying to use Hash::MoreUtils::slice (v. 0.02) to take a slice of a hash based on an array that can be passed in. But if that list is empty, I'm getting the whole hash, which is the exact opposite of what I want (I would want an empty hash), and it seems extremely counterintuitive for it to behave this way. This is not even described in the documentation anywhere. I notice that v. 0.02 docs have LIST as an optional parameter, but with this behavior it seems like the only reason one would use slice without a list would be to dereference the hashref, which seems pointless. -- Titi Ala'ilima Principal Software Engineer LeadKarma LLC (617) 300 0202 x110
Subject: Re: [rt.cpan.org #77429] AutoReply: slice has undocumented, and frankly undesirable behavior, for empty lists
Date: Fri, 25 May 2012 13:18:01 -0400
To: bug-Hash-MoreUtils [...] rt.cpan.org
From: "Titi Ala'ilima" <titi [...] leadkarma.com>
On Fri, May 25, 2012 at 11:27 AM, Bugs in Hash-MoreUtils via RT < bug-Hash-MoreUtils@rt.cpan.org> wrote: Show quoted text
> I'm trying to use Hash::MoreUtils::slice (v. 0.02) to take a slice of a > hash based on an array that can be passed in. But if that list is empty, > I'm getting the whole hash, which is the exact opposite of what I want (I > would want an empty hash), and it seems extremely counterintuitive for it > to behave this way. This is not even described in the documentation > anywhere. I notice that v. 0.02 docs have LIST as an optional parameter, > but with this behavior it seems like the only reason one would use slice > without a list would be to dereference the hashref, which seems pointless.
Now that I'm making use of slice_def, I understand why one might want to use the keys of the hash as an implicit list for that and other conditional slices, so I suppose it is for calling consistency that this behavior was duplicated on slice, but I think it's a bad idea. Here are a couple ideas for how to manage it: - allow a flag to be passed before the hashref indicating whether to use the keys as the keylist. since it's not a hashref, you can detect its presence and adjust behavior accordingly. my preference would be for the default to be _not_ using the hash keys implicitly, but if backwards compatibility is important that makes things difficult. - pass an extra symbol to the import that indicates how empty lists should be treated. or import a symbol that could be set (locally if need be) to change the behavior. - whatever you do, this definitely needs to be documented! -- Titi Ala'ilima Principal Software Engineer LeadKarma LLC (617) 300 0202 x110
Hi Titi Ala'ilima, I'm happy about your ticket, but really not able to fix it at the moment (restructuring my personal IT landscape and have the servers down). Since it's not mission critical, please accept my apologies but it'll take a while until all is up again. For the moment I tried to understand your reported problem(s) and fully agree that it is a bug which needs to fixed. The documentation is right there - that shouldn't happen. You're follow up confuses me a bit. Invoking slice with an empty list should return nothing. But any obscure extra parameters are nothing what would solve anything. Please provide tests demonstrating your feature requests should work and I'll see what I can do in middle future. Best regards, Jens
Subject: Re: [rt.cpan.org #77429] slice has undocumented, and frankly undesirable behavior, for empty lists
Date: Fri, 25 May 2012 14:03:14 -0400
To: bug-Hash-MoreUtils [...] rt.cpan.org
From: "Titi Ala'ilima" <titi [...] leadkarma.com>
On Fri, May 25, 2012 at 1:38 PM, Jens Rehsack via RT < bug-Hash-MoreUtils@rt.cpan.org> wrote: Show quoted text
> For the moment I tried to understand your reported problem(s) and fully > agree that it is a bug which needs to fixed. The documentation is right > there - that shouldn't happen. You're follow up confuses me a bit. > Invoking slice with an empty list should return nothing. But any obscure > extra parameters are nothing what would solve anything. >
The point I was making in my followup is that for slice_def, slice_exists, and slice_grep, an argument could be made that a usage like this would be desirable: my %hash = slice_def $hashref; rather than doing it explicitly: my %hash = slice_def $hashref, keys %$hashref; I'm guessing this is the reason why the LIST parameter was made optional. However, that breaks cases like: my %hash = slice_def $hashref, @possibly_empty_list; But you could get the convenience by putting a special parameter _before_ the hashref, e.g.: use Hash::MoreUtils qw($ALLKEYS slice_def); my %hash = slice_def $ALLKEYS $hashref; Or you could pass something to the import to set the behavior (globally, not so safe): use Hash::MoreUtils qw(:use_allkeys slice_def); my %hash = slice_def $hashref; Or you could have a package variable dictating the behavior (importable or not, no big deal): local $Hash::MoreUtils::ALLKEYS = 1; my %hash = slice_def $hashref; Got the idea? I think of those three, the last is the most appealing, as it allows global and local control so would provide an easy fix for backward compatibility, and fine-tuned control for whichever behavior is desirable. But you could always beg off and force people to do it explicitly across the board. It's just a pain for people who are already making use of that "feature". -- Titi Ala'ilima Principal Software Engineer LeadKarma LLC (617) 300 0202 x110
No prose, please - just code. Take the distribution as you get it from CPAN, add to the t/ directory a new file named my_wishes.t and start coding your tests into it, testing as if everything implemented and must work. When I'm choosing to implement one of your proposals, I can easily test if it fits your requirements - you documented them by writing the tests. With those tests you could start hacking on your own and check yia "./Build test" whether you broke something previously working and fix that. When you're finished, submit your changes attached as unified diff. Thanks, Jens
Subject: Re: [rt.cpan.org #77429] slice has undocumented, and frankly undesirable behavior, for empty lists
Date: Fri, 25 May 2012 14:21:45 -0400
To: bug-Hash-MoreUtils [...] rt.cpan.org
From: "Titi Ala'ilima" <titi [...] leadkarma.com>
On Fri, May 25, 2012 at 2:10 PM, Jens Rehsack via RT < bug-Hash-MoreUtils@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=77429 > > > No prose, please - just code. >
Sorry, I could do that but I suggested a few different possibilities and am not overly committed to any of them. Should I give you one test for each and you can choose which you would like to implement? Or if you had a preferred direction, I could probably turn out the necessary tests and associated code changes in short order. -- Titi Ala'ilima Principal Software Engineer LeadKarma LLC (617) 300 0202 x110
Different files for the problem which needs to be fixed and another one for the feature proposals. The only preference I have is: do not break existing applications relying one the (correct parts of the) current implementation. So if you need a new kind of slice behavior, probably a new function should be used instead of an optional argument at some place. This finally produces better readable code ... /Jens
Subject: Re: [rt.cpan.org #77429] slice has undocumented, and frankly undesirable behavior, for empty lists
Date: Fri, 25 May 2012 14:35:44 -0400
To: bug-Hash-MoreUtils [...] rt.cpan.org
From: "Titi Ala'ilima" <titi [...] leadkarma.com>
On Fri, May 25, 2012 at 2:29 PM, Jens Rehsack via RT < bug-Hash-MoreUtils@rt.cpan.org> wrote: Show quoted text
> Different files for the problem which needs to be fixed and another one > for the feature proposals. The only preference I have is: do not break > existing applications relying one the (correct parts of the) current > implementation. >
Fair enough. I will define "correct" as no default LIST, and use new functions for the current incorrect, but in some cases desirable, behavior. Any applications relying on a default LIST will break, but can switch off to the new functions. -- Titi Ala'ilima Principal Software Engineer LeadKarma LLC (617) 300 0202 x110
After re-reading entire stuff, I got the conclusion that it's the same issue as Bernhard reported in rt#57095.
I added the documentation how the default behavior is intended. If you really need to empty a hash, please do %h = ();