Class::MethodMaker::scalar(3) User Contributed Perl Documentation Class::MethodMaker::scalar(3)

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

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

Creates methods to handle array values in an object. For a component named "x", by default creates methods "x", "x_reset", "x_isset", "x_clear".

Methods available are:

"*"

$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).

*_reset

$m->a_reset;

Created by default. Resets the component back to its default. Normally, this means that *_isset will return false, and "*" will return undef. If "-default" is in effect, then the component will be set to the default value, and *_isset will return true. If "-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 *_isset will return true.

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

*_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.

*_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 *_isset will return true.

*_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).

*_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.

2023-07-25 perl v5.38.0