Acme-Given-Hash

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

v0.001 
  - make it work
v0.002
  - forgot to enfore 5.10 as a min 
v0.03
  - updated docs and test to show how to do a default
v0.05
  - update to turn off warnings for smartmatch in 5.18

README.pod  view on Meta::CPAN

=head1 NAME 

Acme::Given::Hash - you like given but it's too much typing.

=head1 SYNOPSIS

  use Acme::Given::Hash;
  my $result = $given ~~ gvn { $value1 => $scalar
                             , $value2 => do { ... }
                             , $value3 => sub{ ... }
                             } || $default ;

  # is going to result in the same thing as: 
  my $result; 
  given($given) {
    when ($value1) { $result = $scalar }
    when ($value2) { $result = do{ ... }}
    when ($value3) { $result = sub{...}->() }
    default        { $result = $default }
  };


  # if you use an arrayref you can do more complex matching
  $result = $given ~~ gvn [ 'scalar' => 'still works, just like the hash'
                          , qr{reg}  => [qw{also works}]
                          , [1..5]   => 'works too'
                          , {a=>b,c=>d} => 'same here'
                          , sub{$_>100} => 'for subs $_ is localized to the value of $given.'
                          , gvn { foo => 'bar' } => 'also works but "bar" will be seen as a true value rather then the thing to match'

t/00_works.t  view on Meta::CPAN


#---------------------------------------------------------------------------
#  HASHREF notation
#---------------------------------------------------------------------------
is 'this' ~~ gvn { this => 'that'} , 'that', q{works!};
is 'that' ~~ gvn { that => do{1+1}}, 2     , q{works!};
is 'that' ~~ gvn { that => sub{3}} , 3     , q{works!};

is 'that' ~~ gvn { moo  => 3 }     , undef , q{fails!};

is 'that' ~~ gvn { moo  => 3 } || 'kitten' , 'kitten' , q{default};

#---------------------------------------------------------------------------
#  LIST NOTATION
#---------------------------------------------------------------------------
my $found = {foo=>bar=>};
my $gvn = gvn [ that  => 'this'
              , qr{x} => 'found an x'
              , [1..5]=> 'one thru five'
              , { foo => 1 , bar => 2 } => $found
              , gvn { ruby => 'matz'

t/00_works.t  view on Meta::CPAN

                    } => 'language'
              ];
is 'that'       ~~ $gvn, 'this'          , 'list notation works with a scalar!';
is 'found an x' ~~ $gvn, 'found an x'    , 'list notation works with a regex!';
is 3            ~~ $gvn, 'one thru five' , 'list notation works with an aref!';
is_deeply 'foo' ~~ $gvn, $found          , 'list notation works with a href!';
is 'perl'       ~~ $gvn, 'language'      , 'list notation works with an object!';

is 'kitten'     ~~ $gvn, undef           , 'list notation fails!';

is 'kitten' ~~ $gvn || 'puppy', 'puppy'  , 'list notation fails with default with || case!';

#---------------------------------------------------------------------------
#  scoring NOTATION
#---------------------------------------------------------------------------
my $score = gvn [ sub{$_ > 15} => 2
                , sub{$_ < 10} => 1
                ] ;
is 20 ~~ $score || 0, 2, 'SCORE: upper';
is 2  ~~ $score || 0, 1, 'SOCRE: lower';
is 12 ~~ $score || 0, 0, 'SCORE: missing';



( run in 0.382 second using v1.01-cache-2.11-cpan-0a6323c29d9 )