summaryrefslogtreecommitdiff
path: root/lib/Class/Struct.pm
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2001-05-14 13:42:38 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2001-05-14 13:42:38 +0000
commit726cfeaf1bb81f4cf007dab21416f820c3e9b64e (patch)
tree2c10150e21017cb51eaa22e82c14432bcc1502e4 /lib/Class/Struct.pm
parentab903215d0dba02fe67d564bb62d729b4cbaf13e (diff)
downloadperl-726cfeaf1bb81f4cf007dab21416f820c3e9b64e.tar.gz
More DWIMminess for Class::Struct: calling the array or hash
accessors only with one argument, an array or a hash reference, sets the underlying array or hash. This mirrors nicely also the usage in the constructor. From Bernd Sokolowsky <ulmo@garozzo.franken.de>, via Damian Conway. p4raw-id: //depot/perl@10099
Diffstat (limited to 'lib/Class/Struct.pm')
-rw-r--r--lib/Class/Struct.pm14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/Class/Struct.pm b/lib/Class/Struct.pm
index 6e5de810f7..5c68bf34d3 100644
--- a/lib/Class/Struct.pm
+++ b/lib/Class/Struct.pm
@@ -14,7 +14,7 @@ require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(struct);
-$VERSION = '0.59';
+$VERSION = '0.60';
## Tested on 5.002 and 5.003 without class membership tests:
my $CHECK_CLASS_MEMBERSHIP = ($] >= 5.003_95);
@@ -203,11 +203,13 @@ sub struct {
if( defined $arrays{$name} ){
$out .= " my \$i;\n";
$out .= " \@_ ? (\$i = shift) : return \$r->$elem;\n";
+ $out .= " if (ref(\$i) eq 'ARRAY' && !\@_) { \$r->$elem = \$i; return \$r }\n";
$sel = "->[\$i]";
}
elsif( defined $hashes{$name} ){
$out .= " my \$i;\n";
- $out .= " \@_ ? (\$i = shift) : return \$r->$elem;\n";
+ $out .= " \@_ ? (\$i = shift) : return \$r->$elem;\n";
+ $out .= " if (ref(\$i) eq 'HASH' && !\@_) { \$r->$elem = \$i; return \$r }\n";
$sel = "->{\$i}";
}
elsif( defined $classes{$name} ){
@@ -389,6 +391,10 @@ is C<'@'>, the accessor returns the array element value. If the
element type is C<'*@'>, a reference to the array element is
returned.
+As a special case, when the accessor is called with an array reference
+as the sole argument, this causes an assignment of the whole array element.
+The object reference is returned.
+
=item Hash (C<'%'> or C<'*%'>)
The element is a hash, initialized by default to C<()>.
@@ -403,6 +409,10 @@ assigned to the hash element. If the element type is C<'%'>, the
accessor returns the hash element value. If the element type is
C<'*%'>, a reference to the hash element is returned.
+As a special case, when the accessor is called with a hash reference
+as the sole argument, this causes an assignment of the whole hash element.
+The object reference is returned.
+
=item Class (C<'Class_Name'> or C<'*Class_Name'>)
The element's value must be a reference blessed to the named