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

OP::Hash - Hashtable object

DESCRIPTION

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

INHERITANCE

This class inherits additional class and object methods from the following packages:

OP::Class > OP::Object > OP::Hash

SYNOPSIS

  use OP::Hash;

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

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

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

PUBLIC CLASS METHODS

  • $class->assert(*@rules)

    Return a new OP::Type::Hash instance.

    Hash() simply specifies that the attribute's value is a free-form hashtable. No further validation will be performed against the value, other than to make sure it's a HASH (or OP::Hash) reference.

    Hash() is intended for cases where arbitrary, extrinsic hashtable- formatted data, with keys not known in advance, needs to be stored within an object. It should not be used as a replacement for ExtID() linked to a properly modeled class.

    The key/value pairs in the stored hash live in a dynamically subclassed linked table, with foreign key constraints against the parent table.

      #
      # File: Example.pm
      #
      use OP;
    
      create "OP::Example" => {
        inlineHash => OP::Hash->assert()
      };

    In caller:

      #!/bin/env perl
      #
      # File: somecaller.pl
      #
    
      use strict;
      use warnings;
    
      use OP::Example;
    
      my $example = OP::Example->spawn("Inline Hash Example");
    
      $example->setInlineHash({ foo => bar });
    
      $example->save();

PUBLIC INSTANCE METHODS

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

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

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

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

    List iterator method. Runs $sub for each element in self; returns true on success.

      my $hash = OP::Hash->new(
        foo => "uno",
        bar => "dos",
        rebar => "tres"
      );
    
      $hash->each( sub {
        print "Have key: $_, value: $hash->{$_}\n";
      } );
    
      #
      # Expected output:
      #
      # Have key: foo, value: uno
      # Have key: bar, value: dos
      # Have key: rebar, value: tres
      #
  • $self->keys()

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

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

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

      my $hash = OP::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 OP::Object::set to always use OP::Hash and OP::Array when it can.

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

    Returns the number of key/value pairs in self

  • $self->isEmpty()

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

SEE ALSO

This file is part of OP.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 117:

You forgot a '=back' before '=head1'