Know the overkill: When the method is called without an argument (except $self) it should return the internal header hash as ref, as rvalue would be enough.$obj->header('My Header', 'value'); # Set $obj->header('My Header') = 'value'; # Set if ($obj->header('My Header') eq 'value' ) { # Get ...
I know the return statement must be omitted in order to make lvalue work. Because subs return then the return value of the last statement I thought I just put the two different return values at the end of a if () { } else { } statement. This returns an error message (Can't return a temporary from lvalue subroutine), so I put in a return statement for the hash ref.
Now it's working: lvalue for hash element, rvalue for whole hash.
The code looks like this:
My question now: Is this clean enough or already a dirty trick or "black magic"? I would love to hear the opinion from a Perl guru with deeper insight in lvalue subs.sub header : lvalue { my ( $self, $h, $value ) = @_; if ( @_ == 1 ) { return $self->{header}; } $self->{header}->{$h} = $value if defined $value; $self->{header}->{$h}; }
Thanks
In reply to Mixed lvalue/rvalue sub function by mscharrer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |