Acme-Given-Hash
view release on metacpan or search on metacpan
, gvn { foo => 'bar' } => 'also works but "bar" will be seen as a true value rather then the thing to match'
];
=head1 EXPORTED FUNCTIONS
=head2 gvn
my $given_hash_obj = gvn {...};
my $given_array_obj = gvn [...];
This is really just a simple constructor that blesses the given structure in to an object where ~~ is overridden as to trip the value of the structure.
=head1 TODO
All on Github: https://github.com/notbenh/Acme-Given-Hash/issues
=head1 AUTHORS
NOTBENH <ben hengst> wrote this but it was heavly inspired by the team over at Puppet Labs as they have something like this, Also I could not have done this with out the smart match op, thanks team perl.
=head1 SOURCE
lib/Acme/Given/Hash.pm view on Meta::CPAN
use v5.10;
no if $] >= 5.018, warnings => "experimental::smartmatch";
our @EXPORT = qw{gvn};
#ABSTRACT: is given() too much typing for you?
sub gvn ($) {
my $when = shift;
# old hashref notation
if ( ref($when) eq 'HASH' ) {
return bless {exact => $when, calculate => []}, 'Acme::Given::Hash::Object';
}
# new arrayref notation
elsif ( ref($when) eq 'ARRAY' ) {
my $input = natatime 2, @{ $_[0] };
my $self = {exact=>{}, calculate=>[]};
my $it = natatime 2, @$when;
while (my @pairs = $it->()) {
if( ref($pairs[0]) eq '' ) {
$self->{exact}->{$pairs[0]} = $pairs[1];
}
else {
push @{ $self->{calculate} }, {match => $pairs[0], value => $pairs[1]};
}
}
return bless $self, 'Acme::Given::Hash::Object';
}
die 'gvn only takes hashrefs and arrayrefs, you have passed soemthing else';
}
package Acme::Given::Hash::Object;
{
$Acme::Given::Hash::Object::VERSION = '0.007';
}
use strict;
use warnings;
( run in 0.442 second using v1.01-cache-2.11-cpan-de7293f3b23 )