Test::S...ashBase(3)
π 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 byTest2,Test2::Suite, andTest2::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 douse base 'Parent::Class';before loading HashBase, no cap. this is how you subclass aTest::Stream::Hashbaseclass, 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 π
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
- fedora 43: perl-Test-Stream 1.302027-35.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms