From 15fa3f951855185e0fc41d6db88fe3384cbd84a6 Mon Sep 17 00:00:00 2001 From: Hauke D Date: Mon, 19 Jul 2021 12:50:58 +0200 Subject: [PATCH] Documentation tweak (with test) and fixes --- lib/Util/H2O.pm | 11 ++++++----- t/Util-H2O.t | 7 ++++++- xt/author.t | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Util/H2O.pm b/lib/Util/H2O.pm index 2b96559..14d770b 100644 --- a/lib/Util/H2O.pm +++ b/lib/Util/H2O.pm @@ -17,7 +17,7 @@ Util::H2O - Hash to Object: turns hashrefs into objects with accessors for keys my $hash = h2o { foo => "bar", x => "y" }, qw/ more keys /; print $hash->foo, "\n"; # accessor $hash->x("z"); # change value - $hash->more("quz"); # additional keys + $hash->more("cowbell"); # additional keys my $struct = { hello => { perl => "world!" } }; h2o -recurse, $struct; # objectify nested hashrefs as well @@ -145,7 +145,7 @@ in Perl's symbol table and use memory accordingly. Whether or not to use L's C to prevent modifications to the hash's keyset. Defaults to I. -The C<-nolock> option is provided as a short form of C<< lock=>0 >>. +The C<-nolock> option is provided as a short form of C<< -lock=>0 >>. Keysets of objects created by the constructor generated by the C<-new> option are also locked. Versions of this module before @@ -159,7 +159,7 @@ Versions of this module as of v0.12 issue a warning on old Perls. =item C<-nolock> -Short form of the option C<< lock=>0 >>. +Short form of the option C<< -lock=>0 >>. =item C<-ro> @@ -170,7 +170,7 @@ other words, this makes the object and the underlying hash immutable. You cannot specify any C<@additional_keys> with this option enabled unless you also use the C<-new> option - the additional keys will then only be useful as arguments to the constructor. This option can't be used with C<-nolock> or -C<< lock=>0 >>. +C<< -lock=>0 >>. This option was added in v0.12. Using this option will not work and cause a warning when used on really old Perls (before v5.8.9), because this @@ -183,7 +183,8 @@ functionality was not yet available there. You must supply a plain (unblessed) hash reference here. Be aware that this function I modify the original hashref(s) by blessing it and locking its keyset (the latter can be disabled with the -C<-lock> option). +C<-lock> option), and if you use C<-meth> or C<-classify>, keys whose +values are code references will be removed. An accessor will be set up for each key in the hash; note that the keys must of course be valid Perl identifiers for you to be able to diff --git a/t/Util-H2O.t b/t/Util-H2O.t index 0105ce5..1e553f6 100755 --- a/t/Util-H2O.t +++ b/t/Util-H2O.t @@ -20,7 +20,7 @@ L. =cut -use Test::More tests => 190; +use Test::More tests => 191; use Scalar::Util qw/blessed/; sub exception (&) { eval { shift->(); 1 } ? undef : ($@ || die) } ## no critic (ProhibitSubroutinePrototypes, RequireFinalReturn, RequireCarping) @@ -127,6 +127,11 @@ my $PACKRE = qr/\AUtil::H2O::_[0-9A-Fa-f]+\z/; is_deeply $o, { x=>111, y=>333 }; is $o->y, 222; } +{ + my $h = { foo => 123, bar => sub {} }; + h2o -meth, $h; + is_deeply $h, { foo => 123 }; +} # -class { diff --git a/xt/author.t b/xt/author.t index efc8d32..f70f801 100755 --- a/xt/author.t +++ b/xt/author.t @@ -89,7 +89,7 @@ subtest 'code in POD' => sub { plan tests=>9; ok eval('{'.<<"END_CODE".';1}'), 'synopsis runs' or diag explain $@; ## no critic (ProhibitStringyEval, RequireCheckingReturnValueOfEval) use warnings; use strict; $$verbatim[0]; - is_deeply \$hash, { foo=>'bar', x=>'z', more=>'quz' }, 'synopsis \$hash'; + is_deeply \$hash, { foo=>'bar', x=>'z', more=>'cowbell' }, 'synopsis \$hash'; is_deeply \$struct, { hello => { perl => "world!" } }, 'synopsis \$struct'; isa_ok \$one, 'Point'; is_deeply \$one, { x=>1, y=>2 }, 'synopsis \$one';