Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: can sub check context for lvalue vs rvalue context?

by ikegami (Patriarch)
on May 11, 2018 at 20:04 UTC ( [id://1214400]=note: print w/replies, xml ) Need Help??


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
    Variable::Magic -- thought it might use a form of tied underneath, but it's really other way around and looks like it might be more efficient, so will maybe poke around w/it for a bit...

    thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found