File Coverage

blib/lib/Rubyish/Hash.pm
Criterion Covered Total %
statement 6 8 75.0
branch 0 2 0.0
condition n/a
subroutine 2 3 66.6
pod n/a
total 8 13 61.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Rubyish::Hash - Hash (class)
4              
5             =cut
6              
7             package Rubyish::Hash;
8              
9 17     17   88 use base qw(Rubyish::Object); # inherit parent
  17         29  
  17         6025  
10 8     8   742 use Rubyish::Syntax::def;
  8         15  
  8         53  
11              
12             =head1 FUNCTIONS
13              
14             =head2 new
15              
16             constructor
17              
18             =cut
19              
20             sub new {
21 0 0   0     my $self = ref($_[1]) eq "HASH" ? $_[1] : {};
22 0           bless $self, $_[0];
23             }
24              
25             =head2 inspect #=> perl_string
26              
27             in
28              
29             =cut
30              
31             def inspect() {
32             my $result;
33             while ( my ($key, $value) = each %{$self} ) {
34             $result .= "$key => $value, ";
35             }
36             $result =~ s/, $/ /g;
37             "{ " . $result . "}";
38             }
39              
40             =head2 fetch
41              
42             =head2 {}
43              
44             Retrieves the value Element corresponding to the key.
45              
46             $hash = Hash({ hello => "world" });
47             $hash->fetch("hello") #=> world
48             $hash->{hello} #=> world
49              
50             =cut
51              
52             def fetch($key) {
53             $self->{$key}
54             };
55              
56             =head2 each
57              
58             =head2 map
59              
60             $hash = Hash({ blah~ });
61             $hash->each( sub {
62             my ($key, $value) = @_; # specify your iterator
63             print "$key => $value\n";
64             });
65              
66             =cut
67              
68             def each($sub) {
69             %result = %{$self};
70             while ( my ($key, $value) = each %result ) {
71             $sub->($key,$value);
72             }
73             $self;
74             };
75             { no strict; *map = *each; }
76              
77             1;