mscharrer has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mixed lvalue/rvalue sub function
by kyle (Abbot) on Apr 18, 2008 at 15:24 UTC | |
|
Re: Mixed lvalue/rvalue sub function
by elmex (Friar) on Apr 18, 2008 at 13:23 UTC | |
by mscharrer (Hermit) on Apr 18, 2008 at 13:38 UTC | |
by elmex (Friar) on Apr 18, 2008 at 13:44 UTC | |
by mscharrer (Hermit) on Apr 18, 2008 at 17:18 UTC |