| 1 | #!perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Util::H2O; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Symbol qw/delete_package/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 Name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | Util::H2O - Hash to Object: turns hashrefs into objects with accessors for keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 Synopsis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | use Util::H2O; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | my $hash = h2o { foo => "bar", x => "y" }, qw/ more keys /; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | print $hash->foo, "\n"; # accessor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | $hash->x("z"); # change value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | $hash->more("cowbell"); # additional keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | my $struct = { hello => { perl => "world!" } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | h2o -recurse, $struct; # objectify nested hashrefs as well | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | print $struct->hello->perl, "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | my $obj = h2o -meth, { # code references become methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | what => "beans", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | cool => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | print $self->what, "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | $obj->cool; # prints "beans" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | h2o -classify=>'Point', { # whip up a class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | angle => sub { my $self = shift; atan2($self->y, $self->x) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, qw/ x y /; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | my $one = Point->new(x=>1, y=>2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | my $two = Point->new(x=>3, y=>4); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | printf "%.3f\n", $two->angle; # prints 0.927 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # For AUTHOR, COPYRIGHT, AND LICENSE see the bottom of this file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | our @EXPORT = qw/ h2o /; ## no critic (ProhibitAutomaticExportation) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | our @EXPORT_OK = qw/ o2h /; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # lock_ref_keys wasn't available until Hash::Util 0.06 / Perl v5.8.9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # (note the following will probably also fail on the Perl v5.9 dev releases) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # uncoverable branch false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # uncoverable condition false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ( $] ge '5.008009' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | require Hash::Util; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | Hash::Util->import(qw/ lock_ref_keys lock_hashref /) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | *lock_ref_keys = *lock_hashref = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | carp "this Perl is too old to lock the hash"; # uncoverable statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | }; # uncoverable statement | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | This module allows you to turn hashrefs into objects, so that instead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | of C<< $hash->{key} >> you can write C<< $hash->key >>, plus you get | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | protection from typos. In addition, options are provided that allow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | you to whip up really simple classes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | You can still use the hash like a normal hashref as well, as in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | C<< $hash->{key} >>, C<keys %$hash>, and so on, but note that by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | default this function also locks the hash's keyset to prevent typos | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | there too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | This module exports a single function by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | =head2 C<h2o I<@opts>, I<$hashref>, I<@additional_keys>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =head3 C<@opts> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | If you specify an option with a value multiple times, only the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | one will take effect. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =item C<-recurse> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | Nested hashes are objectified as well. The only options that are passed down to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | nested hashes are C<-lock> and C<-ro>. I<None> of the other options will be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | applied to the nested hashes, including C<@additional_keys>. Nested arrayrefs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | are not recursed into, but see the C<-arrays> option for that. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | Versions of this module before v0.12 did not pass down the C<-lock> option, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | meaning that if you used C<-nolock, -recurse> on those versions, the nested | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | hashes would still be locked. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | =item C<-arrays> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | Like C<-recurse>, but additionally, C<h2o> is applied to elements of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | nested arrays as well. The same options as with C<-recurse> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | passed down to nested hashes and arrayrefs. Takes precedence over the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | C<-pass> option, i.e. if you use these two options together, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | arrayrefs are still descended into. Like hashrefs, the original | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | arrays are modified! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | This option implies C<-recurse>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | This option was added in v0.20. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | =item C<-meth> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | Any code references present in the hash at the time of this function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | call will be turned into methods. Because these methods are installed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | into the object's package, they can't be changed later by modifying | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | the hash. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | To avoid confusion when iterating over the hash, the hash entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | that were turned into methods are removed from the hash. The key is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | also removed from the "allowed keys" (see the C<-lock> option), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | I<unless> you specify it in C<@additional_keys>. In that case, you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | can change the value of that key completely independently of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | method with the same name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =item C<< -class => I<classname> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | Specify the class name into which to bless the object (as opposed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | the default: a generated, unique package name in C<Util::H2O::>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | I<Note:> If you use this option, C<-clean> defaults to I<false>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | meaning that the package will stay in Perl's symbol table and use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | memory accordingly, and since this function installs the accessors in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | the package every time it is called, if you re-use the same package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | name, you will get "redefined" warnings. Therefore, if you want to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | create multiple objects in the same package, you should probably use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | C<-new> or C<-classify>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | If you wanted to generate a unique package name in a different package, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | you could use: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | C<< h2o -class => sprintf('My::Class::Name::_%x', $hash+0), $hash >>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | perhaps even in combination with C<< -isa => 'My::Class::Name' >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | However, keep in mind that you shouldn't step into another class' namespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | without knowing that this won't cause conflicts, and also that not using the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | default class names means that functions like C<o2h> will no longer identify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | the objects as coming from C<h2o>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =item C<< -classify => I<classname_string or $hashref> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | In the form C<< -classify => I<classname_string> >>, this is simply the short | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | form of the options C<< -new, -meth, -class => I<classname_string> >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | As of v0.16, in the special form C<< -classify => I<$hashref> >>, where the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | C<-classify> B<must> be the B<last> option in C<@opts> before the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | L<C<$hashref>|/"$hashref">, it is the same as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | C<< -new, -meth, -class => __PACKAGE__, I<$hashref> >> - that is, the current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | package's name is used as the custom class name. It does not make sense to use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | this outside of an explicit package, since your class will be named C<main>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | With this option, the C<Point> example in the L</Synopsis> can be written like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | the following, which can be useful if you want to add more things to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | C<package>, or perhaps if you want to write your methods as regular C<sub>s: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | package Point; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | use Util::H2O; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | h2o -classify, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | angle => sub { my $self = shift; atan2($self->y, $self->x) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | }, qw/ x y /; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | Note C<h2o> will remain in the package's namespace, one possibility is that you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | could load L<namespace::clean> after you load this module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | You might also note that in the above example, one could write C<angle> as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | regular C<sub> in the package. And at that point, one might recongize the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | similarity between the code and what one can do with e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | L<Class::Tiny|Class::Tiny> or even L<Moo|Moo>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | =item C<< -isa => I<arrayref or scalar> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | Convenience option to set the L<C<@ISA>|perlvar/"@ISA"> variable in the package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | of the object, so that the object inherits from that/those package(s). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | This option was added in v0.14. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | B<Warning:> The methods created by C<h2o> will not call superclass methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | This means the parent class' C<DESTROY> method(s) are not called, and any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | accessors generated from hash keys are blindly overriden. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | =item C<-new> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | Generates a constructor named C<new> in the package. The constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | works as a class and instance method, and dies if it is given any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | arguments that it doesn't know about. If you want more advanced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | features, like required arguments, validation, or other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | initialization, you should probably L<switch|/"Upgrading to Moo"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | to something like L<Moo> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | =item C<< -destroy => I<coderef> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | Allows you to specify a custom destructor. This coderef will be called from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | object's actual C<DESTROY> in void context with the first argument being the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | same as the first argument to the C<DESTROY> method. Errors will be converted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | to warnings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | This option was added in v0.14. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =item C<< -clean => I<bool> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | Whether or not to clean up the generated package when the object is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | destroyed. Defaults to I<false> when C<-class> is specified, I<true> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | otherwise. If this is I<false>, be aware that the packages will stay | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | in Perl's symbol table and use memory accordingly, and any subs/methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | in those packages may cause "redefined" warnings if the package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | name is re-used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | As of v0.16, this module will refuse to delete the package if it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | is named C<main>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item C<< -lock => I<bool> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | Whether or not to use L<Hash::Util>'s C<lock_ref_keys> to prevent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | modifications to the hash's keyset. Defaults to I<true>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | The C<-nolock> option is provided as a short form of C<< -lock=>0 >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | Keysets of objects created by the constructor generated by the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | C<-new> option are also locked. Versions of this module before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | v0.12 did not lock the keysets of new objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | Note that on really old Perls, that is, before Perl v5.8.9, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | L<Hash::Util> and its C<lock_ref_keys> are not available, so the hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | is never locked on those versions of Perl. Versions of this module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | before v0.06 did not lock the keyset. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | Versions of this module as of v0.12 issue a warning on old Perls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | =item C<-nolock> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | Short form of the option C<< -lock=>0 >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | =item C<-ro> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | Makes the entire hash read-only using L<Hash::Util>'s C<lock_hashref> and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | generated accessors will also throw an error if you try to change values. In | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | other words, this makes the object and the underlying hash immutable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | You cannot specify any C<@additional_keys> with this option enabled unless you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | also use the C<-new> option - the additional keys will then only be useful as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | arguments to the constructor. This option can't be used with C<-nolock> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | C<< -lock=>0 >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | This option was added in v0.12. Using this option will not work and cause a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | warning when used on really old Perls (before v5.8.9), because this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | functionality was not yet available there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | =item C<< -pass => "ref" I<or> "undef" >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | When this option is set to C<"undef"> (that's the string C<"undef">, I<not> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | C<undef> itself!), then passing a value of C<undef> for the C<$hashref> will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | not result in a fatal error, the value will simply be passed through. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | When this option is set to the string C<"ref">, then any value other than a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | plain hashref that is a reference, including objects, plus C<undef> as above, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | will be passed through without modification. Any hashes nested inside of these | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | references will not be descended into, even when C<-recurse> is specified. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | However, C<-arrays> takes precedence over this option, see its documentation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | This option was added in v0.18. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | =head3 C<$hashref> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | You must supply a plain (unblessed) hash reference here, unless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | you've specified the C<-pass> and/or C<-arrays> options. Be aware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | that this function I<does> modify the original hashref(s) by blessing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | it and locking its keyset (the latter can be disabled with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | C<-lock> option), and if you use C<-meth> or C<-classify>, keys whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | values are code references will be removed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | If you use C<-arrays>, the elements of those arrays may also be modified. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | An accessor will be set up for each key in the hash(es); note that the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | keys must of course be valid Perl identifiers for you to be able to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | call the method normally (see also the L</Cookbook>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | The following keys will be treated specially by this module. Please note that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | there are further keys that are treated specially by Perl and/or that other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | code may expect to be special, such as L<UNIVERSAL>'s C<isa>. See also | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | L<perlsub> and the references therein. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | =item C<new> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | This key is not allowed in the hash if the C<-new> option is on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | =item C<DESTROY> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | This key is not allowed except if all of the following apply: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | C<-destroy> is not used, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | C<-clean> is off (which happens by default when you use C<-class>), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | C<-meth> is on, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | the value of the key C<DESTROY> is a coderef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | Versions of this module before v0.14 allowed a C<DESTROY> key in more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | circumstances (whenever C<-clean> was off). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | =item C<AUTOLOAD> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | If your hash contains a key named C<AUTOLOAD>, or this key is present in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | C<@additional_keys>, this module will set up a method called C<AUTOLOAD>, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | is subject to Perl's normal autoloading behavior - see L<perlsub/Autoloading> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | and L<perlobj/AUTOLOAD>. Without the C<-meth> option, you will get a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | "catch-all" accessor to which all method calls to unknown method names will go, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | and with C<-meth> enabled (which is implied by C<-classify>), you can install | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | your own custom C<AUTOLOAD> handler by passing a coderef as the value for this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | key - see L</An Autoloading Example>. However, it is important to note that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | enabling autoloading removes any typo protection on method names! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | =head3 C<@additional_keys> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | Methods will be set up for these keys even if they do not exist in the hash. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | Please see the list of keys that are treated specially above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | The (now blessed and optionally locked) C<$hashref>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | our $_PACKAGE_REGEX = qr/\AUtil::H2O::_[0-9A-Fa-f]+\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | sub h2o { ## no critic (RequireArgUnpacking, ProhibitExcessComplexity) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 161 | ▶ | my ($recurse,$arrays,$meth,$class,$isa,$destroy,$new,$clean,$lock,$ro,$pass); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
h2o: called CC=81 SCAR=43.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 161 | ▶ | while ( @_ && $_[0] && !ref$_[0] && $_[0]=~/^-/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
@_ and $_[0] and not ref $_[0] and $_[0] =~ /^-/
@_ and $_[0] and not ref $_[0]
@_ and $_[0]
MC/DC100%4/4
@_ and $_[0] and not ref $_[0] and $_[0] =~ /^-/
@_
$_[0]
not ref $_[0]
$_[0] =~ /^-/
Truth table100%5/5
A and B and not(C) and D
A= @_B= $_[0]C= not ref $_[0]D= $_[0] =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 233 | ▶ | if ($_[0] eq '-recurse' ) { $recurse = shift } ## no critic (ProhibitCascadingIfElse) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 13 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 45 | elsif ($_[0] eq '-arrays'){ $arrays = shift } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 20 | elsif ($_[0] eq '-meth' ) { $meth = shift } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 9 | ▶ | elsif ($_[0] eq '-clean') { $clean = (shift, shift()?1:0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 50 | ▶ | elsif ($_[0] eq '-lock' ) { $lock = (shift, shift()?1:0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 4 | elsif ($_[0] eq '-nolock'){ $lock = 0; shift } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 13 | elsif ($_[0] eq '-ro' ) { $ro = shift } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 8 | elsif ($_[0] eq '-new' ) { $new = shift } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | elsif ($_[0] eq '-pass' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 18 | $pass = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 18 | ▶ | croak "invalid -pass option value (must be 'undef' or 'ref')" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $pass or $pass ne "undef" and $pass ne "ref"
$pass ne "undef" and $pass ne "ref"
MC/DC66%2/3
not defined $pass or $pass ne "undef" and $pass ne "ref"
not defined $pass
$pass ne "undef"
$pass ne "ref"
Truth table75%3/4
not(A) or B and C
A= not defined $passB= $pass ne "undef"C= $pass ne "ref"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | if !defined $pass || $pass ne 'undef' && $pass ne 'ref'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | elsif ($_[0] eq '-class') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 16 | $class = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 16 | ▶ | croak "invalid -class option value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $class or ref $class or not length $class
not defined $class or ref $class
MC/DC100%3/3
not defined $class or ref $class or not length $class
not defined $class
ref $class
not length $class
Truth table100%4/4
not(A) or B or not(C)
A= not defined $classB= ref $classC= not length $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | if !defined $class || ref $class || !length $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | elsif ($_[0] eq '-classify') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 11 | $class = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 11 | ▶ | if ( ref $class eq 'HASH' ) { unshift @_, $class; $class = caller; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 11 | ▶ | croak "invalid -classify option value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $class or ref $class or not length $class
not defined $class or ref $class
MC/DC100%3/3
not defined $class or ref $class or not length $class
not defined $class
ref $class
not length $class
Truth table100%4/4
not(A) or B or not(C)
A= not defined $classB= ref $classC= not length $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | if !defined $class || ref $class || !length $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 8 | $meth = 1; $new = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | elsif ($_[0] eq '-isa') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 7 | $isa = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 7 | ▶ | croak "invalid -isa option value" if !( ref($isa) eq 'ARRAY' || !ref($isa) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $isa eq "ARRAY" or not ref $isa
MC/DC100%2/2
ref $isa eq "ARRAY" or not ref $isa
ref $isa eq "ARRAY"
not ref $isa
Truth table100%3/3
A or not(B)
A= ref $isa eq "ARRAY"B= not ref $isa
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 6 | ▶ | $isa = [$isa] unless ref $isa; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | elsif ($_[0] eq '-destroy') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 8 | $destroy = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 8 | ▶ | croak "invalid -destroy option value" unless ref $destroy eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 3 | else { croak "unknown option to h2o: '$_[0]'" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 145 | ▶ | $clean = !defined $class unless defined $clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 145 | ▶ | $lock = 1 unless defined $lock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 145 | ▶ | $recurse = 1 if $arrays; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 145 | my $hash = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 145 | ▶ | if ( ref $hash ne 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 32 | ▶ | if ( $arrays && ref $hash eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$arrays and ref $hash eq "ARRAY"
MC/DC100%2/2
$arrays and ref $hash eq "ARRAY"
$arrays
ref $hash eq "ARRAY"
Truth table100%3/3
A and B
A= $arraysB= ref $hash eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 15 | for (@$hash) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 23 | ▶ | { h2o( -arrays, -lock=>$lock, ($ro?-ro:()), $_ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $_ eq "HASH" or ref $_ eq "ARRAY"
MC/DC100%2/2
ref $_ eq "HASH" or ref $_ eq "ARRAY"
ref $_ eq "HASH"
ref $_ eq "ARRAY"
Truth table100%3/3
A or B
A= ref $_ eq "HASH"B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | if ref eq 'HASH' || ref eq 'ARRAY' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 15 | return $hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | elsif ( $pass ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 9 | ▶ | if ( $pass eq 'ref' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 6 | ▶ | return $hash if !defined $hash || ref $hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $hash or ref $hash
MC/DC100%2/2
not defined $hash or ref $hash
not defined $hash
ref $hash
Truth table100%3/3
not(A) or B
A= not defined $hashB= ref $hash
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 2 | croak "this h2o call only accepts references or undef"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | else { # $pass must be 'undef' due to checks above | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 3 | ▶ | return $hash if !defined $hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 2 | croak "this h2o call only accepts a plain hashref or undef"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 8 | croak "this h2o call only accepts plain hashrefs"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 113 | ▶ | croak "h2o with additional keys doesn't make sense with -ro" if $ro && @_ && !$new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$ro and @_ and not $new
$ro and @_
MC/DC100%3/3
$ro and @_ and not $new
$ro
@_
not $new
Truth table100%4/4
A and B and not(C)
A= $roB= @_C= not $new
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 112 | my %ak = map {$_=>1} @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 112 | my %keys = map {$_=>1} @_, keys %$hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | croak "h2o hashref may not contain a key named DESTROY" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 112 | ▶ | if exists $keys{DESTROY} && ( $destroy || $clean || !$meth || ref $hash->{DESTROY} ne 'CODE' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%12/12
exists $keys{"DESTROY"} and $destroy || $clean || !$meth || ref $hash->{"DESTROY"} ne "CODE"
$destroy or $clean or not $meth or ref $hash->{"DESTROY"} ne "CODE"
$destroy or $clean or not $meth
$destroy or $clean
MC/DC40%2/5
exists $keys{"DESTROY"} and $destroy || $clean || !$meth || ref $hash->{"DESTROY"} ne "CODE"
exists $keys{"DESTROY"}
$destroy
$clean
not $meth
ref $hash->{"DESTROY"} ne "CODE"
Truth table50%3/6
A and B or C or not(D) or E
A= exists $keys{"DESTROY"}B= $destroyC= $cleanD= not $methE= ref $hash->{"DESTROY"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | croak "h2o hashref may not contain a key named new if you use the -new option" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 100 | ▶ | if $new && exists $keys{new}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new and exists $keys{"new"}
MC/DC100%2/2
$new and exists $keys{"new"}
$new
exists $keys{"new"}
Truth table100%3/3
A and B
A= $newB= exists $keys{"new"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 99 | ▶ | croak "h2o can't turn off -lock if -ro is on" if $ro && !$lock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ro and not $lock
MC/DC100%2/2
$ro and not $lock
$ro
not $lock
Truth table100%3/3
A and not(B)
A= $roB= not $lock
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 98 | ▶ | if ($recurse) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 48 | for (values %$hash) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 59 | ▶ | if ( $arrays && ref eq 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$arrays and ref $_ eq "ARRAY"
MC/DC100%2/2
$arrays and ref $_ eq "ARRAY"
$arrays
ref $_ eq "ARRAY"
Truth table100%3/3
A and B
A= $arraysB= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 10 | ▶ | { h2o(-arrays, -lock=>$lock, ($ro?-ro:()), $_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | elsif ( ref eq 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 17 | ▶ | { h2o($arrays?-arrays:-recurse, -lock=>$lock, ($ro?-ro:()), $_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 98 | ▶ | my $pack = defined $class ? $class : sprintf('Util::H2O::_%x', $hash+0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 98 | for my $k (keys %keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | my $sub = $ro | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 29 | ▶ | ? sub { my $self = shift; croak "this object is read-only" if @_; exists $self->{$k} ? $self->{$k} : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 119 | ▶ | : sub { my $self = shift; $self->{$k} = shift if @_; $self->{$k} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 119 | ▶ | if ( $meth && ref $$hash{$k} eq 'CODE' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$meth and ref $hash->{$k} eq "CODE"
MC/DC100%2/2
$meth and ref $hash->{$k} eq "CODE"
$meth
ref $hash->{$k} eq "CODE"
Truth table100%3/3
A and B
A= $methB= ref $hash->{$k} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 18 | ▶ | { $sub = delete $$hash{$k}; $ak{$k} or delete $keys{$k} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | ▶ | { no strict 'refs'; *{"${pack}::$k"} = $sub } ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 98 | ▶ | if ( $destroy || $clean ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$destroy or $clean
MC/DC100%2/2
$destroy or $clean
$destroy
$clean
Truth table100%3/3
A or B
A= $destroyB= $clean
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | my $sub = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 83 | ▶ | $destroy and ( eval { $destroy->($_[0]); 1 } or carp $@ ); ## no critic (ProhibitMixedBooleanOperators) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
Condition100%4/4
$destroy && (eval {
do {
&$destroy($_[0]);
1
}
} || carp($@))
eval {
do {
&$destroy($_[0]);
1
}
} or carp($@)
MC/DC0%0/3
$destroy && (eval {
do {
&$destroy($_[0]);
1
}
} || carp($@))
$destroy
eval {
do {
&$destroy($_[0]);
1
}
}
carp($@)
Truth table0%0/4
A && B or C
A= $destroyB= eval {
do {
&$destroy($_[0]);
1
}
}C= carp($@)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 83 | ▶ | if ( $clean ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 81 | ▶ | if ( $pack eq 'main' ) { carp "h2o refusing to delete package \"main\"" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 80 | else { delete_package($pack) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 83 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 1 | ▶ | { no strict 'refs'; *{$pack.'::DESTROY'} = $sub } ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 98 | ▶ | if ( $new ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | my $sub = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 17 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 17 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 17 | ▶ | croak "Odd number of elements in argument list" if @_%2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 16 | my $self = {@_}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 16 | ▶ | exists $keys{$_} or croak "Unknown argument '$_'" for keys %$self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 15 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 15 | ▶ | if ($ro) { lock_hashref $self } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 12 | elsif ($lock) { lock_ref_keys $self, keys %keys } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 1 | ▶ | { no strict 'refs'; *{$pack.'::new'} = $sub } ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 1 | ▶ | if ($isa) { no strict 'refs'; @{$pack.'::ISA'} = @$isa } ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 98 | bless $hash, $pack; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 98 | ▶ | if ($ro) { lock_hashref $hash } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 83 | elsif ($lock) { lock_ref_keys $hash, keys %keys } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 98 | return $hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | =head2 C<o2h I<@opts>, I<$h2object>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | This function takes an object as created by C<h2o> and turns it back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | into a hashref by making shallow copies of the object hash and any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | nested objects that may have been created via C<-recurse>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | C<-arrays>, or created manually. This function is recursive by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | default because for a non-recursive operation you can simply write: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | C<{%$h2object}> (making a shallow copy). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | Unlike C<h2o>, this function returns a new hashref instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | modifying the given variable in place (unless what you give this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | function is not an C<h2o> object, in which case it will just be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | returned unchanged). Similarly, if you specify the C<-arrays> option, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | shallow copies of arrays will be returned in place of the original | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | ones, with C<o2h> applied to the elements. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | B<Note> that this function operates only on objects in the default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | package - it does not step into plain hashrefs, it does not step into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | arrayrefs unless you specify C<-arrays>, nor does it operate on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | objects created with the C<-class> or C<-classify> options. Also be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | aware that because methods created via C<-meth> are removed from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | object hash, these will disappear in the resulting hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | This function was added in v0.18. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | =head3 C<@opts> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | If you specify an option with a value multiple times, only the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | one will take effect. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | =item C<-arrays> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | If you specify this option, nested arrayrefs are descended into as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | This option was added in v0.20. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | sub o2h { ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 41 | ▶ | my ($arrays); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
o2h: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 41 | ▶ | while ( @_ && $_[0] && !ref$_[0] && $_[0]=~/^-/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
@_ and $_[0] and not ref $_[0] and $_[0] =~ /^-/
@_ and $_[0] and not ref $_[0]
@_ and $_[0]
MC/DC100%4/4
@_ and $_[0] and not ref $_[0] and $_[0] =~ /^-/
@_
$_[0]
not ref $_[0]
$_[0] =~ /^-/
Truth table100%5/5
A and B and not(C) and D
A= @_B= $_[0]C= not ref $_[0]D= $_[0] =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 27 | ▶ | if ($_[0] eq '-arrays' ) { $arrays = shift } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 1 | else { croak "unknown option to o2h: '$_[0]'" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 40 | ▶ | croak "missing argument to o2h" unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 39 | my $h2o = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 39 | ▶ | croak "too many arguments to o2h" if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 37 | ▶ | my @args = ( $arrays ? (-arrays) : () ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 37 | ▶ | if ( ref($h2o) =~ $_PACKAGE_REGEX ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 18 | { return { map { $_ => o2h(@args, $h2o->{$_}) } keys %$h2o } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | ▶ | elsif ( $arrays && ref $h2o eq 'ARRAY' ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$arrays and ref $h2o eq "ARRAY"
MC/DC100%2/2
$arrays and ref $h2o eq "ARRAY"
$arrays
ref $h2o eq "ARRAY"
Truth table100%3/3
A and B
A= $arraysB= ref $h2o eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 6 | { return [ map { o2h(@args, $_) } @$h2o ] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 13 | return $h2o; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||