Tie::Hash::Method(3)
π NAME π
Bestie, welcome to the Tie::Hash::Method(3) page β a User Contributed Perl Documentation moment. The module is Tie::Hash::Method, and itβs giving tied hash with specific methods overriden by callbacks. Thatβs the whole assignment. π
πΈ VERSION πΈ
Version 0.02. Tiny, but iconic.
β¨ SYNOPSIS β¨
Hereβs the whole flex in code form:
tie my %hash, 'Tie::Hash::Method',
FETCH => sub {
exists $_[0]->base_hash->{$_[1]} ? $_[0]->base_hash->{$_[1]} : $_[1]
};
π¦ DESCRIPTION π¦
Tie::Hash::Method gives you a way to create a tied hash with specific overriden behaviour without having to create a new class to do it. A tied hash with no methods overriden is functionally equivalent to a normal hash. No cap. π―
Each method in a standard tie can be overriden by providing a callback to the tie call. So for instance if you wanted a tied hash that changed 'foo' into 'bar' on store you could say:
tie my %hash, 'Tie::Hash::Method',
STORE => sub {
(my $v=pop)=~s/foo/bar/g if defined $_[2];
return $_[0]->base_hash->{$_[1]}=$v;
};
The callback is called with exactly the same arguments as the tie itself, in particular the tied object is always passed as the first argument. Like, the object is always the main character in slot zero. π
The tied object is itself an array, which contains a second hash in the HASH slot (index 0) which is used to perform the default operations.
The callbacks available are in a hash keyed by name in the METHOD slot of the array (index 1).
If your code needs to store extra data in the object it should be stored in the PRIVATE slot of the object (index 2).
No future release of this module will ever use or alter anything in that slot. Itβs yours, bestie. π«Ά
The arguments passed to the tie constructor will be seperated by the case of their keys. The ones with all capitals will be stored in the METHOD hash, and the rest will be stored in the PRIVATE hash.
π CALLBACKS π
STORE this, key, valueβ Store datum value into key for the tied hash this. πFETCH this, keyβ Retrieve the datum in key for the tied hash this. πFIRSTKEY thisβ Return the first key in the hash. πNEXTKEY this, lastkeyβ Return the next key in the hash. β‘οΈEXISTS this, keyβ Verify that key exists with the tied hash this. βDELETE this, keyβ Delete the key key from the tied hash this. πCLEAR thisβ Clear all values from the tied hash this. π§ΉSCALAR thisβ Returns what evaluating the hash in scalar context yields. π
π Methods π
base_hashβ return or sets the underlying hash for this tie.
$_[0]->base_hash->{$key}= $value;
hβ alias forbase_hash.
$_[0]->h->{$key}= $value;
private_hashβ Return or sets the hash of private data associated with this tie. This value will never be touched by any code in this class or its subclasses. It is purely object specific.
$_[0]->p->{'something'}= 'cake!';
pβ alias forprivate_hashmethod_hashβ return or sets the hash of methods that are overriden for this tie. Exactly why you would want to use this is a little beyond my imagination, but for those who can think of a reason here is a nice way to do it. :-)methodsβ Returns a list of methods that are overriden for this tie. Why this would be useful escapes me, but here it is anyway for Completeness sake, whoever she is, but people are always coding for her so I might as well too.
π Exportable Subs π
The following subs are exportable on request:
hash_overload(PAIRS)β Returns a reference to a hash tied with the specified callbacks overriden. Just a short cut.HASHβ Constant subroutine equivalent to0METHODβ Constant subroutine equivalent to1PRIVATEβ Constant subroutine equivalent to2
π AUTHOR π
Yves Orton, "<yves at cpan.org>"
π BUGS π
Please report any bugs or feature requests to "bug-tie-hash-method at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tie-Hash-Method>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. Bug pipeline: activated. π
π«Ά SUPPORT π«Ά
You can find documentation for this module with the perldoc command.
perldoc Tie::Hash::Method
You can also look for information at:
- RT: CPAN's request tracker β <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tie-Hash-Method>
- AnnoCPAN: Annotated CPAN documentation β <http://annocpan.org/dist/Tie-Hash-Method>
- CPAN Ratings β <http://cpanratings.perl.org/d/Tie-Hash-Method>
- Search CPAN β <http://search.cpan.org/dist/Tie-Hash-Method>
π¦ ACKNOWLEDGEMENTS π¦
Nothing listed here β just a heading and vibes. Respect the silence. π¦π
π COPYRIGHT & LICENSE π
Copyright 2008 Yves Orton, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
From perl v5.32.1, dated 2022-01-21, this was the Tie::Hash::Method(3) page. Bye bestie, go tie some hashes! π
β¨
π¦ where this page comes from
- epel 9: perl-Tie-Hash-Method 0.02-19.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms