Test::S...ashBase(3)

πŸ“¦
version 1.302027 Β· 2 versions
, in fedora 43

πŸ’– Test::S...ashBase(3) β€” User Contributed Perl Documentation πŸ’–

okay bestie, so this is Test::S...ashBase(3), a page from the User Contributed Perl Documentation section, and honestly? It's giving "make me a whole class out of a hashref, but make it cute." Let's go. πŸ¦‹


🌸 NAME 🌸

Test::Stream::HashBase β€” the base class for classes that use a hashref of a hash. yes really. that's the whole vibe. πŸ’…


πŸ’€ DEPRECATED πŸ’€

okay so this is the part where I hold your hand: this distribution is deprecated. It's been replaced by Test2, Test2::Suite, and Test2::Workflow. not me being the bearer of bad news 😭

If you need to make the jump, there's a conversion guide waiting for you in Test::Stream::Manual::ToTest2. go read it, i believe in you. ✨


πŸŽ€ SYNOPSIS πŸŽ€

A class, bestie:

package My::Class;
use strict;
use warnings;

use Test::Stream::HashBase accessors => [qw/foo bar baz/];

# Chance to initialize defaults
sub init {
    my $self = shift;    # No other args
    $self->{+FOO} ||= "foo";
    $self->{+BAR} ||= "bar";
    $self->{+BAZ} ||= "baz";
}

sub print {
    print join ", " => map { $self->{$_} } FOO, BAR, BAZ;
}

Now subclass it πŸ‘‘

package My::Subclass;
use strict;
use warnings;

# Note, you should subclass before loading HashBase.
use base 'My::Class';
use Test::Stream::HashBase accessors => ['bat'];

sub init {
    my $self = shift;

    # We get the constants from the base class for free.
    $self->{+FOO} ||= 'SubFoo';
    $self->{+BAT} || = 'bat';

    $self->SUPER::init();
}

and then actually use it:

package main;
use strict;
use warnings;
use My::Class;

my $one = My::Class->new(foo => 'MyFoo', bar => 'MyBar');

# Accessors!
my $foo = $one->foo;    # 'MyFoo'
my $bar = $one->bar;    # 'MyBar'
my $baz = $one->baz;    # Defaulted to: 'baz'

# Setters!
$one->set_foo('A Foo');
$one->set_bar('A Bar');
$one->set_baz('A Baz');

# Clear!
$one->clear_foo;
$one->clear_bar;
$one->clear_baz;

$one->{+FOO} = 'xxx';

like. look at that. main character energy fr fr πŸ”₯


✨ DESCRIPTION ✨

So this package generates classes based on hashrefs. the way that works is: use it, and it hands you a new() method plus whatever accessors you ask for. the generated accessors are getters, and set_ACCESSOR setters get generated for you too. you also get constants for each accessor (all caps, obviously πŸ’…) which return the key into the hash for that accessor. single inheritence is supported as well. slay.


🌈 IMPORT ARGUMENTS 🌈

  • accessors => [...] β€” this is how you define your accessors. head down to the ACCESSORS section below, i gotchu. πŸ‘‡
  • base => $class β€” DEPRECATED bestie just do use base 'Parent::Class'; before loading HashBase, no cap. this is how you subclass a Test::Stream::Hashbase class, and it gives you all the constants of the parent(s). 🫢
  • into => $class β€” this is a way to apply HashBase to another class. like:
package My::Thing;

sub import {
    my $caller = caller;
    Test::Stream::HashBase->import(@_, into => $class);
    ...
}

πŸ¦‹ METHODS πŸ¦‹

⭐ PROVIDED BY HASH BASE ⭐

  • $it = $class->new(@VALUES) β€” create a new instance using key/value pairs. that's it, that's the method. πŸ’–

πŸŽ€ HOOKS πŸŽ€

  • $self->init() β€” this is your little moment to set some default values on your fields. the only argument is $self, and its indexes are already set from the constructor. go off. ✨

πŸ’… ACCESSORS πŸ’…

To generate accessors you list them when using the module:

use Test::Stream::HashBase accessors => [qw/foo/];

This will generate the following subs in your namespace:

  • foo() β€” getter, used to get the value of the "foo" field.
  • set_foo() β€” setter, used to set the value of the "foo" field.
  • clear_foo() β€” clearer, used to completely remove the 'foo' key from the object hash. gone. deleted. bye. πŸ’€
  • FOO() β€” constant, returs the field "foo"'s key into the class hashref. subclasses will also get this function as a constant, not simply a method, which means it gets copied into the subclass namespace.
the main reason for using these constants is to help you dodge spelling mistakes and similar typos. it will not help you if you forget to prefix the + though. so like. remember the +. πŸ™ˆ

πŸ‘‘ SUBCLASSING πŸ‘‘

You can subclass an existing HashBase class. yes, like this:

use Test::Stream::HashBase
    base      => 'Another::HashBase::Class',
    accessors => [qw/foo bar baz/];

The base class gets added to @ISA for you, and all the constants from base classes get added to subclasses automatically. no extra work, that's the whole point. πŸ’œ


🧚 UTILITIES 🧚

hashbase has a handful of class methods that can be used to generate accessors. these methods ARE NOT exported, and they're not attached to objects created with hashbase. just so we're clear. ✨

  • $sub = Test::Stream::HashBase->gen_accessor($field) β€” generates a coderef that acts as an accessor for the specified field.
  • $sub = Test::Stream::HashBase->gen_getter($field) β€” generates a coderef that acts as a getter for the specified field.
  • $sub = Test::Stream::HashBase->get_setter($field) β€” generates a coderef that acts as a setter for the specified field.

These all work in the same way, except that getters only get, setters always set, and accessors can get and/or set. iykyk πŸ’…

my $sub = Test::Stream::HashBase->gen_accessor('foo');
my $foo = $obj->$sub();
$obj->$sub('value');

You can also add the sub to your class as a named method:

*foo = Test::Stream::HashBase->gen_accessor('foo');

πŸ”₯ SOURCE πŸ”₯

The source code repository for Test::Stream can be found at http://github.com/Test-More/Test-Stream/. go star it or whatever, be normal. ⭐


🌸 MAINTAINERS 🌸

Chad Granum <exodist@cpan.org>


πŸ’– AUTHORS πŸ’–

Chad Granum <exodist@cpan.org>


Copyright 2015 Chad Granum <exodist7@gmail.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/


and that's the whole page bestie!! 🫢 this one came from perl v5.42.0 and it's dated 2025-07-25 β€” literally iconic, no notes, periodt. πŸ’…βœ¨πŸŒˆ

πŸ“¦ where this page comes from