Deciding how to behave, whether it was called as an lvalue or not, is a bad idea, since foo(record_name(...)) will call it as an lvalue.
Instead, you must decide how to behave, whether it is used as an lvalue or not.
You can do this by returning magical meaning .
use Variable::Magic qw( cast wizard ); my $wiz = wizard( data => sub { shift; \@_ }, get => sub { my ($ref, $args) = @_; $$ref = get_record_name(@$args); }, set => sub { my ($ref, $args) = @_; set_record_name(@$args, $$ref); }, ); sub record_name :lvalue { cast(my $rv, $wiz, @_); return $rv; }
A small test:
use Data::Dumper; sub get_record_name { print("get: @_\n"); return "val"; } sub set_record_name { print("set: @_\n"); } my $x = record_name("abc", "def");
Output:
get: abc def set: abc def xyz get: abc def set: abc def xyz
Seeing this, you, of course, have learned that you should abandon the idea of โโusing lvalue sub. You can hide all this complexity (for example, using sentinel ), but the complexity remains. Whimsicality is not worth all the complexity. Use separate setters and getters or use an accessory whose role is based on the number of parameters passed to it ( $s=acc(); vs acc($s) ).
ikegami
source share