#!/usr/bin/perl use strict; use warnings; use Hook::LexWrap; { my $foo; sub foo { @_ ? $foo = shift : $foo; } my $wrapper = wrap *foo, pre => sub { warn 0+@_, " @_"; # splice @_, 0, 1, lc( $_[0]) if @_ > 1; # bad $_[0] = lc $_[0] if @_ > 1; #new warn 0+@_, " @_"; }, post => sub { $_[-1] = wantarray ? [ map {uc} @{$_[-1]} ] : uc $_[-1] }; sub wrapper () :lvalue { $wrapper } # keeps the cloistered # lexwrap alive sub _foo () :lvalue { $foo } # inspection hatch } my $str = 'Quux'; my $tmp = $str; printf "Given $str, wrapped setter reports %s, backdoor shows %s, arg is now %s.\n", foo($tmp), _foo, $tmp; # setter printf "Wrapped getter reports %s, and backdoor shows %s\n", foo(), _foo; # getter __END__ 2 Quux ARRAY(0x804b3f8) at hlw.pl line 13. 2 quux ARRAY(0x804b3f8) at hlw.pl line 15. Given Quux, wrapped setter reports QUUX, backdoor shows Quux, arg is now Quux. 1 ARRAY(0x804b50c) at hlw.pl line 13. 1 ARRAY(0x804b50c) at hlw.pl line 15. Wrapped getter reports QUUX, and backdoor shows Quux