NAME

Hash::AutoHash::AVPairsSingle - Object-oriented access to hash with simple (non-reference) elements

VERSION

Version 1.14

SYNOPSIS

use Hash::AutoHash::AVPairsSingle;

# create object and set intial values
my $avp=new Hash::AutoHash::AVPairsSingle name=>'Joe',hobby=>'chess';

# access or change hash elements via methods
my $name=$avp->name;                        # 'Joe'
$avp->name('Joey');                         # change name to 'Joey'
$avp->pets({dog=>'Spot'});                  # illegal - reference

# you can also use standard hash notation and functions
my $name=$avp->{name};                      # 'Joey'
$avp->{name}='Joe';                         # change name back to 'Joe'
my($name,$hobby)=@$avp{qw(name hobby)};     # get 2 elements in one statement
my @keys=keys %$avp;                        # ('name','hobby')
my @values=values %$avp;                    # ('Joe','chess')

while(my($key,$value)=each %$avp) {
   print "$key => @$value\n";               # prints each element as usual
}
delete $avp->{hobby};                       # no more hobby

# alias $avp to regular hash for more concise hash notation
use Hash::AutoHash::AVPairsSingle qw(autohash_alias);
my %hash;
autohash_alias($avp,%hash);
# access or change hash elements without using ->
$hash{hobby}='go';                          # change hobby to 'go'
my $hobby=$hash{hobby};                     # 'go'
my($name,$hobby)=@hash{qw(name hobby)};     # get 2 elements in one statement

DESCRIPTION

This is a subclass of Hash::AutoHash which wraps a tied hash whose elements are simple values like numbers and strings, not references. "AVP" stands for "attribute-value pairs". Hash::AutoHash::Record uses this class to represent attribute-value pairs parsed from text files.

Like Hash::AutoHash itself, this class lets you get or set hash elements using hash notation or by invoking a method with the same name as the key. See SYNOPSIS for examples.

Also like Hash::AutoHash, this class provides a full plate of functions for performing hash operations on Hash::AutoHash::AVPairsSingle objects. These are useful if you want to avoid hash notation all together.

And also like Hash::AutoHash, you can alias the object to a regular hash for more concise hash notation. See SYNOPSIS for examples. Admittedly, this is a minor convenience, but the reduction in verbosity can be useful in some cases.

As in Hash::AutoHash, the namespace is "clean"; any method invoked on an object is interpreted as a request to access or change an element of the underlying hash. The software accomplishes this by providing all its capabilities through class methods (these are methods, such as 'new', that are invoked on the class rather than on individual objects), functions that must be imported into the caller's namespace, and methods invoked on the tied object implementing the hash.

CAUTION: As of version 1.12, it is not possible to use method notation for keys with the same names as methods inherited from UNIVERSAL (the base class of everything). These are 'can', 'isa', 'DOES', and 'VERSION'. The reason is that as of Perl 5.9.3, calling UNIVERSAL methods as functions is deprecated and developers are encouraged to use method form instead. Previous versions of AutoHash are incompatible with CPAN modules that adopt this style.

new

Title   : new 
Usage   : $avp=new Hash::AutoHash::AVPairsSingle name=>'Joe',hobby=>'chess'
          -- OR --
          $avp=new Hash::AutoHash::AVPairsSingle [name=>'Joe',hobby=>'chess']
          -- OR --
          $avp=new Hash::AutoHash::AVPairsSingle {name=>'Joe',hobby=>'chess'}
Function: Create Hash::AutoHash::AVPairsSingle object and set elements.
Returns : Hash::AutoHash::AVPairsSingle object
Args    : Optional list of key=>value pairs which are used to set elements of
          the object. Args can also be passed as ARRAY or HASH

Functions inherited from Hash::AutoHash

The following functions are inherited from Hash::AutoHash and operate exactly as there. You must import them into your namespace before use.

use Hash::AutoHash::AVPairsSingle
   qw(autohash_alias autohash_tied autohash_get autohash_set
      autohash_clear autohash_delete autohash_each autohash_exists 
      autohash_keys autohash_values 
      autohash_count autohash_empty autohash_notempty)

autohash_alias

Aliasing a Hash::AutoHash::AVPairsSingle object to a regular hash avoids the need to dereference the variable when using hash notation. As a convenience, the autoahash_alias functions can link in either direction depending on whether the given object exists.

Title   : autohash_alias
Usage   : autohash_alias($avp,%hash)
Function: Link $avp to %hash such that they will have exactly the same value.
Args    : Hash::AutoHash::AVPairsSingle object and hash 
Returns : Hash::AutoHash::AVPairsSingle object

autohash_tied

You can access the object implementing the tied hash using Perl's built-in tied function or the autohash_tied function inherited from Hash::AutoHash. Advantages of autohash_tied are (1) it operates directly on the Hash::AutoHash::AVPairsSingle object without requiring a leading '%', and (2) it provide an arguably simpler syntax for invoking methods on the tied object.

Title   : autohash_tied 
Usage   : $tied=autohash_tied($avp)
          -- OR --
          $tied=autohash_tied(%hash)
          -- OR --
          $result=autohash_tied($avp,'some_method',@parameters)
          -- OR --
          $result=autohash_tied(%hash,'some_method',@parameters)
Function: The first two forms return the object implementing the tied hash. The
          latter two forms invoke a method on the tied object. 
          In forms 1 and 3, the first argument is the 
          Hash::AutoHash::AVPairsSingle object.
          In forms 2 and 4, the first argument is a hash to which a 
          Hash::AutoHash::AVPairsSingle object has been aliased
Returns : In forms 1 and 2, object implementing tied hash or undef.
          In forms 3 and 4, result of invoking method (which can be anything or
          nothing), or undef.
Args    : Form 1. Hash::AutoHash::AVPairsSingle object
          Form 2. hash to which Hash::AutoHash::AVPairsSingle object is aliased
          Form 3. Hash::AutoHash::AVPairsSingle object, method name, optional 
            list of parameters for method
          Form 4. hash to which Hash::AutoHash::AVPairsSingle object is aliased, 
            method name, optional list of parameters for method

autohash_get

Title   : autohash_get
Usage   : ($name,$hobby)=autohash_get($avp,qw(name hobby))
Function: Get values for multiple keys.
Args    : Hash::AutoHash::AVPairsSingle object and list of keys
Returns : list of argument values

autohash_set

Title   : autohash_set
Usage   : autohash_set($avp,name=>'Joe Plumber',first_name=>'Joe')
          -- OR --
          autohash_set($avp,['name','first_name'],['Joe Plumber','Joe'])
Function: Set multiple arguments in existing object.
Args    : Form 1. Hash::AutoHash::AVPairsSingle object and list of key=>value pairs
          Form 2. Hash::AutoHash::MultiValue object, ARRAY of keys, ARRAY of 
          values
Returns : Hash::AutoHash::AVPairsSingle object

Functions for hash-like operations

The remaining functions provide hash-like operations on Hash::AutoHash::AVPairsSingle objects. These are useful if you want to avoid hash notation all together.

autohash_clear

Title   : autohash_clear
Usage   : autohash_clear($avp)
Function: Delete entire contents of $avp
Args    : Hash::AutoHash::AVPairsSingle object
Returns : nothing

autohash_delete

Title   : autohash_delete
Usage   : autohash_delete($avp,@keys)
Function: Delete keys and their values from $avp.
Args    : Hash::AutoHash::AVPairsSingle object, list of keys
Returns : nothing

autohash_exists

Title   : autohash_exists
Usage   : if (autohash_exists($avp,$key)) { ... }
Function: Test whether key is present in $avp.
Args    : Hash::AutoHash::AVPairsSingle object, key
Returns : boolean

autohash_each

Title   : autohash_each
Usage   : while (my($key,$value)=autohash_each($avp)) { ... }
          -- OR --
          while (my $key=autohash_each($avp)) { ... }
Function: Iterate over all key=>value pairs or all keys present in $avp
Args    : Hash::AutoHash::AVPairsSingle object
Returns : list context: next key=>value pair in $avp or empty list at end
          scalar context: next key in $avp or undef at end

autohash_keys

Title   : autohash_keys
Usage   : @keys=autohash_keys($avp)
Function: Get all keys that are present in $avp
Args    : Hash::AutoHash::AVPairsSingle object
Returns : list of keys

autohash_values

Title   : autohash_values
Usage   : @values=autohash_values($avp)
Function: Get the values of all keys that are present in $avp
Args    : Hash::AutoHash::AVPairsSingle object
Returns : list of values

autohash_count

Title   : autohash_count
Usage   : $count=autohash_count($avp)
Function: Get the number keys that are present in $avp
Args    : Hash::AutoHash::AVPairsSingle object
Returns : number

autohash_empty

Title   : autohash_empty
Usage   : if (autohash_empty($avp)) { ... }
Function: Test whether $avp is empty
Args    : Hash::AutoHash::AVPairsSingle object
Returns : boolean

autohash_notempty

Title   : autohash_notempty
Usage   : if (autohash_notempty($avp)) { ... }
Function: Test whether $avp is not empty. Complement of autohash_empty
Args    : Hash::AutoHash::AVPairsSingle object
Returns : boolean

SEE ALSO

perltie and Tie::Hash present background on tied hashes.

Hash::AutoHash provides the object wrapping machinery. The documentation of that class includes a detailed list of caveats and cautions. Hash::AutoHash::Args, Hash::AutoHash::MultiValued, Hash::AutoHash::AVPairsMulti, Hash::AutoHash::Record are other subclasses of Hash::AutoHash.

Hash::AutoHash::AVPairsMulti is similar but allows each attribute to have multiple values.

Hash::AutoHash::Record uses this class to to represent attribute-value pairs parsed from text files.

AUTHOR

Nat Goodman, <natg at shore.net>

BUGS

Please report any bugs or feature requests to bug-hash-autohash-avpairssingle at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-AutoHash-AVPairsSingle. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Hash::AutoHash::AVPairsSingle

You can also look for information at:

COPYRIGHT & LICENSE

Copyright 2009 Nat Goodman.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.