The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Object::AutoAccessor - Accessor class by using AUTOLOAD

SYNOPSIS

  use Object::AutoAccessor;
  
  my $obj = Object::AutoAccessor->new();
  
  # setter methods
  $obj->foo('bar');
  $obj->set_foo('bar');
  $obj->param(foo => 'bar');
  
  # getter methods
  $obj->foo();
  $obj->get_foo();
  $obj->param('foo');
  
  # set/get array
  $obj->array([qw(foo bar)]);
  $obj->push(array => 'baz');
  my $baz = $obj->pop('array');
  my $foobar = $obj->join(',', 'array'); # got 'foo,bar'
  
  # set/get hash
  $obj->hash(+{ foo => 'fooval', bar => 'barval' });
  my $hashref = $obj->hash;
  my @keys = $obj->keys('hash');
  my @values = $obj->values('hash');
  my ($key, $val) = $obj->each('hash');
  
  # set/get coderef
  $obj->code(sub { print "CODEREF\n" });
  my $code = $obj->code;
  $code->();
  
  # set/get globref
  $obj->glob(\*STDOUT);
  my $glob = $obj->glob;
  print $glob "Hello\n";
  
  # is_hashref/arrayref/coderef/globref/scalar
  $obj->is_hashref('hash');
  $obj->is_arrayref('array');
  $obj->is_coderef('code');
  $obj->is_globref('glob');
  $obj->is_scalar('foo');
  
  # $obj->param() is compatible with HTML::Template->param()
  my @keywords = $obj->param();
  my $val = $obj->param('hash');
  $obj->param(key => 'val');

DESCRIPTION

Object::AutoAccessor is a Accessor class to get/set values by AUTOLOADed method automatically, and also can use various methods of the same name as built-in functions such as push() , pop() , each() , join() , length() , sprintf() and so on. Moreover, param() is compatible with HTML::Template module, so you can use Object::AutoAccessor object for HTML::Template's associate option.

METHODS

new ( [ OPTIONS ] )

Create a new Object::AutoAccessor object. Then you can use several options to control object's behavior.

  • autoload

    If set to 0, the object cannot use the AUTOLOADed-accessor-method such as foo() , set_foo() and get_foo() but param() . Defaults to 1.

  • bindstyle

    If set to 'sql', behavior of bind() method changes into SQL-style-quoting. Defaults to 'normal' or undef.

renew ( [ OPTIONS ] )

Create a new Object::AutoAccessor object to remaining current options.

KEY ( [ VALUE ] )

This method provides an accessor that methodname is same as keyname by using AUTOLOAD mechanism.

  # setter methods
  $obj->foo('bar');
  $obj->set_foo('bar');
  $obj->param(foo => 'bar');
  
  # getter methods
  $obj->foo();
  $obj->get_foo();
  $obj->param('foo');
param ( [ KEY => VALUE, ... ] )

This method is compatible with param() method of HTML::Template module.

  # set value
  $obj->param(foo => 'bar');
  $obj->param(
    foo => 'bar',
    bar => [qw(1 2 3)],
    baz => { one => 1, two => 2, three => 3 }
  );
  
  # get value
  $obj->param('foo'); # got 'bar'
  
  # get list keys of parameters
  @keys = $obj->param();
bind ( KEY, BIND )

This method provides a simple replacing mechanism that changes placeholder to bindings just looks like execute() method of DBI.

  $obj->sentence(q{What is the ? ? in ?\?});
  
  # $result is "What is the highest mountain in Japan?"
  $result = $obj->bind(sentence => qw(highest mountain Japan));
bindstyle ( STYLE )

If you want SQL-style-quoting, use bindstyle() and set value 'sql'.

  @binds = ('bar' "It's OK" '-0.123');
  $obj->bindstyle('sql');
  $obj->statement(q{SELECT * FROM foo WHERE BAR = ? AND BAZ = ? AND DECIMAL = ?});
  
  # $result is "SELECT * FROM foo WHERE BAR = 'bar' AND BAZ = 'It''s OK' AND DECIMAL = -0.123"
  $result = $obj->bind(statement => @binds);
as_hashref ()

As shown in name. :)

AUTHOR

Copyright 2005 Michiya Honda, <pia@cpan.org> All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

HTML::Template.