forked from GMOD/jbrowse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAutoHash.pm
114 lines (71 loc) · 2.09 KB
/
AutoHash.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
=head1 NAME
AutoHash.pm
=head1 SYNOPSIS
Simple Perl module wrapping a hashref with AUTOLOAD-ed accessors.
=head1 EXAMPLES
use AutoHash;
# construct a hash with two key-value pairs
my $autohash = AutoHash->new ( "key1" => "value1",
"key2" => "value2" );
# get a value
print $autohash->key1, "\n";
# set a value
$autohash->key2 ("new value");
print $autohash->key2, "\n";
=head1 GENERAL USAGE
An AutoHash object is a blessed hash reference.
Its only inbuilt method is the constructor, 'new'.
All other methods will be automatically interpreted as hash element accessors for the eponymous tag.
If the method is called with an argument, it's a setter; otherwise, it's a getter.
=head1 METHODS
=cut
package AutoHash;
use Exporter;
@ISA = qw (Exporter);
@EXPORT = qw (new AUTOLOAD);
@EXPORT_OK = @EXPORT;
use strict;
use vars '@ISA';
use Carp;
=head2 new
my $autohash1 = AutoHash->new();
my $autohash2 = AutoHash->new (%existing_hash);
Creates a new AutoHash object.
=cut
sub new {
my ($class, @data) = @_;
my $self = {@data};
$class = ref($class) if ref($class);
bless $self, $class;
return $self;
}
=head2 Accessors (getters)
$autohash->MYTAG()
Returns the hash value with tag "MYTAG".
=head2 Accessors (setters)
$autohash->MYTAG ($MYVALUE)
Sets the value of hash tag "MYTAG" to $MYVALUE.
Returns $MYVALUE.
Creates a new AutoHash object.
=cut
# AUTOLOAD method
sub AUTOLOAD {
my ($self, @args) = @_;
my $sub = our $AUTOLOAD;
$sub =~ s/.*:://; # strip off module path
# check for DESTROY
return if $sub eq "DESTROY";
# get or set
return @args
? ($self->{$sub} = shift(@args))
: $self->{$sub};
}
=head1 AUTHOR
Ian Holmes E<lt>ihh@berkeley.eduE<gt>
Copyright (c) 2007-2009 The Evolutionary Software Foundation
This package and its accompanying libraries are free software; you can
redistribute it and/or modify it under the terms of the LGPL (either
version 2.1, or at your option, any later version) or the Artistic
License 2.0. Refer to LICENSE for the full license text.
=cut
1;