man Class::MethodMaker::scalar () - Create methods for handling a scalar value.

NAME

Class::Method::scalar - Create methods for handling a scalar value.

SYNOPSIS

  package MyClass;
  use Class::MethodMaker
    [ scalar => [qw/ a -static s /]];

  sub new {
    my $class = shift;
    bless {}, $class;
  }

  package main;

  my $m = MyClass->new;
  my $a, $x;

  $a = $m->a;       # *undef*
  $x = $m->a_isset; # false
  $a = $m->a(1);    # 1
  $m->a(3);
  $x = $m->a_isset; # true
  $a = $m->a;       # 3
  $a = $m->a(5);     # 5;
  $m->a_reset;
  $x = $m->a_isset; # false

DESCRIPTION

Creates methods to handle array values in an object. For a component named CWx, by default creates methods CWx, CWx_reset, CWx_isset, CWx_clear.

Methods available are:

CI*

  $m->a(3);
  $a = $m->a;       # 3
  $a = $m->a(5);     # 5;

Created by default. If an argument is provided, the component is set to that value. The method returns the value of the component (after assignment to a provided value, if appropriate).

CI*_reset

  $m->a_reset;

Created by default. Resets the component back to its default. Normally, this means that CW*_isset will return false, and CW* will return undef. If CW-default is in effect, then the component will be set to the default value, and CW*_isset will return true. If CW-default_ctor is in effect, then the default subr will be invoked, and its return value used to set the value of the component, and CW*_isset will return true.

Advanced Note: actually, defaults are assigned as needed: typically, the next time a the value of a component is read.

CI*_isset

  print $m->a_isset ? "true" : "false";

Created by default. Whether the component is currently set. This is different from being defined; initially, the component is not set (and if read, will return undef); it can be set to undef (which is a set value, which also returns undef). Having been set, the only way to unset the component is with <*_reset>.

If a default value is in effect, then <*_isset> will always return true.

CI*_clear

  $m->a(5);
  $a = $m->a;       # 5
  $x = $m->a_isset; # true
  $m->a_clear;
  $a = $m->a;       # *undef*
  $x = $m->a_isset; # true

Created by default. A shorthand for setting to undef. Note that the component will be set to undef, not reset, so CW*_isset will return true.

CI*_get

  package MyClass;
  use Class::MethodMaker
    [ scalar => [{'*_get' => '*_get'}, 'a'],
      new    => new, ];

  package main;
  my $m = MyClass->new;
  $m->a(3);
  $a = $m->a_get;     # 3
  $a = $m->a_get(5);  # 3; ignores argument
  $a = $m->a_get(5);  # 3; unchanged by previous call

Created on request. Retrieves the value of the component without setting (ignores any arguments passed).

CI*_set

  package MyClass;
  use Class::MethodMaker
    [ scalar => [{'*_set' => '*_set'}, 'a'],
      new    => new, ];

  package main;
  my $m = MyClass->new;
  $m->a(3);
  $a = $m->a_set;     # *undef*
  $a = $m->a_set(5);  # *undef*; value is set but not returned
  $a = $m->a;         # 5

Created on request. Sets the component to the first argument (or undef if no argument provided). Returns no value.