The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Devel::Ladybug::Hash - Hashtable object

DESCRIPTION

Extends Devel::Ladybug::Object to handle Perl HASH refs as Devel::Ladybug Objects. Provides constructor, getters, setters, "Ruby-esque" collection, and other methods which one might expect a Hash table object to respond to.

SYNOPSIS

  use Devel::Ladybug::Hash;

  my $hash = Devel::Ladybug::Hash->new();

  my $hashFromNonRef = Devel::Ladybug::Hash->new(%hash); # Makes new ref

  my $hashFromRef = Devel::Ladybug::Hash->new($hashref); # Keeps orig ref

PUBLIC INSTANCE METHODS

  • $hash->each($sub), yield(item, [item, ...]), emit(item, [item...])

    Ruby-esque key iterator method. Returns a new Devel::Ladybug::Array, containing the yielded results of calling the received sub for each key in $hash.

    $hash->each is shorthand for $hash->keys->each, so you're really calling each in Devel::Ladybug::Array. yield and emit are exported by Devel::Ladybug::Array. Please see the documentation for Devel::Ladybug::Array regarding usage of each, yield, and emit.

      #
      # For example, quickly wrap <a> tags around array elements:
      #
      my $tagged = $object->each( sub {
        my $key = shift;
    
        print "Key $key is $object->{$key}\n";
    
        yield("<a name=\"$key\">$object->{$key}</a>");
      } );
  • $self->keys()

    Returns an Devel::Ladybug::Array object containing self's alpha sorted keys.

      my $hash = Devel::Ladybug::Hash->new(foo=>'alpha', bar=>'bravo');
    
      my $keys = $hash->keys();
    
      print $keys->join(','); # Prints out "bar,foo"
  • $self->values()

    Returns an Devel::Ladybug::Array object containing self's values, alpha sorted by key.

      my $hash = Devel::Ladybug::Hash->new(foo=>'alpha', bar=>'bravo');
    
      my $values = $hash->values();
    
      print $values->join(','); # Prints out "bravo,alpha"
  • $self->set($key,$value);

    Set the received instance variable. Extends Devel::Ladybug::Object::set to always use Devel::Ladybug::Hash and Devel::Ladybug::Array when it can.

      my $hash = Devel::Ladybug::Hash->new(foo=>'alpha', bar=>'bravo');
    
      $hash->set('bar', 'foxtrot'); # bar was "bravo", is now "foxtrot"
  • $self->count

    Returns the number of key/value pairs in self

  • $self->isEmpty()

    Returns true if self's count is 0, otherwise false.

SEE ALSO

This file is part of Devel::Ladybug.