Suppose you have an object built around a complex hash. For some reason, you want to give other objects access to only part of it -- but you don't want to go through variable scoping or enforced encapsulation. Try this technique to make mini-objects.
Notes:
- First, the recursion isn't great. There are better ways to do this, but none of them I know of are easier or more clear to explain.
- Next, there's no real error checking, except for the definedness factor. You'll want to implement that.
- The key checking isn't very smart, either, but it works.
As for the tests, note that we display the keys for the top level object. Then, we display the keys for the newly created child object. Finally, we update a key nominally in the child object through the top object, and demonstrate that it is updated in the child object. No real thrill here, if you understand references, but a big caveat if you don't.
This one might be useful, somewhere.
#!/usr/bin/perl -w
use strict;
package SliceMe;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $self = {
'top' => '1',
'nest1' => {
'hi' => '1',
'there' => '2'
},
'nest2' => {
'nest2a' => {
'first' => '1',
'second' => '2',
},
'nest2b' => 'not here'
},
'bottom' => '1',
};
bless ($self, $class);
return $self;
}
sub slice {
my $self = shift;
my $class = ref($self) || $self;
my $key = shift;
my $hash;
$hash = _pare($self, $key);
bless ($hash, $class) if defined $hash;
return $hash;
}
sub _pare {
my $hash_ref = shift;
my $key = shift;
if (exists $hash_ref->{$key}) {
return $hash_ref->{$key};
} else {
foreach my $try_hash (keys %$hash_ref) {
_pare($try_hash, $key);
}
return undef;
}
}
package main;
my $obj = new SliceMe;
print "obj keys:\n";
print "\t$_\n" foreach (keys %$obj);
my $obj_nest2 = $obj->slice("nest2");
print "obj_nest2 keys:\n";
print "\t$_\n" foreach (keys %$obj_nest2);
foreach (keys %$obj_nest2) {
print "$_\t=>\t", $obj_nest2->{$_}, "\n";
}
$obj->{nest2}->{nest2b} = "okay, here now";
print $obj_nest2->{nest2b}, "\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.