Subject: | Methods sometimes return values, sometimes objects |
Hi
I'm worried by the fact that a getter/setter, when called with a
parameter, sets the value, but returns the object and not the value.
1) x.pl:
#!/usr/bin/env perl
package Module;
use strict;
use warnings;
use Hash::FieldHash ':all';
fieldhash my %current => 'current';
fieldhash my %start => 'start';
# -----------------------------------------------
sub _init
{
my($self, $arg) = @_;
$$arg{current} = ''; # Can't be set in new().
$$arg{start} ||= ''; # Can be set in new().
$self = from_hash($self, $arg);
$self -> current($self -> start);
return $self;
} # End of _init.
# -----------------------------------------------
sub new
{
my($class, %arg) = @_;
my($self) = bless {}, $class;
$self = $self -> _init(\%arg);
return $self;
} # End of new.
# -----------------------------------------------
sub run
{
my($self) = @_;
} # End of run.
# -----------------------------------------------
package main;
use strict;
use warnings;
# -----------------------------------------------
my($m) = Module -> new(start => 'One');
print "Test 1\n";
print "start: ", $m -> start, ". \n";
print "current: ", $m -> current, ". \n";
$m -> start('Two');
print "Test 2\n";
print "start: ", $m -> start, ". \n";
print "current: ", $m -> current, ". \n";
$m -> current($m -> start);
print "Test 3\n";
print "start: ", $m -> start, ". \n";
print "current: ", $m -> current, ". \n";
$m -> start('Three');
print "Test 4\n";
print "start: ", $m -> start, ". \n";
print "current: ", $m -> current($m -> start), ". \n";
2) x.log
Test 1
start: One.
current: One.
Test 2
start: Two.
current: One.
Test 3
start: Two.
current: Two.
Test 4
start: Three.
current: Module=HASH(0x1024e88).
Why is the last line not Three? Is this by design or a bug :-)?
My expectation is that the result should be Three. It's certainly very
confusing.
Cheers
Ron