Hash-MultiValue-0.15/000755 000765 000024 00000000000 12152673276 015364 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/Changes000644 000765 000024 00000003210 12152673276 016653 0ustar00miyagawastaff000000 000000 Revision history for Perl extension Hash::MultiValue 0.15 2013-06-03 01:45:46 JST - use MakeMaker 0.14 2013-05-12 16:18:14 PDT - Use milla 0.13 Fri Oct 19 12:41:45 PDT 2012 - repackage with Module::Install > 1.04 0.12 Wed Feb 15 07:45:50 CET 2012 - Reapply set method optimisation that was lost in 0.11 - *really* fix uninitialized warnings in tests (RT#74096) - Add support for Storable serialization 0.11 Sun Feb 12 13:04:54 PST 2012 - Fix segfaulting splice invocation on perls < 5.8.7 - Fix uninitialized warning on older perls 0.10 Sun Sep 18 12:51:49 PDT 2011 - Implemented set (aristotle) 0.09 Wed Jun 15 15:22:12 PDT 2011 - Implemented thread safety (chansen) 0.08 Thu Feb 11 10:14:02 PST 2010 - Tries to import UNIVERSAL::ref if it's loaded. No need to monkeypatch 0.07 Sat Feb 6 15:17:10 PST 2010 - No code change. Fixed the packaging 0.06 Sat Jan 30 14:17:03 PST 2010 - Changed the interface of ->keys() so it now returns the duplicate key as well in the original order. This is more useful and compatible to what MultiDict.py does. - Added ->each and ->values (confound) 0.05 Thu Jan 28 19:27:45 PST 2010 - Added ->mixed and ->multi as aliases for as_hashref_mixed and as_hashref_multi. 0.04 Sat Dec 19 10:55:31 PST 2009 - Some internal rewrites and merge methods (aristotle) 0.03 Thu Dec 17 09:57:50 PST 2009 - Added ->from_mixed constructor 0.02 Wed Dec 16 14:27:01 PST 2009 - Silenced warnings for ref() 0.01 Tue Dec 15 00:35:54 2009 - original version Hash-MultiValue-0.15/cpanfile000644 000765 000024 00000000115 12152673276 017065 0ustar00miyagawastaff000000 000000 requires 'perl', '5.008001'; on test => sub { requires 'Test::More'; }; Hash-MultiValue-0.15/dist.ini000644 000765 000024 00000000037 12152673276 017030 0ustar00miyagawastaff000000 000000 [@Milla] installer = MakeMaker Hash-MultiValue-0.15/lib/000755 000765 000024 00000000000 12152673276 016132 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/LICENSE000644 000765 000024 00000044005 12152673276 016374 0ustar00miyagawastaff000000 000000 This software is copyright (c) 2013 by Tatsuhiko Miyagawa . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2013 by Tatsuhiko Miyagawa . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2013 by Tatsuhiko Miyagawa . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Hash-MultiValue-0.15/Makefile.PL000644 000765 000024 00000002550 12152673276 017340 0ustar00miyagawastaff000000 000000 use strict; use warnings; use 5.008001; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Store multiple values per key", "AUTHOR" => "Tatsuhiko Miyagawa ", "BUILD_REQUIRES" => {}, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Hash-MultiValue", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Hash::MultiValue", "PREREQ_PM" => {}, "TEST_REQUIRES" => { "Test::More" => 0 }, "VERSION" => "0.15", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { my $tr = delete $WriteMakefileArgs{TEST_REQUIRES}; my $br = $WriteMakefileArgs{BUILD_REQUIRES}; for my $mod ( keys %$tr ) { if ( exists $br->{$mod} ) { $br->{$mod} = $tr->{$mod} if $tr->{$mod} > $br->{$mod}; } else { $br->{$mod} = $tr->{$mod}; } } } unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Hash-MultiValue-0.15/MANIFEST000644 000765 000024 00000000453 12152673276 016517 0ustar00miyagawastaff000000 000000 Changes LICENSE MANIFEST META.json META.yml Makefile.PL README cpanfile dist.ini lib/Hash/MultiValue.pm t/00_compile.t t/from_mixed.t t/hash.t t/multi.t t/ref.t t/release-pod-syntax.t t/set.t t/storable.t t/threads.t t/write.t tools/benchmark.pl xt/perlcritic.t xt/pod.t xt/podspell.t xt/synopsis.t Hash-MultiValue-0.15/META.json000644 000765 000024 00000003257 12152673276 017014 0ustar00miyagawastaff000000 000000 { "abstract" : "Store multiple values per key", "author" : [ "Tatsuhiko Miyagawa " ], "dynamic_config" : 0, "generated_by" : "Dist::Milla version v1.0.2, Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.131490", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Hash-MultiValue", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.30" } }, "develop" : { "requires" : { "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "perl" : "5.008001" } }, "test" : { "requires" : { "Test::More" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/miyagawa/Hash-MultiValue/issues" }, "homepage" : "https://github.com/miyagawa/Hash-MultiValue", "repository" : { "type" : "git", "url" : "https://github.com/miyagawa/Hash-MultiValue.git", "web" : "https://github.com/miyagawa/Hash-MultiValue" } }, "version" : "0.15", "x_contributors" : [ "Aristotle Pagaltzis ", "Hans Dieter Pearcey ", "Peter Rabbitson ", "Tatsuhiko Miyagawa ", "chansen " ] } Hash-MultiValue-0.15/META.yml000644 000765 000024 00000001734 12152673276 016642 0ustar00miyagawastaff000000 000000 --- abstract: 'Store multiple values per key' author: - 'Tatsuhiko Miyagawa ' build_requires: Test::More: 0 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Milla version v1.0.2, Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.131490' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Hash-MultiValue no_index: directory: - t - xt - inc - share - eg - examples requires: perl: 5.008001 resources: bugtracker: https://github.com/miyagawa/Hash-MultiValue/issues homepage: https://github.com/miyagawa/Hash-MultiValue repository: https://github.com/miyagawa/Hash-MultiValue.git version: 0.15 x_contributors: - 'Aristotle Pagaltzis ' - 'Hans Dieter Pearcey ' - 'Peter Rabbitson ' - 'Tatsuhiko Miyagawa ' - 'chansen ' Hash-MultiValue-0.15/README000644 000765 000024 00000022441 12152673276 016247 0ustar00miyagawastaff000000 000000 NAME Hash::MultiValue - Store multiple values per key SYNOPSIS use Hash::MultiValue; my $hash = Hash::MultiValue->new( foo => 'a', foo => 'b', bar => 'baz', ); # $hash is an object, but can be used as a hashref and DWIMs! my $foo = $hash->{foo}; # 'b' (the last entry) my $foo = $hash->get('foo'); # 'b' (always, regardless of context) my @foo = $hash->get_all('foo'); # ('a', 'b') keys %$hash; # ('foo', 'bar') not guaranteed to be ordered $hash->keys; # ('foo', 'foo', 'bar') guaranteed to be ordered DESCRIPTION Hash::MultiValue is an object (and a plain hash reference) that may contain multiple values per key, inspired by MultiDict of WebOb. RATIONALE In a typical web application, the request parameters (a.k.a CGI parameters) can be single value or multi values. Using CGI.pm style "param" is one way to deal with this problem (and it is good, as long as you're aware of its list context gotcha), but there's another approach to convert parameters into a hash reference, like Catalyst's "$c->req->parameters" does, and it sucks. Why? Because the value could be just a scalar if there is one value and an array ref if there are multiple, depending on *user input* rather than *how you code it*. So your code should always be like this to be defensive: my $p = $c->req->parameters; my @maybe_multi = ref $p->{m} eq 'ARRAY' ? @{$p->{m}} : ($p->{m}); my $must_single = ref $p->{m} eq 'ARRAY' ? $p->{m}->[0] : $p->{m}; Otherwise you'll get a random runtime exception of *Can't use string as an ARRAY ref* or get stringified array *ARRAY(0xXXXXXXXXX)* as a string, *depending on user input* and that is miserable and insecure. This module provides a solution to this by making it behave like a single value hash reference, but also has an API to get multiple values on demand, explicitly. HOW THIS WORKS The object returned by "new" is a blessed hash reference that contains the last entry of the same key if there are multiple values, but it also keeps the original pair state in the object tracker (a.k.a inside out objects) and allows you to access the original pairs and multiple values via the method calls, such as "get_all" or "flatten". This module does not use "tie" or overload and is quite fast. Yes, there is Tie::Hash::MultiValue and this module tries to solve exactly the same problem, but using a different implementation. UPDATING CONTENTS When you update the content of the hash, DO NOT UPDATE using the hash reference interface: this won't write through to the tracking object. my $hash = Hash::MultiValue->new(...); # WRONG $hash->{foo} = 'bar'; delete $hash->{foo}; # Correct $hash->add(foo => 'bar'); $hash->remove('foo'); See below for the list of updating methods. METHODS new $hash = Hash::MultiValue->new(@pairs); Creates a new object that can be treated as a plain hash reference as well. get $value = $hash->get($key); $value = $hash->{$key}; Returns a single value for the given $key. If there are multiple values, the last one (not first one) is returned. See below for why. Note that this always returns the single element as a scalar, regardless of its context, unlike CGI.pm's "param" method etc. get_one $value = $hash->get_one($key); Returns a single value for the given $key. This method croaks if there is no value or multiple values associated with the key, so you should wrap it with eval or modules like Try::Tiny. get_all @values = $hash->get_all($key); Returns a list of values for the given $key. This method always returns a list regardless of its context. If there is no value attached, the result will be an empty list. keys @keys = $hash->keys; Returns a list of all keys, including duplicates (see the example in the "SYNOPSIS"). If you want only unique keys, use "keys %$hash", as normal. values @values = $hash->values; Returns a list of all values, in the same order as "$hash->keys". set $hash->set($key [, $value ... ]); Changes the stored value(s) of the given $key. This removes or adds pairs as necessary to store the new list but otherwise preserves order of existing pairs. "$hash->{$key}" is updated to point to the last value. add $hash->add($key, $value [, $value ... ]); Appends a new value to the given $key. This updates the value of "$hash->{$key}" as well so it always points to the last value. remove $hash->remove($key); Removes a key and associated values for the given $key. clear $hash->clear; Clears the hash to be an empty hash reference. flatten @pairs = $hash->flatten; Gets pairs of keys and values. This should be exactly the same pairs which are given to "new" method unless you updated the data. each $hash->each($code); # e.g. $hash->each(sub { print "$_[0] = $_[1]\n" }); Calls $code once for each "($key, $value)" pair. This is a more convenient alternative to calling "flatten" and then iterating over it two items at a time. Inside $code, $_ contains the current iteration through the loop, starting at 0. For example: $hash = Hash::MultiValue->new(a => 1, b => 2, c => 3, a => 4); $hash->each(sub { print "$_: $_[0] = $_[1]\n" }); # 0: a = 1 # 1: b = 2 # 2: c = 3 # 3: a = 4 Be careful not to change @_ inside your coderef! It will update the tracking object but not the plain hash. In the future, this limitation *may* be removed. clone $new = $hash->clone; Creates a new Hash::MultiValue object that represents the same data, but obviously not sharing the reference. It's identical to: $new = Hash::MultiValue->new($hash->flatten); as_hashref $copy = $hash->as_hashref; Creates a new plain (unblessed) hash reference where a value is a single scalar. It's identical to: $copy = +{%$hash}; as_hashref_mixed, mixed $mixed = $hash->as_hashref_mixed; $mixed = $hash->mixed; Creates a new plain (unblessed) hash reference where the value is a single scalar, or an array ref when there are multiple values for a same key. Handy to create a hash reference that is often used in web application frameworks request objects such as Catalyst. Ths method does exactly the opposite of "from_mixed". as_hashref_multi, multi $multi = $hash->as_hashref_multi; $multi = $hash->multi; Creates a new plain (unblessed) hash reference where values are all array references, regardless of there are single or multiple values for a same key. from_mixed $hash = Hash::MultiValue->from_mixed({ foo => [ 'a', 'b' ], bar => 'c', }); Creates a new object out of a hash reference where the value is single or an array ref depending on the number of elements. Handy to convert from those request objects used in web frameworks such as Catalyst. This method does exactly the opposite of "as_hashref_mixed". WHY LAST NOT FIRST? You might wonder why this module uses the *last* value of the same key instead of *first*. There's no strong reasoning on this decision since one is as arbitrary as the other, but this is more consistent to what Perl does: sub x { return ('a', 'b', 'c'); } my $x = x(); # $x = 'c' my %a = ( a => 1 ); my %b = ( a => 2 ); my %m = (%a, %b); # $m{a} = 2 When perl gets a list in a scalar context it gets the last entry. Also if you merge hashes having a same key, the last one wins. NOTES ON ref If you pass this MultiValue hash object to some upstream functions that you can't control and does things like: if (ref $args eq 'HASH') { ... } because this is a blessed hash reference it doesn't match and would fail. To avoid that you should call "as_hashref" to get a *finalized* (= non-blessed) hash reference. You can also use UNIVERSAL::ref to make it work magically: use UNIVERSAL::ref; # before loading Hash::MultiValue use Hash::MultiValue; and then all "ref" calls to Hash::MultiValue objects will return *HASH*. THREAD SAFETY Prior to version 0.09, this module wasn't safe in a threaded environment, including win32 fork() emulation. Versions newer than 0.09 is considered thread safe. AUTHOR Tatsuhiko Miyagawa Aristotle Pagaltzis Hans Dieter Pearcey Thanks to Michael Peters for the suggestion to use inside-out objects instead of tie. LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO * * Tie::Hash::MultiValue Hash-MultiValue-0.15/t/000755 000765 000024 00000000000 12152673276 015627 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/tools/000755 000765 000024 00000000000 12152673276 016524 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/xt/000755 000765 000024 00000000000 12152673276 016017 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/xt/perlcritic.t000644 000765 000024 00000000223 12152673276 020341 0ustar00miyagawastaff000000 000000 use strict; use Test::More; eval q{ use Test::Perl::Critic }; plan skip_all => "Test::Perl::Critic is not installed." if $@; all_critic_ok("lib"); Hash-MultiValue-0.15/xt/pod.t000644 000765 000024 00000000201 12152673276 016757 0ustar00miyagawastaff000000 000000 use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Hash-MultiValue-0.15/xt/podspell.t000644 000765 000024 00000000342 12152673276 020025 0ustar00miyagawastaff000000 000000 use Test::More; eval q{ use Test::Spelling }; plan skip_all => "Test::Spelling is not installed." if $@; add_stopwords(); set_spell_cmd("aspell -l en list"); all_pod_files_spelling_ok('lib'); __DATA__ Tatsuhiko Miyagawa Hash-MultiValue-0.15/xt/synopsis.t000644 000765 000024 00000000160 12152673276 020070 0ustar00miyagawastaff000000 000000 use Test::More; eval "use Test::Synopsis"; plan skip_all => "Test::Synopsis required" if $@; all_synopsis_ok(); Hash-MultiValue-0.15/tools/benchmark.pl000755 000765 000024 00000001526 12152673276 021022 0ustar00miyagawastaff000000 000000 #!/usr/bin/perl use strict; use Benchmark; use Hash::MultiValue; package main; my $how_many = shift || 100; my @form = map { $_ => $_ % 5 == 0 ? [ rand 1000, rand 10000 ] : rand 1000 } 1..$how_many; timethese 0, { 'normal hash' => sub { my %form = @form; my $form = \%form; my @k = keys %$form; $form->{14} = 1000; delete $form->{14}; my @values = @{$form}{1..10}; }, multivalue => sub { my $form = Hash::MultiValue->new(@form); my @k = keys %$form; $form->{14} = 1000; delete $form->{14}; my @values = @{$form}{1..10}; }, multivalue_oo => sub { my $form = Hash::MultiValue->new(@form); my @k = $form->keys; $form->add(14 => 1000); $form->remove(14); my @values = map $form->get($_), 1..10; }, }; Hash-MultiValue-0.15/t/00_compile.t000644 000765 000024 00000000114 12152673276 017737 0ustar00miyagawastaff000000 000000 use strict; use Test::More tests => 1; BEGIN { use_ok 'Hash::MultiValue' } Hash-MultiValue-0.15/t/from_mixed.t000644 000765 000024 00000000556 12152673276 020153 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; my $hash = Hash::MultiValue->from_mixed({ foo => [ 'a', 'b' ], bar => 'baz', baz => 33, }); is "$hash->{foo}", 'b'; is ref $hash->{foo}, ''; my @foo = $hash->get_all('foo'); is_deeply \@foo, [ 'a', 'b' ]; is_deeply [ sort keys %$hash ], [ 'bar', 'baz', 'foo' ]; is $hash->{baz} + 2, 35; done_testing; Hash-MultiValue-0.15/t/hash.t000644 000765 000024 00000002607 12152673276 016744 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; my $hash = Hash::MultiValue->new( foo => 'a', foo => 'b', bar => 'baz', baz => 33, ); { my $foo = $hash->as_hashref; is ref $foo, 'HASH'; is scalar keys %$foo, 3; is ref $foo->{foo}, ''; is $foo->{foo}, 'b'; is $foo->{bar}, 'baz'; $foo->{x} = 'y'; isnt $hash->{x}, 'y'; } { my $foo = $hash->as_hashref_mixed; is ref $foo, 'HASH'; is scalar keys %$foo, 3; is ref $foo->{foo}, 'ARRAY'; is_deeply $foo->{foo}, [ 'a', 'b' ]; is $foo->{bar}, 'baz'; } { my $foo = $hash->as_hashref_multi; is ref $foo, 'HASH'; is scalar keys %$foo, 3; is ref $foo->{foo}, 'ARRAY'; is_deeply $foo->{foo}, [ 'a', 'b' ]; is_deeply $foo->{bar}, [ 'baz' ]; } { my @output; $hash->each(sub { push @output, [ $_, @_ ] }); is_deeply \@output, [ [ 0, 'foo', 'a' ], [ 1, 'foo', 'b' ], [ 2, 'bar', 'baz' ], [ 3, 'baz', 33 ], ]; } { # Test for this even though we want people not to do it $hash->each(sub { $_[1]++ }); is_deeply [ $hash->flatten ], [ foo => 'b', foo => 'c', bar => 'bba', baz => 34 ]; is_deeply $hash, { foo => 'b', bar => "baz", baz => 33 }; is_deeply [ $hash->keys ], [ qw(foo foo bar baz) ]; is_deeply [ $hash->values ], [ qw(b c bba 34) ]; } done_testing; Hash-MultiValue-0.15/t/multi.t000644 000765 000024 00000000766 12152673276 017157 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; my $hash = Hash::MultiValue->new( foo => 'a', foo => 'b', bar => 'baz', baz => 33, ); is "$hash->{foo}", 'b'; is ref $hash->{foo}, ''; my @foo = $hash->get_all('foo'); is_deeply \@foo, [ 'a', 'b' ]; is_deeply [ sort keys %$hash ], [ 'bar', 'baz', 'foo' ]; is_deeply [ $hash->keys ], [ 'foo', 'foo', 'bar', 'baz' ]; is $hash->{baz} + 2, 35; is $hash->get_one('bar'), 'baz'; eval { $hash->get_one('foo'); }; ok $@; done_testing; Hash-MultiValue-0.15/t/ref.t000644 000765 000024 00000000311 12152673276 016563 0ustar00miyagawastaff000000 000000 use Test::More; BEGIN { eval { require UNIVERSAL::ref }; plan skip_all => 'No UNIVERSAL::ref' if $@; } use Hash::MultiValue; my $h = Hash::MultiValue->new; is ref $h, 'HASH'; done_testing; Hash-MultiValue-0.15/t/release-pod-syntax.t000644 000765 000024 00000000450 12152673276 021537 0ustar00miyagawastaff000000 000000 #!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); Hash-MultiValue-0.15/t/set.t000644 000765 000024 00000001432 12152673276 016607 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; my @l = qw( foo a bar baz foo b bar quux ); my $hash = Hash::MultiValue->new( @l ); $hash->set(foo => 1 .. 3); is_deeply [ $hash->flatten ], [ qw( foo 1 bar baz foo 2 bar quux foo 3 ) ], 'more items than before'; is $hash->{'foo'}, 3; $hash->add(bar => 'qux'); $hash->set(bar => 'a' .. 'c'); is_deeply [ $hash->flatten ], [ qw( foo 1 bar a foo 2 bar b foo 3 bar c ) ], 'exactly as many items as before'; is $hash->{'bar'}, 'c'; $hash->set(foo => qw(x y)); is_deeply [ $hash->flatten ], [ qw( foo x bar a foo y bar b bar c ) ], 'fewer items than before'; is $hash->{'foo'}, 'y'; $hash->set('bar'); is_deeply [ $hash->flatten ], [ qw( foo x foo y ) ], 'no items'; is $hash->{'bar'}, undef; isa_ok $hash, 'Hash::MultiValue'; done_testing; Hash-MultiValue-0.15/t/storable.t000644 000765 000024 00000001147 12152673276 017632 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; use Storable qw(freeze thaw dclone); my $l = [ qw( foo a bar baz foo b bar quux ) ]; my $hash = Hash::MultiValue->new( @$l ); is_deeply [ $hash->flatten ], $l, 'flattening works'; my $frozen = freeze $hash; undef $hash; $hash = thaw $frozen; is_deeply [ $hash->flatten ], $l, '... even after deserialisation'; my $clone = dclone $hash; is_deeply [ $clone->flatten ], $l, '... and cloning'; $clone->remove('foo'); my ($n_hash, $n_clone) = map scalar @{[$_->flatten]}, $hash, $clone; ok $n_hash > $n_clone, '... which makes independent objects'; done_testing; Hash-MultiValue-0.15/t/threads.t000644 000765 000024 00000001105 12152673276 017443 0ustar00miyagawastaff000000 000000 use strict; use Config; use Test::More; use Hash::MultiValue; BEGIN { plan skip_all => "perl interpreter is not compiled with ithreads" unless $Config{useithreads}; plan skip_all => "perl 5.8.1 required for thread tests" unless $] > '5.0080009'; require threads; } plan tests => 2; my $h = Hash::MultiValue->new(foo => 'bar'); my @exp = ('bar'); is_deeply([$h->get_all('foo')], \@exp, 'got expected results'); my @got = threads->create(sub { $h->get_all('foo'); })->join; is_deeply(\@got, \@exp, 'got expected results in cloned interpreter'); Hash-MultiValue-0.15/t/write.t000644 000765 000024 00000001026 12152673276 017145 0ustar00miyagawastaff000000 000000 use strict; use Test::More; use Hash::MultiValue; my $hash = Hash::MultiValue->new( foo => 'a', foo => 'b', bar => 'baz', ); $hash->add(baz => 33); is $hash->{baz}, 33; my $new_hash = $hash->clone; is_deeply $hash, $new_hash; $new_hash->add('xyz' => 'zzy'); $hash->remove('foo'); is_deeply [ sort keys %$hash ], [ qw(bar baz) ]; is_deeply [ $hash->keys ], [ qw(bar baz) ]; $hash->add('bar'); # no-op add is $hash->{'bar'}, 'baz'; $hash->clear; is_deeply $hash, {}; isa_ok $hash, 'Hash::MultiValue'; done_testing; Hash-MultiValue-0.15/lib/Hash/000755 000765 000024 00000000000 12152673276 017015 5ustar00miyagawastaff000000 000000 Hash-MultiValue-0.15/lib/Hash/MultiValue.pm000644 000765 000024 00000034067 12152673276 021454 0ustar00miyagawastaff000000 000000 package Hash::MultiValue; use strict; no warnings 'void'; use 5.006_002; our $VERSION = '0.15'; use Carp (); use Scalar::Util qw(refaddr); # there does not seem to be a relevant RT or perldelta entry for this use constant _SPLICE_SAME_ARRAY_SEGFAULT => $] < '5.008007'; my %keys; my %values; my %registry; BEGIN { require Config; my $needs_registry = ($^O eq 'Win32' || $Config::Config{useithreads}); if ($needs_registry) { *CLONE = sub { foreach my $oldaddr (keys %registry) { my $this = refaddr $registry{$oldaddr}; $keys{$this} = delete $keys{$oldaddr}; $values{$this} = delete $values{$oldaddr}; Scalar::Util::weaken($registry{$this} = delete $registry{$oldaddr}); } }; } *NEEDS_REGISTRY = sub () { $needs_registry }; } if (defined &UNIVERSAL::ref::import) { UNIVERSAL::ref->import; } sub ref { 'HASH' } sub create { my $class = shift; my $self = bless {}, $class; my $this = refaddr $self; $keys{$this} = []; $values{$this} = []; Scalar::Util::weaken($registry{$this} = $self) if NEEDS_REGISTRY; $self; } sub new { my $class = shift; my $self = $class->create; unshift @_, $self; goto &{ $self->can('merge_flat') }; } sub from_mixed { my $class = shift; my $self = $class->create; unshift @_, $self; goto &{ $self->can('merge_mixed') }; } sub DESTROY { my $this = refaddr shift; delete $keys{$this}; delete $values{$this}; delete $registry{$this} if NEEDS_REGISTRY; } sub get { my($self, $key) = @_; $self->{$key}; } sub get_all { my($self, $key) = @_; my $this = refaddr $self; my $k = $keys{$this}; (@{$values{$this}}[grep { $key eq $k->[$_] } 0 .. $#$k]); } sub get_one { my ($self, $key) = @_; my @v = $self->get_all($key); return $v[0] if @v == 1; Carp::croak "Key not found: $key" if not @v; Carp::croak "Multiple values match: $key"; } sub set { my $self = shift; my $key = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; my @idx = grep { $key eq $k->[$_] } 0 .. $#$k; my $added = @_ - @idx; if ($added > 0) { my $start = $#$k + 1; push @$k, ($key) x $added; push @idx, $start .. $#$k; } elsif ($added < 0) { my ($start, @drop, @keep) = splice @idx, $added; for my $i ($start+1 .. $#$k) { if (@drop and $i == $drop[0]) { shift @drop; next; } push @keep, $i; } splice @$_, $start, 0+@$_, ( _SPLICE_SAME_ARRAY_SEGFAULT ? @{[ @$_[@keep] ]} # force different source array : @$_[@keep] ) for $k, $v; } if (@_) { @$v[@idx] = @_; $self->{$key} = $_[-1]; } else { delete $self->{$key}; } $self; } sub add { my $self = shift; my $key = shift; $self->merge_mixed( $key => \@_ ); $self; } sub merge_flat { my $self = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; push @{ $_ & 1 ? $v : $k }, $_[$_] for 0 .. $#_; @{$self}{@$k} = @$v; $self; } sub merge_mixed { my $self = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; my $hash; $hash = shift if @_ == 1; while ( my ($key, $value) = @_ ? splice @_, 0, 2 : each %$hash ) { my @value = CORE::ref($value) eq 'ARRAY' ? @$value : $value; next if not @value; $self->{$key} = $value[-1]; push @$k, ($key) x @value; push @$v, @value; } $self; } sub remove { my ($self, $key) = @_; $self->set($key); $self; } sub clear { my $self = shift; %$self = (); my $this = refaddr $self; $keys{$this} = []; $values{$this} = []; $self; } sub clone { my $self = shift; CORE::ref($self)->new($self->flatten); } sub keys { my $self = shift; return @{$keys{refaddr $self}}; } sub values { my $self = shift; return @{$values{refaddr $self}}; } sub flatten { my $self = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; map { $k->[$_], $v->[$_] } 0 .. $#$k; } sub each { my ($self, $code) = @_; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; for (0 .. $#$k) { $code->($k->[$_], $v->[$_]); } return $self; } sub as_hashref { my $self = shift; my %hash = %$self; \%hash; } sub as_hashref_mixed { my $self = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; my %hash; push @{$hash{$k->[$_]}}, $v->[$_] for 0 .. $#$k; for (CORE::values %hash) { $_ = $_->[0] if 1 == @$_; } \%hash; } sub mixed { $_[0]->as_hashref_mixed } sub as_hashref_multi { my $self = shift; my $this = refaddr $self; my $k = $keys{$this}; my $v = $values{$this}; my %hash; push @{$hash{$k->[$_]}}, $v->[$_] for 0 .. $#$k; \%hash; } sub multi { $_[0]->as_hashref_multi } sub STORABLE_freeze { my $self = shift; my $this = refaddr $self; return '', $keys{$this}, $values{$this}; } sub STORABLE_thaw { my $self = shift; my ($is_cloning, $serialised, $k, $v) = @_; my $this = refaddr $self; $keys {$this} = $k; $values{$this} = $v; @{$self}{@$k} = @$v; return $self; } 1; __END__ =encoding utf-8 =for stopwords =head1 NAME Hash::MultiValue - Store multiple values per key =head1 SYNOPSIS use Hash::MultiValue; my $hash = Hash::MultiValue->new( foo => 'a', foo => 'b', bar => 'baz', ); # $hash is an object, but can be used as a hashref and DWIMs! my $foo = $hash->{foo}; # 'b' (the last entry) my $foo = $hash->get('foo'); # 'b' (always, regardless of context) my @foo = $hash->get_all('foo'); # ('a', 'b') keys %$hash; # ('foo', 'bar') not guaranteed to be ordered $hash->keys; # ('foo', 'foo', 'bar') guaranteed to be ordered =head1 DESCRIPTION Hash::MultiValue is an object (and a plain hash reference) that may contain multiple values per key, inspired by MultiDict of WebOb. =head1 RATIONALE In a typical web application, the request parameters (a.k.a CGI parameters) can be single value or multi values. Using CGI.pm style C is one way to deal with this problem (and it is good, as long as you're aware of its list context gotcha), but there's another approach to convert parameters into a hash reference, like Catalyst's C<< $c->req->parameters >> does, and it B. Why? Because the value could be just a scalar if there is one value and an array ref if there are multiple, depending on I rather than I. So your code should always be like this to be defensive: my $p = $c->req->parameters; my @maybe_multi = ref $p->{m} eq 'ARRAY' ? @{$p->{m}} : ($p->{m}); my $must_single = ref $p->{m} eq 'ARRAY' ? $p->{m}->[0] : $p->{m}; Otherwise you'll get a random runtime exception of I or get stringified array I as a string, I and that is miserable and insecure. This module provides a solution to this by making it behave like a single value hash reference, but also has an API to get multiple values on demand, explicitly. =head1 HOW THIS WORKS The object returned by C is a blessed hash reference that contains the last entry of the same key if there are multiple values, but it also keeps the original pair state in the object tracker (a.k.a inside out objects) and allows you to access the original pairs and multiple values via the method calls, such as C or C. This module does not use C or L and is quite fast. Yes, there is L and this module tries to solve exactly the same problem, but using a different implementation. =head1 UPDATING CONTENTS When you update the content of the hash, B using the hash reference interface: this won't write through to the tracking object. my $hash = Hash::MultiValue->new(...); # WRONG $hash->{foo} = 'bar'; delete $hash->{foo}; # Correct $hash->add(foo => 'bar'); $hash->remove('foo'); See below for the list of updating methods. =head1 METHODS =over 4 =item new $hash = Hash::MultiValue->new(@pairs); Creates a new object that can be treated as a plain hash reference as well. =item get $value = $hash->get($key); $value = $hash->{$key}; Returns a single value for the given C<$key>. If there are multiple values, the last one (not first one) is returned. See below for why. Note that this B returns the single element as a scalar, regardless of its context, unlike CGI.pm's C method etc. =item get_one $value = $hash->get_one($key); Returns a single value for the given C<$key>. This method B if there is no value or multiple values associated with the key, so you should wrap it with eval or modules like L. =item get_all @values = $hash->get_all($key); Returns a list of values for the given C<$key>. This method B returns a list regardless of its context. If there is no value attached, the result will be an empty list. =item keys @keys = $hash->keys; Returns a list of all keys, including duplicates (see the example in the L). If you want only unique keys, use C<< keys %$hash >>, as normal. =item values @values = $hash->values; Returns a list of all values, in the same order as C<< $hash->keys >>. =item set $hash->set($key [, $value ... ]); Changes the stored value(s) of the given C<$key>. This removes or adds pairs as necessary to store the new list but otherwise preserves order of existing pairs. C<< $hash->{$key} >> is updated to point to the last value. =item add $hash->add($key, $value [, $value ... ]); Appends a new value to the given C<$key>. This updates the value of C<< $hash->{$key} >> as well so it always points to the last value. =item remove $hash->remove($key); Removes a key and associated values for the given C<$key>. =item clear $hash->clear; Clears the hash to be an empty hash reference. =item flatten @pairs = $hash->flatten; Gets pairs of keys and values. This should be exactly the same pairs which are given to C method unless you updated the data. =item each $hash->each($code); # e.g. $hash->each(sub { print "$_[0] = $_[1]\n" }); Calls C<$code> once for each C<($key, $value)> pair. This is a more convenient alternative to calling C and then iterating over it two items at a time. Inside C<$code>, C<$_> contains the current iteration through the loop, starting at 0. For example: $hash = Hash::MultiValue->new(a => 1, b => 2, c => 3, a => 4); $hash->each(sub { print "$_: $_[0] = $_[1]\n" }); # 0: a = 1 # 1: b = 2 # 2: c = 3 # 3: a = 4 Be careful B to change C<@_> inside your coderef! It will update the tracking object but not the plain hash. In the future, this limitation I be removed. =item clone $new = $hash->clone; Creates a new Hash::MultiValue object that represents the same data, but obviously not sharing the reference. It's identical to: $new = Hash::MultiValue->new($hash->flatten); =item as_hashref $copy = $hash->as_hashref; Creates a new plain (unblessed) hash reference where a value is a single scalar. It's identical to: $copy = +{%$hash}; =item as_hashref_mixed, mixed $mixed = $hash->as_hashref_mixed; $mixed = $hash->mixed; Creates a new plain (unblessed) hash reference where the value is a single scalar, or an array ref when there are multiple values for a same key. Handy to create a hash reference that is often used in web application frameworks request objects such as L. Ths method does exactly the opposite of C. =item as_hashref_multi, multi $multi = $hash->as_hashref_multi; $multi = $hash->multi; Creates a new plain (unblessed) hash reference where values are all array references, regardless of there are single or multiple values for a same key. =item from_mixed $hash = Hash::MultiValue->from_mixed({ foo => [ 'a', 'b' ], bar => 'c', }); Creates a new object out of a hash reference where the value is single or an array ref depending on the number of elements. Handy to convert from those request objects used in web frameworks such as L. This method does exactly the opposite of C. =back =head1 WHY LAST NOT FIRST? You might wonder why this module uses the I value of the same key instead of I. There's no strong reasoning on this decision since one is as arbitrary as the other, but this is more consistent to what Perl does: sub x { return ('a', 'b', 'c'); } my $x = x(); # $x = 'c' my %a = ( a => 1 ); my %b = ( a => 2 ); my %m = (%a, %b); # $m{a} = 2 When perl gets a list in a scalar context it gets the last entry. Also if you merge hashes having a same key, the last one wins. =head1 NOTES ON ref If you pass this MultiValue hash object to some upstream functions that you can't control and does things like: if (ref $args eq 'HASH') { ... } because this is a blessed hash reference it doesn't match and would fail. To avoid that you should call C to get a I (= non-blessed) hash reference. You can also use UNIVERSAL::ref to make it work magically: use UNIVERSAL::ref; # before loading Hash::MultiValue use Hash::MultiValue; and then all C calls to Hash::MultiValue objects will return I. =head1 THREAD SAFETY Prior to version 0.09, this module wasn't safe in a threaded environment, including win32 fork() emulation. Versions newer than 0.09 is considered thread safe. =head1 AUTHOR Tatsuhiko Miyagawa Emiyagawa@bulknews.netE Aristotle Pagaltzis Hans Dieter Pearcey Thanks to Michael Peters for the suggestion to use inside-out objects instead of tie. =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO =over 4 =item * L =item * L =back =cut