Still exploring what can be done with lvalue subs in closures, I came to realize that the trinary operator is also an lvalue. It may be used as a switch for access to different cloistered lexicals. Consider
foo becomes the accessor for both the scalar and the array. The ordinary rules of context apply:{ my ($foo,@foo); sub foo () :lvalue { wantarray ? @foo : $foo; } }
More complicated practices are possible. The scalar might be a coderef, to be called on the array.foo = 'Flintstone'; (foo) = qw/Fred Wilma Pebbles/; # Hmmm, odd bug, the lines following demonstrate what works # print scalar(foo), ', ', $_, $/ for foo; print join ' & ', foo; print ' ', scalar(foo),$/;
Something special can happen in void context, by using a defined wantarray ? : ; term in the accessor, though the options are limited by each term's need to be an lvalue. Here a scalar closure is undefined by being called in void context:
This is just exploration, not part of a project. What can you think of to do with it?{ my $foo; sub foo () :lvalue { defined wantarray ? $foo : undef $foo; } }
After Compline,
Zaxo
In reply to More Lvalue Subs - Double-Barreled Closures by Zaxo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |