in reply to can sub check context for lvalue vs rvalue context?
It's not clear what you are asking, but the comments make me think the goal is to avoid returning expensive tied variables when not necessary.
This is what substr, keys, vec and $#a do. The catch is these are ops, so they can simply do
I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
For a sub, you'd need to locate the op that called the sub first. Contextual::Return can do this for you.
sub foo :lvalue { LVALUE { ... } RVALUE { ... } }
But does this actually speed things up? That's for you to find out.
By the way, there are lighter types of magical variables than tied variables that can be used here.
use Variable::Magic qw( wizard cast ); { my $wiz = wizard( # Called before reading the magic scalar (${$_[0]}). # Assign the value to ${$_[0]} that you want to be fetched. # $data is $_[1]. get => sub { ... }, # Called after an assignment to the magic scalar (${$_[0]}). # $data is $_[1]. set => sub { ... }, ); sub foo :lvalue { LVALUE { my $data = ...; cast(my $magical, $wiz, $data); $magical } RVALUE { ... } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: can sub check context for lvalue vs rvalue context?
by perl-diddler (Chaplain) on May 13, 2018 at 12:30 UTC |